Unify array structure for locales

This commit is contained in:
GunChleoc 2024-03-10 12:22:24 +00:00
parent 1df1e23644
commit a1414f0041
4 changed files with 18 additions and 21 deletions

View File

@ -221,23 +221,23 @@ module LanguagesHelper
# but for presenting posting and filtering languages, we drop the country code and # but for presenting posting and filtering languages, we drop the country code and
# fall back to the language code, thus skipping these locales there. # fall back to the language code, thus skipping these locales there.
UI_ONLY_REGIONAL_LOCALES = { UI_ONLY_REGIONAL_LOCALES = {
'en-GB': 'English (British)', 'en-GB': ['English (British)', 'English (British)'].freeze,
'es-AR': 'Español (Argentina)', 'es-AR': ['Spanish (Argentina)', 'Español (Argentina)'].freeze,
'es-MX': 'Español (México)', 'es-MX': ['Spanish (Mexico)', 'Español (México)'].freeze,
'fr-CA': 'Français (Canadien)', 'fr-CA': ['French (Canadian)', 'Français (Canadien)'].freeze,
'pt-BR': 'Português (Brasil)', 'pt-BR': ['Portuguese (Brasil)', 'Português (Brasil)'].freeze,
'pt-PT': 'Português (Portugal)', 'pt-PT': ['Portuguese (Portugal)', 'Português (Portugal)'].freeze,
'sr-Latn': 'Srpski (latinica)', 'sr-Latn': ['Serbian (Latin)', 'Srpski (latinica)'].freeze,
}.freeze }.freeze
KNOWN_LOCALES = {}.merge(SUPPORTED_LOCALES).merge(UI_ONLY_REGIONAL_LOCALES).freeze
# Helper for self.sorted_locale_keys # Helper for self.sorted_locale_keys
private_class_method def self.locale_name_for_sorting(locale) private_class_method def self.locale_name_for_sorting(locale)
if locale.blank? || locale == 'und' if locale.blank? || locale == 'und'
'000' '000'
elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym]) elsif (known_locale = KNOWN_LOCALES[locale.to_sym])
ASCIIFolding.new.fold(supported_locale[1]).downcase ASCIIFolding.new.fold(known_locale[1]).downcase
elsif (regional_locale = UI_ONLY_REGIONAL_LOCALES[locale.to_sym])
ASCIIFolding.new.fold(regional_locale).downcase
else else
locale locale
end end
@ -251,10 +251,8 @@ module LanguagesHelper
def native_locale_name(locale) def native_locale_name(locale)
if locale.blank? || locale == 'und' if locale.blank? || locale == 'und'
I18n.t('generic.none') I18n.t('generic.none')
elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym]) elsif (known_locale = KNOWN_LOCALES[locale.to_sym])
supported_locale[1] known_locale[1]
elsif (regional_locale = UI_ONLY_REGIONAL_LOCALES[locale.to_sym])
regional_locale
else else
locale locale
end end
@ -263,8 +261,8 @@ module LanguagesHelper
def standard_locale_name(locale) def standard_locale_name(locale)
if locale.blank? if locale.blank?
I18n.t('generic.none') I18n.t('generic.none')
elsif (supported_locale = SUPPORTED_LOCALES[locale.to_sym]) elsif (known_locale = KNOWN_LOCALES[locale.to_sym])
supported_locale[0] known_locale[0]
else else
locale locale
end end

View File

@ -14,8 +14,7 @@ module ActivityPub::CaseTransform
when String when String
camel_lower_cache[value] ||= if value.start_with?('_:') camel_lower_cache[value] ||= if value.start_with?('_:')
"_:#{value.delete_prefix('_:').underscore.camelize(:lower)}" "_:#{value.delete_prefix('_:').underscore.camelize(:lower)}"
elsif LanguagesHelper::SUPPORTED_LOCALES.key?(value.to_sym) || elsif LanguagesHelper::KNOWN_LOCALES.key?(value.to_sym)
LanguagesHelper::UI_ONLY_REGIONAL_LOCALES.key?(value.to_sym)
value value
else else
value.underscore.camelize(:lower) value.underscore.camelize(:lower)

View File

@ -96,7 +96,7 @@ namespace :repo do
end.uniq.compact end.uniq.compact
missing_available_locales = locales_in_files - I18n.available_locales 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) } missing_locale_names = I18n.available_locales.reject { |locale| supported_locale_codes.include?(locale) }
critical = false critical = false

View File

@ -5,7 +5,7 @@ require 'rails_helper'
describe LanguagesHelper do describe LanguagesHelper do
describe 'the SUPPORTED_LOCALES constant' do describe 'the SUPPORTED_LOCALES constant' do
it 'includes all i18n locales' 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
end end