2015-11-14 04:42:01 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'i18n/backend/discourse_i18n'
|
2015-11-14 05:34:13 +08:00
|
|
|
require 'translation_override'
|
2015-11-14 04:42:01 +08:00
|
|
|
|
|
|
|
describe I18n::Backend::DiscourseI18n do
|
|
|
|
|
|
|
|
let(:backend) { I18n::Backend::DiscourseI18n.new }
|
|
|
|
|
|
|
|
before do
|
2015-11-20 05:36:59 +08:00
|
|
|
I18n.reload!
|
|
|
|
backend.store_translations(:en, :foo => 'Foo in :en', :bar => 'Bar in :en', :wat => "Hello %{count}")
|
2015-11-14 04:42:01 +08:00
|
|
|
backend.store_translations(:en, :items => {:one => 'one item', :other => "%{count} items" })
|
|
|
|
backend.store_translations(:de, :bar => 'Bar in :de')
|
|
|
|
backend.store_translations(:'de-AT', :baz => 'Baz in :de-AT')
|
|
|
|
end
|
|
|
|
|
2015-11-20 05:36:59 +08:00
|
|
|
after do
|
|
|
|
I18n.reload!
|
|
|
|
end
|
|
|
|
|
2015-11-14 04:42:01 +08:00
|
|
|
it 'translates the basics as expected' do
|
|
|
|
expect(backend.translate(:en, 'foo')).to eq("Foo in :en")
|
|
|
|
expect(backend.translate(:en, 'items', count: 1)).to eq("one item")
|
|
|
|
expect(backend.translate(:en, 'items', count: 3)).to eq("3 items")
|
2015-11-20 05:36:59 +08:00
|
|
|
expect(backend.translate(:en, 'wat', count: 3)).to eq("Hello 3")
|
2015-11-14 04:42:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#exists?' do
|
|
|
|
it 'returns true when a key is given that exists' do
|
|
|
|
expect(backend.exists?(:de, :bar)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when a key is given that exists in a fallback locale of the locale' do
|
|
|
|
expect(backend.exists?(:de, :foo)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when an existing key and an existing locale is given' do
|
|
|
|
expect(backend.exists?(:en, :foo)).to eq(true)
|
|
|
|
expect(backend.exists?(:de, :bar)).to eq(true)
|
|
|
|
expect(backend.exists?(:'de-AT', :baz)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when a non-existing key and an existing locale is given' do
|
|
|
|
expect(backend.exists?(:en, :bogus)).to eq(false)
|
|
|
|
expect(backend.exists?(:de, :bogus)).to eq(false)
|
|
|
|
expect(backend.exists?(:'de-AT', :bogus)).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when a key is given which is missing from the given locale and exists in a fallback locale' do
|
|
|
|
expect(backend.exists?(:de, :foo)).to eq(true)
|
|
|
|
expect(backend.exists?(:'de-AT', :foo)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when a key is given which is missing from the given locale and all its fallback locales' do
|
|
|
|
expect(backend.exists?(:de, :baz)).to eq(false)
|
|
|
|
expect(backend.exists?(:'de-AT', :bogus)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-14 05:34:13 +08:00
|
|
|
describe 'with overrides' do
|
2015-11-20 05:36:59 +08:00
|
|
|
it 'returns the overriden key' do
|
2015-11-14 05:34:13 +08:00
|
|
|
TranslationOverride.upsert!('en', 'foo', 'Overwritten foo')
|
2015-11-20 05:36:59 +08:00
|
|
|
expect(I18n.translate('foo')).to eq('Overwritten foo')
|
|
|
|
|
|
|
|
TranslationOverride.upsert!('en', 'foo', 'new value')
|
|
|
|
I18n.reload!
|
|
|
|
expect(I18n.translate('foo')).to eq('new value')
|
2015-11-14 05:34:13 +08:00
|
|
|
end
|
|
|
|
|
2015-11-20 05:36:59 +08:00
|
|
|
it 'supports disabling' do
|
|
|
|
TranslationOverride.upsert!('en', 'foo', 'meep')
|
2015-11-14 05:34:13 +08:00
|
|
|
|
2015-11-20 05:36:59 +08:00
|
|
|
I18n.overrides_disabled do
|
|
|
|
expect(I18n.translate('foo')).to eq('meep')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports interpolation' do
|
|
|
|
TranslationOverride.upsert!('en', 'foo', 'hello %{world}')
|
|
|
|
expect(I18n.translate('foo', world: 'foo')).to eq('hello foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports interpolation named count' do
|
|
|
|
TranslationOverride.upsert!('en', 'wat', 'goodbye %{count}')
|
|
|
|
expect(I18n.translate('wat', count: 123)).to eq('goodbye 123')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports one and other' do
|
|
|
|
TranslationOverride.upsert!('en', 'items.one', 'one fish')
|
|
|
|
TranslationOverride.upsert!('en', 'items.other', '%{count} fishies')
|
|
|
|
expect(I18n.translate('items', count: 13)).to eq('13 fishies')
|
|
|
|
expect(I18n.translate('items', count: 1)).to eq('one fish')
|
2015-11-14 05:34:13 +08:00
|
|
|
end
|
2015-11-21 06:13:37 +08:00
|
|
|
|
|
|
|
describe "client json" do
|
|
|
|
it "is empty by default" do
|
|
|
|
expect(I18n.client_overrides_json).to eq("{}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return server overrides" do
|
|
|
|
TranslationOverride.upsert!('en', 'foo', 'bar')
|
|
|
|
expect(I18n.client_overrides_json).to eq("{}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns client overrides" do
|
|
|
|
TranslationOverride.upsert!('en', 'js.foo', 'bar')
|
|
|
|
json = ::JSON.parse(I18n.client_overrides_json)
|
|
|
|
|
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['js.foo']).to eq('bar')
|
|
|
|
end
|
|
|
|
end
|
2015-11-14 05:34:13 +08:00
|
|
|
end
|
|
|
|
|
2015-11-14 04:42:01 +08:00
|
|
|
end
|