From df6383d2d8866a657c3c7bdd913b277f9a5c3a15 Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Thu, 13 Aug 2020 10:49:27 +0200 Subject: [PATCH] [apps/i18n] Added default country for languages After choosing a language at onboarding, the country menu now has a specific country selected by default (Spain for Spanish, Italy for Italian...) Default countries are specified in apps/language_preferences.csv Change-Id: Ia6392aceb9bebf7e62a692c5a79eb8c4d7b71a9d --- apps/Makefile | 3 +- apps/i18n.py | 38 ++++++++++++++++--- apps/language_preferences.csv | 9 +++++ apps/on_boarding/localization_controller.cpp | 7 ++++ apps/on_boarding/localization_controller.h | 4 +- .../sub_menu/localization_controller.cpp | 8 ++++ .../sub_menu/localization_controller.h | 4 +- apps/shared/localization_controller.cpp | 2 +- apps/shared/localization_controller.h | 6 +-- 9 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 apps/language_preferences.csv diff --git a/apps/Makefile b/apps/Makefile index e95a74369..eb07e5ceb 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -56,6 +56,7 @@ $(call object_for,apps/apps_container_storage.cpp apps/apps_container.cpp apps/m # I18n file generation country_preferences = apps/country_preferences.csv +language_preferences = apps/language_preferences.csv # The header is refered to as so make sure it's findable this way SFLAGS += -I$(BUILD_DIR) @@ -73,7 +74,7 @@ $(eval $(call rule_for, \ I18N, \ apps/i18n.cpp, \ $(i18n_files), \ - $$(PYTHON) apps/i18n.py --codepoints $(code_points) --countrypreferences $(country_preferences) --header $$(subst .cpp,.h,$$@) --implementation $$@ --locales $$(EPSILON_I18N) --countries $$(EPSILON_COUNTRIES) --files $$^ --generateISO6391locales $$(EPSILON_GETOPT), \ + $$(PYTHON) apps/i18n.py --codepoints $(code_points) --countrypreferences $(country_preferences) --languagepreferences $(language_preferences) --header $$(subst .cpp,.h,$$@) --implementation $$@ --locales $$(EPSILON_I18N) --countries $$(EPSILON_COUNTRIES) --files $$^ --generateISO6391locales $$(EPSILON_GETOPT), \ global \ )) diff --git a/apps/i18n.py b/apps/i18n.py index 83bcdfb8a..3c688d64e 100644 --- a/apps/i18n.py +++ b/apps/i18n.py @@ -21,6 +21,7 @@ parser.add_argument('--locales', nargs='+', help='locale to actually generate') parser.add_argument('--countries', nargs='+', help='countries to actually generate') parser.add_argument('--codepoints', help='the code_points.h file') parser.add_argument('--countrypreferences', help='the country_preferences.csv file') +parser.add_argument('--languagepreferences', help='the language_preferences.csv file') parser.add_argument('--files', nargs='+', help='an i18n file') parser.add_argument('--generateISO6391locales', type=int, nargs='+', help='whether to generate the ISO6391 codes for the languages (for instance "en" for english)') @@ -117,17 +118,32 @@ def parse_codepoints(file): codepoints = parse_codepoints(args.codepoints) +def parse_csv_with_header(file): + res = [] + with io.open(file, 'r', encoding='utf-8') as csvfile: + csvreader = csv.reader(csvfile, delimiter=',') + for row in csvreader: + res.append(row) + return (res[0], res[1:]) + def parse_country_preferences(file): countryPreferences = {} - with io.open(file, "r", encoding="utf-8") as csvfile: - csvreader = csv.reader(csvfile, delimiter=',') - headers = next(csvreader, None) - for row in csvreader: - countryPreferences[row[0]] = [headers[i] + "::" + row[i] for i in range(1, len(row))] + header, records = parse_csv_with_header(file) + for record in records: + countryPreferences[record[0]] = [header[i] + "::" + record[i] for i in range(1, len(record))] return countryPreferences countryPreferences = parse_country_preferences(args.countrypreferences) +def parse_language_preferences(file): + languagePreferences = {} + header, records = parse_csv_with_header(file) + for record in records: + languagePreferences[record[0]] = (header[1], record[1]) + return languagePreferences + +languagePreferences = parse_language_preferences(args.languagepreferences) + def print_block_from_list(target, header, data, beautify=lambda arg: arg, prefix=" ", footer="};\n\n"): target.write(header) for i in range(len(data)): @@ -179,6 +195,7 @@ def print_header(data, path, locales, countries): "enum class Country : uint8_t {\n", countries, lambda arg: arg.upper()) + defaultCountry = countries[-1] # Country names print_block_from_list(f, @@ -187,10 +204,19 @@ def print_header(data, path, locales, countries): lambda arg: arg.upper(), " Message::Country") + # Language preferences + f.write("constexpr static Country DefaultCountryForLanguage[NumberOfLanguages] = {\n") + for language in locales: + key = language if (language in languagePreferences) else '??' + header, country = languagePreferences[key] + line = " " + header + "::" + (country if country in countries else defaultCountry) + f.write(line + ",\n") + f.write("};\n\n") + # Country preferences f.write("constexpr static CountryPreferences CountryPreferencesArray[] = {\n") for country in countries: - key = country if (country in countryPreferences) else 'inl' + key = country if (country in countryPreferences) else defaultCountry line = " CountryPreferences(" for param in countryPreferences[key]: line += param + ", " diff --git a/apps/language_preferences.csv b/apps/language_preferences.csv new file mode 100644 index 000000000..867820f71 --- /dev/null +++ b/apps/language_preferences.csv @@ -0,0 +1,9 @@ +Language,I18n::Country +en,US +fr,FR +nl,NL +pt,PT +it,IT +de,DE +es,ES +??,WW diff --git a/apps/on_boarding/localization_controller.cpp b/apps/on_boarding/localization_controller.cpp index bed17051f..059d03a28 100644 --- a/apps/on_boarding/localization_controller.cpp +++ b/apps/on_boarding/localization_controller.cpp @@ -1,9 +1,16 @@ #include "localization_controller.h" #include #include +#include namespace OnBoarding { +int LocalizationController::indexOfCellToSelectOnReset() const { + return mode() == Mode::Language ? + 0 : + IndexOfCountry(I18n::DefaultCountryForLanguage[static_cast(GlobalPreferences::sharedGlobalPreferences()->language())]); +} + bool LocalizationController::handleEvent(Ion::Events::Event event) { if (Shared::LocalizationController::handleEvent(event)) { if (mode() == Mode::Language) { diff --git a/apps/on_boarding/localization_controller.h b/apps/on_boarding/localization_controller.h index 3f47a6fb0..03e0c131f 100644 --- a/apps/on_boarding/localization_controller.h +++ b/apps/on_boarding/localization_controller.h @@ -10,8 +10,8 @@ class LocalizationController : public Shared::LocalizationController { public: using Shared::LocalizationController::LocalizationController; - bool shouldDisplayTitle() override { return mode() == Mode::Country; } - bool shouldResetSelectionToTopCell() override { return true; } + int indexOfCellToSelectOnReset() const override; + bool shouldDisplayTitle() const override { return mode() == Mode::Country; } bool handleEvent(Ion::Events::Event event) override; }; diff --git a/apps/settings/sub_menu/localization_controller.cpp b/apps/settings/sub_menu/localization_controller.cpp index b77981c8e..5088289db 100644 --- a/apps/settings/sub_menu/localization_controller.cpp +++ b/apps/settings/sub_menu/localization_controller.cpp @@ -1,7 +1,15 @@ #include "localization_controller.h" +#include + namespace Settings { +int LocalizationController::indexOfCellToSelectOnReset() const { + return mode() == Mode::Language ? + static_cast(GlobalPreferences::sharedGlobalPreferences()->language()) : + IndexOfCountry(GlobalPreferences::sharedGlobalPreferences()->country()); +} + bool LocalizationController::handleEvent(Ion::Events::Event event) { if (Shared::LocalizationController::handleEvent(event) || event == Ion::Events::Left) { static_cast(parentResponder())->pop(); diff --git a/apps/settings/sub_menu/localization_controller.h b/apps/settings/sub_menu/localization_controller.h index 7fee93862..531014cf4 100644 --- a/apps/settings/sub_menu/localization_controller.h +++ b/apps/settings/sub_menu/localization_controller.h @@ -10,8 +10,8 @@ class LocalizationController : public Shared::LocalizationController { public: using Shared::LocalizationController::LocalizationController; - bool shouldDisplayTitle() override { return false; } - bool shouldResetSelectionToTopCell() override { return false; } + int indexOfCellToSelectOnReset() const override; + bool shouldDisplayTitle() const override { return false; } bool handleEvent(Ion::Events::Event event) override; TELEMETRY_ID("Localization"); diff --git a/apps/shared/localization_controller.cpp b/apps/shared/localization_controller.cpp index ba433b95f..f08c86b7a 100644 --- a/apps/shared/localization_controller.cpp +++ b/apps/shared/localization_controller.cpp @@ -127,7 +127,7 @@ LocalizationController::LocalizationController(Responder * parentResponder, KDCo void LocalizationController::resetSelection() { selectableTableView()->deselectTable(); - selectCellAtLocation(0, (shouldResetSelectionToTopCell()) ? 0 : (mode() == Mode::Language) ? static_cast(GlobalPreferences::sharedGlobalPreferences()->language()) : IndexOfCountry(GlobalPreferences::sharedGlobalPreferences()->country())); + selectCellAtLocation(0, indexOfCellToSelectOnReset()); } void LocalizationController::setMode(LocalizationController::Mode mode) { diff --git a/apps/shared/localization_controller.h b/apps/shared/localization_controller.h index 224ca7c37..82cee3689 100644 --- a/apps/shared/localization_controller.h +++ b/apps/shared/localization_controller.h @@ -22,9 +22,9 @@ public: Mode mode() const { return m_mode; } void setMode(Mode mode); - virtual bool shouldDisplayTitle() = 0; - virtual bool shouldResetSelectionToTopCell() = 0; - bool shouldDisplayWarning() { return mode() == Mode::Country; } + virtual int indexOfCellToSelectOnReset() const = 0; + virtual bool shouldDisplayTitle() const = 0; + bool shouldDisplayWarning() const { return mode() == Mode::Country; } View * view() override { return &m_contentView; } const char * title() override;