Fix language sorting in settings (#27158)

This commit is contained in:
gunchleoc 2023-10-04 08:23:50 +01:00 committed by GitHub
parent c676bc91e9
commit 68ec1d180a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 2 deletions

View File

@ -230,6 +230,24 @@ module LanguagesHelper
'sr-Latn': 'Srpski (latinica)',
}.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 = REGIONAL_LOCALE_NAMES[locale.to_sym])
ASCIIFolding.new.fold(regional_locale).downcase
else
locale
end
end
# Sort locales by native name for dropdown menus
def self.sorted_locale_keys(locale_keys)
locale_keys.sort_by { |key, _| locale_name_for_sorting(key) }
end
def native_locale_name(locale)
if locale.blank? || locale == 'und'
I18n.t('generic.none')

View File

@ -2,7 +2,11 @@
module SettingsHelper
def filterable_languages
LanguagesHelper::SUPPORTED_LOCALES.keys
LanguagesHelper.sorted_locale_keys(LanguagesHelper::SUPPORTED_LOCALES.keys)
end
def ui_languages
LanguagesHelper.sorted_locale_keys(I18n.available_locales)
end
def session_device_icon(session)

View File

@ -7,7 +7,7 @@
= simple_form_for current_user, url: settings_preferences_appearance_path, html: { method: :put, id: 'edit_user' } do |f|
.fields-row
.fields-group.fields-row__column.fields-row__column-6
= f.input :locale, collection: I18n.available_locales, wrapper: :with_label, include_blank: false, label_method: ->(locale) { native_locale_name(locale) }, selected: I18n.locale, hint: false
= f.input :locale, collection: ui_languages, wrapper: :with_label, include_blank: false, label_method: ->(locale) { native_locale_name(locale) }, selected: I18n.locale, hint: false
.fields-group.fields-row__column.fields-row__column-6
= f.input :time_zone, wrapper: :with_label, collection: ActiveSupport::TimeZone.all.map { |tz| ["(GMT#{tz.formatted_offset}) #{tz.name}", tz.tzinfo.name] }, hint: false

View File

@ -60,4 +60,30 @@ describe LanguagesHelper do
end
end
end
describe 'sorted_locales' do
context 'when sorting with native name' do
it 'returns Suomi after Gàidhlig' do
expect(described_class.sorted_locale_keys(%w(fi gd))).to eq(%w(gd fi))
end
end
context 'when sorting with diacritics' do
it 'returns Íslensk before Suomi' do
expect(described_class.sorted_locale_keys(%w(fi is))).to eq(%w(is fi))
end
end
context 'when sorting with non-Latin' do
it 'returns Suomi before Amharic' do
expect(described_class.sorted_locale_keys(%w(am fi))).to eq(%w(fi am))
end
end
context 'when sorting with local variants' do
it 'returns variant in-line' do
expect(described_class.sorted_locale_keys(%w(en eo en-GB))).to eq(%w(en en-GB eo))
end
end
end
end