Merge pull request #43 from haganbmj/localized-strings-wildcard

feat: restore locale level translations for other packages (fixes #41)
This commit is contained in:
Brendan Hagan
2022-08-02 21:45:17 -04:00
committed by GitHub
5 changed files with 45 additions and 17 deletions
+2
View File
@@ -6,6 +6,8 @@ HEAD: new items added as changes are made
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Features: Features:
* Restore reading of package translations from top level locale file. (haganbmj#43)
* Bundle other locale files, provide English translations as fallback rather than just using the key name.
Bug fixes: Bug fixes:
+15 -12
View File
@@ -48,7 +48,7 @@ IMPLEMENT_REFLECTION(Field) {
} }
REFLECT(name); REFLECT(name);
REFLECT_LOCALIZED(caption); REFLECT_LOCALIZED(caption);
REFLECT_LOCALIZED(description); REFLECT_LOCALIZED(description); // FIXME: This field is both unused and uninitialized.
REFLECT_N("icon", icon_filename); REFLECT_N("icon", icon_filename);
REFLECT(editable); REFLECT(editable);
REFLECT(save_value); REFLECT(save_value);
@@ -66,24 +66,25 @@ IMPLEMENT_REFLECTION(Field) {
void Field::after_reading(Version ver) { void Field::after_reading(Version ver) {
name = canonical_name_form(name); name = canonical_name_form(name);
if(caption.default_.empty()) caption.default_ = name_to_caption(name); if(caption.default_.empty()) caption.default_ = tr(package_relative_filename, name, name_to_caption);
if(card_list_name.default_.empty()) card_list_name.default_ = capitalize(caption.default_); if(card_list_name.default_.empty()) card_list_name.default_ = tr(package_relative_filename, caption.default_, capitalize);
} }
template <> template <>
intrusive_ptr<Field> read_new<Field>(Reader& reader) { intrusive_ptr<Field> read_new<Field>(Reader& reader) {
intrusive_ptr<Field> field;
// there must be a type specified // there must be a type specified
String type; String type;
reader.handle(_("type"), type); reader.handle(_("type"), type);
if (type == _("text")) return make_intrusive<TextField>(); if (type == _("text")) field = make_intrusive<TextField>();
else if (type == _("choice")) return make_intrusive<ChoiceField>(); else if (type == _("choice")) field = make_intrusive<ChoiceField>();
else if (type == _("multiple choice")) return make_intrusive<MultipleChoiceField>(); else if (type == _("multiple choice")) field = make_intrusive<MultipleChoiceField>();
else if (type == _("boolean")) return make_intrusive<BooleanField>(); else if (type == _("boolean")) field = make_intrusive<BooleanField>();
else if (type == _("image")) return make_intrusive<ImageField>(); else if (type == _("image")) field = make_intrusive<ImageField>();
else if (type == _("symbol")) return make_intrusive<SymbolField>(); else if (type == _("symbol")) field = make_intrusive<SymbolField>();
else if (type == _("color")) return make_intrusive<ColorField>(); else if (type == _("color")) field = make_intrusive<ColorField>();
else if (type == _("info")) return make_intrusive<InfoField>(); else if (type == _("info")) field = make_intrusive<InfoField>();
else if (type == _("package choice")) return make_intrusive<PackageChoiceField>(); else if (type == _("package choice")) field = make_intrusive<PackageChoiceField>();
else if (type.empty()) { else if (type.empty()) {
reader.warning(_ERROR_1_("expected key", _("type"))); reader.warning(_ERROR_1_("expected key", _("type")));
throw ParseError(_ERROR_("aborting parsing")); throw ParseError(_ERROR_("aborting parsing"));
@@ -91,6 +92,8 @@ intrusive_ptr<Field> read_new<Field>(Reader& reader) {
reader.warning(_ERROR_1_("unsupported field type", type)); reader.warning(_ERROR_1_("unsupported field type", type));
throw ParseError(_ERROR_("aborting parsing")); throw ParseError(_ERROR_("aborting parsing"));
} }
field->package_relative_filename = reader.getPackage()->relativeFilename().Clone();
return field;
} }
// ----------------------------------------------------------------------------- : Style // ----------------------------------------------------------------------------- : Style
+1
View File
@@ -60,6 +60,7 @@ public:
Alignment card_list_align; ///< Alignment of the card list colummn. Alignment card_list_align; ///< Alignment of the card list colummn.
OptionalScript sort_script; ///< The script to use when sorting this, if not the value. OptionalScript sort_script; ///< The script to use when sorting this, if not the value.
Dependencies dependent_scripts; ///< Scripts that depend on values of this field Dependencies dependent_scripts; ///< Scripts that depend on values of this field
String package_relative_filename;
/// Creates a new Value corresponding to this Field /// Creates a new Value corresponding to this Field
virtual ValueP newValue() = 0; virtual ValueP newValue() = 0;
+18
View File
@@ -120,6 +120,24 @@ String tr(const Package& pkg, const String& subcat, const String& key, DefaultLo
return loc->tr(subcat, key, def); return loc->tr(subcat, key, def);
} }
String tr(const String& pkg_relative_filename, const String& key, DefaultLocaleFun def) {
if (!the_locale) return def(key);
SubLocaleP loc = the_locale->package_translations[pkg_relative_filename];
if (!loc) {
loc = find_wildcard_and_set(the_locale->package_translations, pkg_relative_filename);
}
return loc->tr(key, def);
}
String tr(const String& pkg_relative_filename, const String& subcat, const String& key, DefaultLocaleFun def) {
if (!the_locale) return def(key);
SubLocaleP loc = the_locale->package_translations[pkg_relative_filename];
if (!loc) {
loc = find_wildcard_and_set(the_locale->package_translations, pkg_relative_filename);
}
return loc->tr(subcat, key, def);
}
// ----------------------------------------------------------------------------- : LocalizedString // ----------------------------------------------------------------------------- : LocalizedString
String const& LocalizedString::get(String const& locale) const { String const& LocalizedString::get(String const& locale) const {
+4
View File
@@ -51,10 +51,14 @@ String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def = warn_and
/// Translate 'key' in the for a Package using the current locale /// Translate 'key' in the for a Package using the current locale
[[deprecated]] [[deprecated]]
String tr(const Package&, const String& key, DefaultLocaleFun def); String tr(const Package&, const String& key, DefaultLocaleFun def);
[[deprecated]]
String tr(const String&, const String& key, DefaultLocaleFun def);
/// Translate 'key' in the for a Package using the current locale /// Translate 'key' in the for a Package using the current locale
[[deprecated]] [[deprecated]]
String tr(const Package&, const String& subcat, const String& key, DefaultLocaleFun def); String tr(const Package&, const String& subcat, const String& key, DefaultLocaleFun def);
[[deprecated]]
String tr(const String&, const String& subcat, const String& key, DefaultLocaleFun def);
/// A localized string for menus /// A localized string for menus
#define _MENU_(s) tr(LOCALE_CAT_MENU, _(s)) #define _MENU_(s) tr(LOCALE_CAT_MENU, _(s))