Skip to content

Commit

Permalink
Merge pull request #1863 from 18F/jmhooper-english-fallback-translations
Browse files Browse the repository at this point in the history
Fallback to english for untranslated text
  • Loading branch information
jmhooper authored Dec 12, 2017
2 parents 25b54a8 + 94022f1 commit 668b8fc
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
21 changes: 21 additions & 0 deletions config/initializers/i18n_english_fallback.rb
Original file line number Diff line number Diff line change
@@ -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
73 changes: 73 additions & 0 deletions spec/config/initializers/i18n_english_fallback.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 668b8fc

Please sign in to comment.