From e0d9e847402f3912f7aced103f824ba333586fc7 Mon Sep 17 00:00:00 2001 From: Brendan Hagan Date: Tue, 2 Aug 2022 21:10:19 -0400 Subject: [PATCH] feat: restore locale level translations for other packages (fixes #41) This is only a partial fix though, it basically restores the old behavior as a default. That doesn't help though with flipping the definitions to get stuff out of the UI Locale file though. So that'll have to come next for allowing wildcard resolutions to be done from the game(?) or maybe locale extension files? --- CHANGES.md | 2 ++ src/data/field.cpp | 31 +++++++++++++++++-------------- src/data/field.hpp | 3 ++- src/data/locale.cpp | 18 ++++++++++++++++++ src/util/locale.hpp | 8 ++++++-- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 625ef156..21c8a914 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,8 @@ HEAD: new items added as changes are made ------------------------------------------------------------------------------ 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: diff --git a/src/data/field.cpp b/src/data/field.cpp index 533a3cbb..9e85c63c 100644 --- a/src/data/field.cpp +++ b/src/data/field.cpp @@ -48,7 +48,7 @@ IMPLEMENT_REFLECTION(Field) { } REFLECT(name); REFLECT_LOCALIZED(caption); - REFLECT_LOCALIZED(description); + REFLECT_LOCALIZED(description); // FIXME: This field is both unused and uninitialized. REFLECT_N("icon", icon_filename); REFLECT(editable); REFLECT(save_value); @@ -65,32 +65,35 @@ IMPLEMENT_REFLECTION(Field) { } void Field::after_reading(Version ver) { - name = canonical_name_form(name); - if(caption.default_.empty()) caption.default_ = name_to_caption(name); - if(card_list_name.default_.empty()) card_list_name.default_ = capitalize(caption.default_); + name = canonical_name_form(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_ = tr(package_relative_filename, caption.default_, capitalize); } template <> intrusive_ptr read_new(Reader& reader) { + intrusive_ptr field; // there must be a type specified String type; reader.handle(_("type"), type); - if (type == _("text")) return make_intrusive(); - else if (type == _("choice")) return make_intrusive(); - else if (type == _("multiple choice")) return make_intrusive(); - else if (type == _("boolean")) return make_intrusive(); - else if (type == _("image")) return make_intrusive(); - else if (type == _("symbol")) return make_intrusive(); - else if (type == _("color")) return make_intrusive(); - else if (type == _("info")) return make_intrusive(); - else if (type == _("package choice")) return make_intrusive(); + if (type == _("text")) field = make_intrusive(); + else if (type == _("choice")) field = make_intrusive(); + else if (type == _("multiple choice")) field = make_intrusive(); + else if (type == _("boolean")) field = make_intrusive(); + else if (type == _("image")) field = make_intrusive(); + else if (type == _("symbol")) field = make_intrusive(); + else if (type == _("color")) field = make_intrusive(); + else if (type == _("info")) field = make_intrusive(); + else if (type == _("package choice")) field = make_intrusive(); else if (type.empty()) { reader.warning(_ERROR_1_("expected key", _("type"))); throw ParseError(_ERROR_("aborting parsing")); } else { reader.warning(_ERROR_1_("unsupported field type", type)); throw ParseError(_ERROR_("aborting parsing")); - } + } + field->package_relative_filename = reader.getPackage()->relativeFilename().Clone(); + return field; } // ----------------------------------------------------------------------------- : Style diff --git a/src/data/field.hpp b/src/data/field.hpp index bf7c6f9a..ba94022f 100644 --- a/src/data/field.hpp +++ b/src/data/field.hpp @@ -59,7 +59,8 @@ public: LocalizedString card_list_name; ///< Name to use in card list. Alignment card_list_align; ///< Alignment of the card list colummn. 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 virtual ValueP newValue() = 0; diff --git a/src/data/locale.cpp b/src/data/locale.cpp index 8b284b60..b73bc436 100644 --- a/src/data/locale.cpp +++ b/src/data/locale.cpp @@ -118,6 +118,24 @@ String tr(const Package& pkg, const String& subcat, const String& key, DefaultLo loc = find_wildcard_and_set(the_locale->package_translations, pkg.relativeFilename()); } 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 diff --git a/src/util/locale.hpp b/src/util/locale.hpp index 8009cda4..164837dd 100644 --- a/src/util/locale.hpp +++ b/src/util/locale.hpp @@ -50,11 +50,15 @@ String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def = warn_and /// Translate 'key' in the for a Package using the current locale [[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 [[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 #define _MENU_(s) tr(LOCALE_CAT_MENU, _(s))