From 94022f17b9504b102a58bb2addae173e3d6852bc Mon Sep 17 00:00:00 2001 From: Jonathan Hooper Date: Fri, 8 Dec 2017 08:45:18 -0600 Subject: [PATCH] Fallback to english for untranslated text **Why**: So that users using the site in other languages will not see NOT TRANSLATED AT. **How**: Patch `I18n.translate` and `I18n.t` such that when it is about to return "NOT TRANSLATED AT", it instead calls back to the I18n backend for the same translation in english. --- config/initializers/i18n_english_fallback.rb | 21 ++++++ .../initializers/i18n_english_fallback.rb | 73 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 config/initializers/i18n_english_fallback.rb create mode 100644 spec/config/initializers/i18n_english_fallback.rb diff --git a/config/initializers/i18n_english_fallback.rb b/config/initializers/i18n_english_fallback.rb new file mode 100644 index 00000000000..227dd5e7724 --- /dev/null +++ b/config/initializers/i18n_english_fallback.rb @@ -0,0 +1,21 @@ +I18n.module_eval do + class << self + def translate(*args) + original_translation = super(*args) + return original_translation unless original_translation == 'NOT TRANSLATED YET' + fallback_to_english(*args) + end + + alias_method :t, :translate + + private + + def fallback_to_english(*args) + key = args.shift + options = args.last.is_a?(Hash) ? args.last : {} + options.delete(:locale) + + config.backend.translate(:en, key, options) + end + end +end diff --git a/spec/config/initializers/i18n_english_fallback.rb b/spec/config/initializers/i18n_english_fallback.rb new file mode 100644 index 00000000000..689d01dd3e9 --- /dev/null +++ b/spec/config/initializers/i18n_english_fallback.rb @@ -0,0 +1,73 @@ +require 'rails_helper' + +describe I18n do + let(:translation_key) { 'asdf.qwert.1234' } + let(:english_translation) { 'this is some text' } + let(:spanish_translation) { 'NOT TRANSLATED YET' } + let(:local_argument) { :en } + + before do + allow(I18n.config.backend).to receive(:translate). + with(:es, translation_key, {}). + and_return(spanish_translation) + allow(I18n.config.backend).to receive(:translate). + with(:en, translation_key, {}). + and_return(english_translation) + end + + after do + I18n.locale = :en + end + + describe '#translate' do + context 'with non-english locale' do + context 'when the requested string is untranslated' do + it 'should return the english translation with a locale argument' do + expect(I18n.t('asdf.qwert.1234', locale: :es)).to eq(english_translation) + end + + it 'should return the english translation with a global locale' do + I18n.locale = :es + + expect(I18n.t('asdf.qwert.1234')).to eq(english_translation) + end + end + + context 'when the requested string is translated' do + let(:spanish_translation) { 'esto es un texto' } + + it 'should return the non-english translation with a locale argument' do + expect(I18n.t('asdf.qwert.1234', locale: :es)).to eq(spanish_translation) + end + + it 'should return the non-english translation with a global locale' do + I18n.locale = :es + + expect(I18n.t('asdf.qwert.1234')).to eq(spanish_translation) + end + end + end + + context 'with english locale' do + it 'should return the english translation with a locale argument' do + expect(I18n.t('asdf.qwert.1234', locale: :en)).to eq(english_translation) + end + + it 'should return the english translation with a global locale' do + expect(I18n.t('asdf.qwert.1234')).to eq(english_translation) + end + end + + context 'when english translation is "NOT TRANSLATED YET"' do + let(:english_translation) { 'NOT TRANSLATED YET' } + + it 'does not recurse with a locale argument' do + expect(I18n.t('asdf.qwert.1234', locale: :en)).to eq('NOT TRANSLATED YET') + end + + it 'does not recurse without a locale argument' do + expect(I18n.t('asdf.qwert.1234')).to eq('NOT TRANSLATED YET') + end + end + end +end