-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |