2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-13 15:42:01 -05:00
|
|
|
require 'i18n/backend/discourse_i18n'
|
2015-11-13 16:34:13 -05:00
|
|
|
require 'translation_override'
|
2015-11-13 15:42:01 -05:00
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe I18n::Backend::DiscourseI18n do
|
2015-11-13 15:42:01 -05:00
|
|
|
|
|
|
|
let(:backend) { I18n::Backend::DiscourseI18n.new }
|
|
|
|
|
|
|
|
before do
|
2018-09-18 06:59:00 -04:00
|
|
|
backend.reload!
|
|
|
|
backend.store_translations(:en, foo: 'Foo in :en', bar: 'Bar in :en', wat: 'Hello %{count}')
|
|
|
|
backend.store_translations(:en, items: { one: 'one item', other: '%{count} items' })
|
2015-11-13 15:42:01 -05:00
|
|
|
backend.store_translations(:de, bar: 'Bar in :de')
|
2018-08-30 17:32:24 -04:00
|
|
|
backend.store_translations(:en, link: '[text](url)')
|
2015-11-13 15:42:01 -05:00
|
|
|
end
|
|
|
|
|
2015-11-19 16:36:59 -05:00
|
|
|
after do
|
2018-09-18 06:59:00 -04:00
|
|
|
backend.reload!
|
2015-11-19 16:36:59 -05:00
|
|
|
end
|
|
|
|
|
2015-11-13 15:42:01 -05:00
|
|
|
it 'translates the basics as expected' do
|
2018-09-18 06:59:00 -04:00
|
|
|
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')
|
|
|
|
expect(backend.translate(:en, 'wat', count: 3)).to eq('Hello 3')
|
2015-11-13 15:42:01 -05:00
|
|
|
end
|
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
it 'can be searched by key or value' do
|
|
|
|
expect(backend.search(:en, 'fo')).to eq('foo' => 'Foo in :en')
|
|
|
|
expect(backend.search(:en, 'foo')).to eq('foo' => 'Foo in :en')
|
|
|
|
expect(backend.search(:en, 'Foo')).to eq('foo' => 'Foo in :en')
|
|
|
|
expect(backend.search(:en, 'hello')).to eq('wat' => 'Hello %{count}')
|
|
|
|
expect(backend.search(:en, 'items.one')).to eq('items.one' => 'one item')
|
2018-08-30 17:32:24 -04:00
|
|
|
expect(backend.search(:en, '](')).to eq('link' => '[text](url)')
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can return multiple results' do
|
|
|
|
results = backend.search(:en, 'item')
|
|
|
|
|
|
|
|
expect(results['items.one']).to eq('one item')
|
|
|
|
expect(results['items.other']).to eq('%{count} items')
|
|
|
|
end
|
|
|
|
|
2018-01-22 08:42:12 -05:00
|
|
|
describe 'fallbacks' do
|
|
|
|
it 'uses fallback locales for translating' do
|
|
|
|
expect(backend.translate(:de, 'bar')).to eq('Bar in :de')
|
|
|
|
expect(backend.translate(:de, 'foo')).to eq('Foo in :en')
|
|
|
|
end
|
2016-02-13 17:01:05 -05:00
|
|
|
end
|
|
|
|
|
2015-11-13 15:42:01 -05:00
|
|
|
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)
|
|
|
|
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)
|
2018-01-22 08:42:12 -05:00
|
|
|
expect(backend.exists?(:ru, :bogus)).to eq(false)
|
2015-11-13 15:42:01 -05:00
|
|
|
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)
|
2018-01-22 08:42:12 -05:00
|
|
|
expect(backend.exists?(:ru, :foo)).to eq(true)
|
2015-11-13 15:42:01 -05:00
|
|
|
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)
|
2018-01-22 08:42:12 -05:00
|
|
|
expect(backend.exists?(:ru, :bogus)).to eq(false)
|
2015-11-13 15:42:01 -05:00
|
|
|
end
|
|
|
|
end
|
2019-05-13 16:07:42 -04:00
|
|
|
|
|
|
|
describe '#pluralize' do
|
|
|
|
it 'uses fallback locales when a pluralization key is missing' do
|
|
|
|
SiteSetting.default_locale = 'ru'
|
|
|
|
|
|
|
|
backend.store_translations(:ru, items: { one: '%{count} Russian item', other: '%{count} Russian items' })
|
|
|
|
|
|
|
|
expect(backend.translate(:ru, :items, count: 1)).to eq('1 Russian item')
|
|
|
|
expect(backend.translate(:ru, :items, count: 2)).to eq('2 items')
|
|
|
|
expect(backend.translate(:ru, :items, count: 5)).to eq('5 Russian items')
|
|
|
|
|
|
|
|
backend.store_translations(:ru, items: { one: '%{count} Russian item', few: '%{count} Russian items are a few', other: '%{count} Russian items' })
|
|
|
|
expect(backend.translate(:ru, :items, count: 2)).to eq('2 Russian items are a few')
|
|
|
|
|
|
|
|
backend.store_translations(:en, airplanes: { one: '%{count} airplane' })
|
|
|
|
expect(backend.translate(:ru, :airplanes, count: 1)).to eq('1 airplane')
|
|
|
|
expect { backend.translate(:ru, :airplanes, count: 2) }.to raise_error(I18n::InvalidPluralizationData)
|
|
|
|
end
|
|
|
|
end
|
2020-11-11 10:44:01 -05:00
|
|
|
|
|
|
|
describe ".sort_local_files" do
|
|
|
|
it "sorts an array of client ymls with '-(highest-number)' being last" do
|
|
|
|
expect(I18n::Backend::DiscourseI18n.sort_locale_files(
|
|
|
|
[
|
|
|
|
'discourse/plugins/discourse-second/config/locales/client-99.es.yml',
|
|
|
|
'discourse/plugins/discourse-first/config/locales/client.es.yml',
|
|
|
|
'discourse/plugins/discourse-third/config/locales/client-2.es.yml',
|
|
|
|
'discourse/plugins/discourse-third/config/locales/client-3.bs_BA.yml',
|
|
|
|
]
|
|
|
|
)).to eq(
|
|
|
|
[
|
|
|
|
'discourse/plugins/discourse-first/config/locales/client.es.yml',
|
|
|
|
'discourse/plugins/discourse-third/config/locales/client-2.es.yml',
|
|
|
|
'discourse/plugins/discourse-third/config/locales/client-3.bs_BA.yml',
|
|
|
|
'discourse/plugins/discourse-second/config/locales/client-99.es.yml',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2015-11-13 15:42:01 -05:00
|
|
|
end
|