From a1414f00416f6000c291786b013be8c723c78569 Mon Sep 17 00:00:00 2001 From: GunChleoc Date: Sun, 10 Mar 2024 12:22:24 +0000 Subject: [PATCH] Unify array structure for locales --- app/helpers/languages_helper.rb | 32 +++++++++++++-------------- app/lib/activitypub/case_transform.rb | 3 +-- lib/tasks/repo.rake | 2 +- spec/helpers/languages_helper_spec.rb | 2 +- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/app/helpers/languages_helper.rb b/app/helpers/languages_helper.rb index 4427fee8d17..2a7677033ce 100644 --- a/app/helpers/languages_helper.rb +++ b/app/helpers/languages_helper.rb @@ -221,23 +221,23 @@ module LanguagesHelper # but for presenting posting and filtering languages, we drop the country code and # fall back to the language code, thus skipping these locales there. UI_ONLY_REGIONAL_LOCALES = { - 'en-GB': 'English (British)', - 'es-AR': 'Español (Argentina)', - 'es-MX': 'Español (México)', - 'fr-CA': 'Français (Canadien)', - 'pt-BR': 'Português (Brasil)', - 'pt-PT': 'Português (Portugal)', - 'sr-Latn': 'Srpski (latinica)', + 'en-GB': ['English (British)', 'English (British)'].freeze, + 'es-AR': ['Spanish (Argentina)', 'Español (Argentina)'].freeze, + 'es-MX': ['Spanish (Mexico)', 'Español (México)'].freeze, + 'fr-CA': ['French (Canadian)', 'Français (Canadien)'].freeze, + 'pt-BR': ['Portuguese (Brasil)', 'Português (Brasil)'].freeze, + 'pt-PT': ['Portuguese (Portugal)', 'Português (Portugal)'].freeze, + 'sr-Latn': ['Serbian (Latin)', 'Srpski (latinica)'].freeze, }.freeze + KNOWN_LOCALES = {}.merge(SUPPORTED_LOCALES).merge(UI_ONLY_REGIONAL_LOCALES).freeze + # Helper for self.sorted_locale_keys private_class_method def self.locale_name_for_sorting(locale) if locale.blank? || locale == 'und' '000' - elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym]) - ASCIIFolding.new.fold(supported_locale[1]).downcase - elsif (regional_locale = UI_ONLY_REGIONAL_LOCALES[locale.to_sym]) - ASCIIFolding.new.fold(regional_locale).downcase + elsif (known_locale = KNOWN_LOCALES[locale.to_sym]) + ASCIIFolding.new.fold(known_locale[1]).downcase else locale end @@ -251,10 +251,8 @@ module LanguagesHelper def native_locale_name(locale) if locale.blank? || locale == 'und' I18n.t('generic.none') - elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym]) - supported_locale[1] - elsif (regional_locale = UI_ONLY_REGIONAL_LOCALES[locale.to_sym]) - regional_locale + elsif (known_locale = KNOWN_LOCALES[locale.to_sym]) + known_locale[1] else locale end @@ -263,8 +261,8 @@ module LanguagesHelper def standard_locale_name(locale) if locale.blank? I18n.t('generic.none') - elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym]) - supported_locale[0] + elsif (known_locale = KNOWN_LOCALES[locale.to_sym]) + known_locale[0] else locale end diff --git a/app/lib/activitypub/case_transform.rb b/app/lib/activitypub/case_transform.rb index 93b4b988879..9977647ce0f 100644 --- a/app/lib/activitypub/case_transform.rb +++ b/app/lib/activitypub/case_transform.rb @@ -14,8 +14,7 @@ module ActivityPub::CaseTransform when String camel_lower_cache[value] ||= if value.start_with?('_:') "_:#{value.delete_prefix('_:').underscore.camelize(:lower)}" - elsif LanguagesHelper::SUPPORTED_LOCALES.key?(value.to_sym) || - LanguagesHelper::UI_ONLY_REGIONAL_LOCALES.key?(value.to_sym) + elsif LanguagesHelper::KNOWN_LOCALES.key?(value.to_sym) value else value.underscore.camelize(:lower) diff --git a/lib/tasks/repo.rake b/lib/tasks/repo.rake index e5dfb570d4f..f14049ea0ea 100644 --- a/lib/tasks/repo.rake +++ b/lib/tasks/repo.rake @@ -96,7 +96,7 @@ namespace :repo do end.uniq.compact missing_available_locales = locales_in_files - I18n.available_locales - supported_locale_codes = Set.new(LanguagesHelper::SUPPORTED_LOCALES.keys + LanguagesHelper::UI_ONLY_REGIONAL_LOCALES.keys) + supported_locale_codes = Set.new(LanguagesHelper::KNOWN_LOCALES.keys) missing_locale_names = I18n.available_locales.reject { |locale| supported_locale_codes.include?(locale) } critical = false diff --git a/spec/helpers/languages_helper_spec.rb b/spec/helpers/languages_helper_spec.rb index eae54b61a46..4f1311f8199 100644 --- a/spec/helpers/languages_helper_spec.rb +++ b/spec/helpers/languages_helper_spec.rb @@ -5,7 +5,7 @@ require 'rails_helper' describe LanguagesHelper do describe 'the SUPPORTED_LOCALES constant' do it 'includes all i18n locales' do - expect(Set.new(described_class::SUPPORTED_LOCALES.keys + described_class::UI_ONLY_REGIONAL_LOCALES.keys)).to include(*I18n.available_locales) + expect(Set.new(described_class::KNOWN_LOCALES.keys)).to include(*I18n.available_locales) end end