2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe LocaleSiteSetting do
|
2018-01-25 19:09:18 +08:00
|
|
|
def core_locales
|
2023-01-09 19:18:21 +08:00
|
|
|
pattern = File.join(Rails.root, "config", "locales", "client.*.yml")
|
|
|
|
Dir.glob(pattern).map { |x| x.split(".")[-2] }
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def native_locale_name(locale)
|
|
|
|
value = LocaleSiteSetting.values.find { |v| v[:value] == locale }
|
|
|
|
value[:name]
|
|
|
|
end
|
2013-06-11 23:39:55 +08:00
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
describe ".valid_value?" do
|
|
|
|
it "returns true for a locale that we have translations for" do
|
|
|
|
expect(LocaleSiteSetting.valid_value?("en")).to eq(true)
|
2013-06-11 23:39:55 +08:00
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
it "returns false for a locale that we do not have translations for" do
|
|
|
|
expect(LocaleSiteSetting.valid_value?("swedish-chef")).to eq(false)
|
2013-06-11 23:39:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
describe ".values" do
|
|
|
|
it "returns all the locales that we have translations for" do
|
2018-01-25 19:09:18 +08:00
|
|
|
expect(LocaleSiteSetting.values.map { |x| x[:value] }).to include(*core_locales)
|
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
it "returns native names" do
|
|
|
|
expect(native_locale_name("de")).to eq("Deutsch")
|
|
|
|
expect(native_locale_name("zh_CN")).to eq("简体中文")
|
|
|
|
expect(native_locale_name("zh_TW")).to eq("繁體中文")
|
2013-06-11 23:39:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
context "with locales from plugin" do
|
2018-01-25 19:09:18 +08:00
|
|
|
before do
|
|
|
|
DiscoursePluginRegistry.register_locale("foo", name: "Foo", nativeName: "Native Foo")
|
|
|
|
DiscoursePluginRegistry.register_locale("bar", name: "Bar", nativeName: "Native Bar")
|
2023-01-09 19:18:21 +08:00
|
|
|
DiscoursePluginRegistry.register_locale(
|
|
|
|
"de",
|
|
|
|
name: "Renamed German",
|
|
|
|
nativeName: "Native renamed German",
|
|
|
|
)
|
|
|
|
DiscoursePluginRegistry.register_locale(
|
|
|
|
"de_AT",
|
|
|
|
name: "German (Austria)",
|
|
|
|
nativeName: "Österreichisch",
|
|
|
|
fallbackLocale: "de",
|
|
|
|
)
|
2018-01-25 19:09:18 +08:00
|
|
|
DiscoursePluginRegistry.register_locale("tlh")
|
|
|
|
|
|
|
|
# Plugins normally register a locale before LocaleSiteSetting is initialized.
|
|
|
|
# That's not happening in tests, so we need to call reset!
|
|
|
|
LocaleSiteSetting.reset!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
DiscoursePluginRegistry.reset!
|
2019-06-03 10:42:19 +08:00
|
|
|
LocaleSiteSetting.reset!
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
describe ".valid_value?" do
|
|
|
|
it "returns true for locales from core" do
|
|
|
|
expect(LocaleSiteSetting.valid_value?("en")).to eq(true)
|
|
|
|
expect(LocaleSiteSetting.valid_value?("de")).to eq(true)
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
it "returns true for locales added by plugins" do
|
|
|
|
expect(LocaleSiteSetting.valid_value?("foo")).to eq(true)
|
|
|
|
expect(LocaleSiteSetting.valid_value?("bar")).to eq(true)
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
describe ".values" do
|
|
|
|
it "returns native names added by plugin" do
|
|
|
|
expect(native_locale_name("foo")).to eq("Native Foo")
|
|
|
|
expect(native_locale_name("bar")).to eq("Native Bar")
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
it "does not allow plugins to override native names that exist in core" do
|
|
|
|
expect(native_locale_name("de")).to eq("Deutsch")
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
it "returns the language code when no nativeName is set" do
|
|
|
|
expect(native_locale_name("tlh")).to eq("tlh")
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
describe ".fallback_locale" do
|
|
|
|
it "returns the fallback locale registered by plugin" do
|
|
|
|
expect(LocaleSiteSetting.fallback_locale("de_AT")).to eq(:de)
|
2018-01-25 19:09:18 +08:00
|
|
|
expect(LocaleSiteSetting.fallback_locale(:de_AT)).to eq(:de)
|
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
it "returns nothing when no fallback locale was registered" do
|
|
|
|
expect(LocaleSiteSetting.fallback_locale("foo")).to be_nil
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
2019-05-16 05:43:00 +08:00
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
it "returns English for English (UK)" do
|
|
|
|
expect(LocaleSiteSetting.fallback_locale("en_GB")).to eq(:en)
|
2019-05-16 05:43:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 19:18:21 +08:00
|
|
|
describe ".fallback_locale" do
|
|
|
|
it "returns English for English (UK)" do
|
|
|
|
expect(LocaleSiteSetting.fallback_locale("en_GB")).to eq(:en)
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
end
|
2013-06-11 23:39:55 +08:00
|
|
|
end
|