mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: English locale with international date formats
Makes en_US the new default locale
This commit is contained in:
@@ -1,116 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require 'i18n/backend/discourse_i18n'
|
||||
require 'translation_override'
|
||||
|
||||
describe I18n::Backend::DiscourseI18n do
|
||||
|
||||
let(:backend) { I18n::Backend::DiscourseI18n.new }
|
||||
|
||||
before do
|
||||
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' })
|
||||
backend.store_translations(:de, bar: 'Bar in :de')
|
||||
backend.store_translations(:ru, baz: 'Baz in :ru')
|
||||
backend.store_translations(:en, link: '[text](url)')
|
||||
end
|
||||
|
||||
after do
|
||||
backend.reload!
|
||||
end
|
||||
|
||||
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')
|
||||
expect(backend.translate(:en, 'wat', count: 3)).to eq('Hello 3')
|
||||
end
|
||||
|
||||
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')
|
||||
expect(backend.search(:en, '](')).to eq('link' => '[text](url)')
|
||||
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
|
||||
|
||||
describe 'fallbacks' do
|
||||
it 'uses fallback locales for searching' do
|
||||
expect(backend.search(:de, 'bar')).to eq('bar' => 'Bar in :de')
|
||||
expect(backend.search(:de, 'foo')).to eq('foo' => 'Foo in :en')
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
it 'uses default_locale as fallback when key exists' do
|
||||
SiteSetting.default_locale = 'ru'
|
||||
expect(backend.translate(:de, 'bar')).to eq('Bar in :de')
|
||||
expect(backend.translate(:de, 'baz')).to eq('Baz in :ru')
|
||||
expect(backend.translate(:de, 'foo')).to eq('Foo in :en')
|
||||
end
|
||||
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?(:ru, :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?(:ru, :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?(:ru, :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?(:ru, :bogus)).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
@@ -1,50 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require 'i18n/backend/fallback_locale_list'
|
||||
|
||||
describe I18n::Backend::FallbackLocaleList do
|
||||
let(:list) { I18n::Backend::FallbackLocaleList.new }
|
||||
|
||||
it "works when default_locale is English" do
|
||||
SiteSetting.default_locale = :en
|
||||
|
||||
expect(list[:ru]).to eq([:ru, :en])
|
||||
expect(list[:en]).to eq([:en])
|
||||
end
|
||||
|
||||
it "works when default_locale is not English" do
|
||||
SiteSetting.default_locale = :de
|
||||
|
||||
expect(list[:ru]).to eq([:ru, :de, :en])
|
||||
expect(list[:de]).to eq([:de, :en])
|
||||
expect(list[:en]).to eq([:en])
|
||||
end
|
||||
|
||||
context "when plugin registered fallback locale" do
|
||||
before do
|
||||
DiscoursePluginRegistry.register_locale("es_MX", fallbackLocale: "es")
|
||||
DiscoursePluginRegistry.register_locale("de_AT", fallbackLocale: "de")
|
||||
end
|
||||
|
||||
after do
|
||||
DiscoursePluginRegistry.reset!
|
||||
end
|
||||
|
||||
it "works when default_locale is English" do
|
||||
SiteSetting.default_locale = :en
|
||||
|
||||
expect(list[:de_AT]).to eq([:de_AT, :de, :en])
|
||||
expect(list[:de]).to eq([:de, :en])
|
||||
expect(list[:en]).to eq([:en])
|
||||
end
|
||||
|
||||
it "works when default_locale is not English" do
|
||||
SiteSetting.default_locale = :de
|
||||
|
||||
expect(list[:es_MX]).to eq([:es_MX, :es, :de, :en])
|
||||
expect(list[:es]).to eq([:es, :de, :en])
|
||||
expect(list[:en]).to eq([:en])
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -89,6 +89,8 @@ describe "translate accelerator" do
|
||||
end
|
||||
|
||||
describe "with overrides" do
|
||||
before { I18n.locale = :en }
|
||||
|
||||
it "returns the overridden key" do
|
||||
override_translation('en', 'foo', 'Overwritten foo')
|
||||
expect(I18n.t('foo')).to eq('Overwritten foo')
|
||||
|
||||
@@ -20,7 +20,6 @@ describe JsLocaleHelper do
|
||||
JsLocaleHelper.extend StubLoadTranslations
|
||||
|
||||
after do
|
||||
I18n.locale = :en
|
||||
JsLocaleHelper.clear_cache!
|
||||
end
|
||||
|
||||
@@ -119,6 +118,16 @@ describe JsLocaleHelper do
|
||||
message = JsLocaleHelper.compile_message_format(message_format_filename('ru'), 'ru', 'format')
|
||||
expect(message).not_to match 'Plural Function not found'
|
||||
end
|
||||
|
||||
it "includes uses message formats from fallback locale" do
|
||||
translations = JsLocaleHelper.translations_for(:en_US)
|
||||
en_us_message_formats = JsLocaleHelper.remove_message_formats!(translations, :en_US)
|
||||
expect(en_us_message_formats).to_not be_empty
|
||||
|
||||
translations = JsLocaleHelper.translations_for(:en)
|
||||
en_message_formats = JsLocaleHelper.remove_message_formats!(translations, :en)
|
||||
expect(en_us_message_formats).to eq(en_message_formats)
|
||||
end
|
||||
end
|
||||
|
||||
it 'performs fallbacks to english if a translation is not available' do
|
||||
@@ -190,7 +199,7 @@ describe JsLocaleHelper do
|
||||
it "finds moment.js locale file for #{locale[:value]}" do
|
||||
content = JsLocaleHelper.moment_locale(locale[:value])
|
||||
|
||||
if (locale[:value] == 'en')
|
||||
if (locale[:value] == SiteSettings::DefaultsProvider::DEFAULT_LOCALE)
|
||||
expect(content).to eq('')
|
||||
else
|
||||
expect(content).to_not eq('')
|
||||
|
||||
@@ -766,7 +766,7 @@ describe SiteSettingExtension do
|
||||
|
||||
describe '.default_locale' do
|
||||
it 'is always loaded' do
|
||||
expect(settings.default_locale).to eq 'en'
|
||||
expect(settings.default_locale).to eq('en_US')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -833,7 +833,7 @@ describe SiteSettingExtension do
|
||||
settings.refresh!
|
||||
|
||||
expect(settings.client_settings_json_uncached).to eq(
|
||||
%Q|{"default_locale":"en","upload_type":"#{upload.url}","string_type":"haha"}|
|
||||
%Q|{"default_locale":"#{SiteSetting.default_locale}","upload_type":"#{upload.url}","string_type":"haha"}|
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -196,7 +196,11 @@ describe TextCleaner do
|
||||
end
|
||||
|
||||
it "removes extraneous space before the end punctuation" do
|
||||
SiteSetting.title_remove_extraneous_space = true
|
||||
expect(TextCleaner.clean_title("Hello there ?")).to eq("Hello there?")
|
||||
|
||||
SiteSetting.title_remove_extraneous_space = false
|
||||
expect(TextCleaner.clean_title("Hello there ?")).to eq("Hello there ?")
|
||||
end
|
||||
|
||||
it "replaces all upper case unicode text with regular unicode case letters" do
|
||||
|
||||
@@ -109,7 +109,7 @@ describe ThemeStore::TgzExporter do
|
||||
# Theme field names should be sanitized before writing to the database,
|
||||
# but protection is in place 'just in case'
|
||||
expect do
|
||||
theme.set_field(target: :translations, name: "en", value: "hacked")
|
||||
theme.set_field(target: :translations, name: SiteSetting.default_locale, value: "hacked")
|
||||
ThemeField.any_instance.stubs(:file_path).returns("../../malicious")
|
||||
theme.save!
|
||||
package
|
||||
|
||||
@@ -15,7 +15,8 @@ describe Wizard::StepUpdater do
|
||||
|
||||
context "locale" do
|
||||
it "does not require refresh when the language stays the same" do
|
||||
updater = wizard.create_updater('locale', default_locale: 'en')
|
||||
locale = SiteSettings::DefaultsProvider::DEFAULT_LOCALE
|
||||
updater = wizard.create_updater('locale', default_locale: locale)
|
||||
updater.update
|
||||
expect(updater.refresh_required?).to eq(false)
|
||||
expect(wizard.completed_steps?('locale')).to eq(true)
|
||||
|
||||
Reference in New Issue
Block a user