From e1cf3a33623dbd9e6156ded3aea5872e0a0d8716 Mon Sep 17 00:00:00 2001 From: Brandon Medenwald Date: Fri, 2 Oct 2020 07:45:41 -0500 Subject: [PATCH] Handle uppercase and lowercase for rails-i18n integrations --- lib/i18n/tasks/missing_keys.rb | 35 +++++++++++++--- spec/missing_keys_spec.rb | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 spec/missing_keys_spec.rb diff --git a/lib/i18n/tasks/missing_keys.rb b/lib/i18n/tasks/missing_keys.rb index 07944034..eab1e33e 100644 --- a/lib/i18n/tasks/missing_keys.rb +++ b/lib/i18n/tasks/missing_keys.rb @@ -79,12 +79,8 @@ def missing_plural_forest(locales, _base = base_locale) def required_plural_keys_for_locale(locale) @plural_keys_for_locale ||= {} return @plural_keys_for_locale[locale] if @plural_keys_for_locale.key?(locale) - @plural_keys_for_locale[locale] = - begin - Set.new(load_rails_i18n_pluralization!(locale)[locale.to_sym][:i18n][:plural][:keys]) - rescue SystemCallError, IOError - Set.new - end + + @plural_keys_for_locale[locale] = plural_keys_for_locale(locale) end # Loads rails-i18n pluralization config for the given locale. @@ -154,5 +150,32 @@ def collapse_same_key_in_locales!(forest) end forest end + + private + + def plural_keys_for_locale(locale) + configuration = load_rails_i18n_pluralization!(locale) + if configuration[locale.to_sym].nil? + alternate_locale = alternate_locale_from(locale) + return Set.new if configuration[alternate_locale.to_sym].nil? + + return set_from_rails_i18n_pluralization(configuration, alternate_locale) + end + set_from_rails_i18n_pluralization(configuration, locale) + rescue SystemCallError, IOError + Set.new + end + + def alternate_locale_from(locale) + re = /(\w{2})-*(\w{2,3})*/ + match = locale.match(re) + language_code = match[1] + country_code = match[2] + "#{language_code}-#{country_code.upcase}" + end + + def set_from_rails_i18n_pluralization(configuration, locale) + Set.new(configuration[locale.to_sym][:i18n][:plural][:keys]) + end end end diff --git a/spec/missing_keys_spec.rb b/spec/missing_keys_spec.rb new file mode 100644 index 00000000..caa658c2 --- /dev/null +++ b/spec/missing_keys_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'MissingKeys' do + describe '#required_plural_keys_for_locale(locale)' do + let(:task) { ::I18n::Tasks::BaseTask.new } + + def configuration_from(locale) + { + "#{locale}": { + i18n: { + plural: { + keys: %i[one other], + rule: -> {} + } + } + } + } + end + + context 'when country code is lowercase' do + let(:locale) { 'en-gb' } + let(:configuration) { configuration_from(locale) } + + before do + allow(task).to receive(:load_rails_i18n_pluralization!).with(locale).and_return(configuration) + end + + it 'accesses the capitalized country code key and returns a populated set' do + expect(task.required_plural_keys_for_locale(locale)).not_to be_empty + end + end + + context 'when country code is uppercase' do + let(:locale) { 'en-GB' } + let(:configuration) { configuration_from(locale) } + + before do + allow(task).to receive(:load_rails_i18n_pluralization!).with(locale.downcase).and_return(configuration) + end + + it 'accesses the capitalized country code key and returns a populated set' do + expect(task.required_plural_keys_for_locale(locale.downcase)).not_to be_empty + end + end + + context 'when country code consists of three letters' do + let(:locale) { 'zh-YUE' } + let(:configuration) { configuration_from(locale) } + + before do + allow(task).to receive(:load_rails_i18n_pluralization!).with(locale.downcase).and_return(configuration) + end + + it 'accesses the country code key and returns a populated set' do + expect(task.required_plural_keys_for_locale(locale.downcase)).not_to be_empty + end + end + + context 'when locale is not present in configuration hash' do + let(:locale) { 'zz-zz' } + let(:configuration) { configuration_from('en-us') } + + before do + allow(task).to receive(:load_rails_i18n_pluralization!).with(locale).and_return(configuration) + end + + it 'returns an empty set' do + expect(task.required_plural_keys_for_locale(locale)).to be_empty + end + end + end +end