2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
def extract_locale(path)
|
|
|
|
path[/\.([^.]{2,})\.yml$/, 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_yaml_compatible?(english, translated)
|
|
|
|
english.each do |k, v|
|
|
|
|
if translated.has_key?(k)
|
|
|
|
if Hash === v
|
|
|
|
if Hash === translated[k]
|
|
|
|
return false unless is_yaml_compatible?(v, translated[k])
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return false unless v.class == translated[k].class
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2022-12-14 03:03:53 +08:00
|
|
|
def load_yaml(path)
|
|
|
|
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.1.0")
|
|
|
|
YAML.load_file(path, aliases: true)
|
|
|
|
else
|
|
|
|
YAML.load_file(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe "i18n integrity checks" do
|
2017-02-24 18:35:33 +08:00
|
|
|
it "has an i18n key for each Site Setting" do
|
2013-02-06 03:16:51 +08:00
|
|
|
SiteSetting.all_settings.each do |s|
|
2023-05-25 07:53:57 +08:00
|
|
|
next if s[:plugin] == SiteSetting::SAMPLE_TEST_PLUGIN.name
|
2023-06-16 22:28:03 +08:00
|
|
|
expect(s[:description]).not_to match(/Translation missing/)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
it "has an i18n key for each Badge description" do
|
2016-04-13 00:55:49 +08:00
|
|
|
Badge
|
|
|
|
.where(system: true)
|
|
|
|
.each do |b|
|
|
|
|
expect(b.long_description).to be_present
|
|
|
|
expect(b.description).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-19 10:13:23 +08:00
|
|
|
Dir["#{Rails.root}/config/locales/{client,server}.*.yml"].each do |path|
|
|
|
|
it "does not contain invalid interpolation keys for '#{path}'" do
|
|
|
|
matches = File.read(path).scan(/%\{([^a-zA-Z\s]+)\}|\{\{([^a-zA-Z\s]+)\}\}/)
|
|
|
|
matches.flatten!
|
|
|
|
matches.compact!
|
|
|
|
matches.uniq!
|
|
|
|
expect(matches).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
Dir["#{Rails.root}/config/locales/client.*.yml"].each do |path|
|
|
|
|
it "has valid client YAML for '#{path}'" do
|
2022-12-14 03:03:53 +08:00
|
|
|
yaml = load_yaml(path)
|
2017-02-24 18:35:33 +08:00
|
|
|
locale = extract_locale(path)
|
2013-03-23 15:57:31 +08:00
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
expect(yaml.keys).to eq([locale])
|
2013-03-23 15:57:31 +08:00
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
expect(yaml[locale]["js"]).to be
|
2019-05-16 05:43:00 +08:00
|
|
|
|
|
|
|
if !LocaleSiteSetting.fallback_locale(locale)
|
|
|
|
expect(yaml[locale]["admin_js"]).to be
|
|
|
|
expect(yaml[locale]["wizard_js"]).to be
|
|
|
|
end
|
2013-03-20 17:51:28 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
Dir["#{Rails.root}/**/locale*/*.en.yml"].each do |english_path|
|
2022-12-14 03:03:53 +08:00
|
|
|
english_yaml = load_yaml(english_path)["en"]
|
2015-09-19 03:12:37 +08:00
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
context(english_path) do
|
|
|
|
it "has no duplicate keys" do
|
|
|
|
english_duplicates = DuplicateKeyFinder.new.find_duplicates(english_path)
|
|
|
|
expect(english_duplicates).to be_empty
|
2015-09-19 03:12:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
Dir[english_path.sub(".en.yml", ".*.yml")].each do |path|
|
|
|
|
next if path[".en.yml"]
|
2015-09-25 06:43:23 +08:00
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
context(path) do
|
|
|
|
locale = extract_locale(path)
|
2022-12-14 03:03:53 +08:00
|
|
|
yaml = load_yaml(path)
|
2015-09-25 06:43:23 +08:00
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
it "has no duplicate keys" do
|
|
|
|
duplicates = DuplicateKeyFinder.new.find_duplicates(path)
|
|
|
|
expect(duplicates).to be_empty
|
2015-09-25 06:43:23 +08:00
|
|
|
end
|
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
it "does not overwrite another locale" do
|
|
|
|
expect(yaml.keys).to eq([locale])
|
2015-09-19 03:12:37 +08:00
|
|
|
end
|
2015-09-25 06:43:23 +08:00
|
|
|
|
2017-02-24 18:35:33 +08:00
|
|
|
unless path["transliterate"]
|
|
|
|
it "is compatible with english" do
|
|
|
|
expect(is_yaml_compatible?(english_yaml, yaml)).to eq(true)
|
2015-09-25 06:43:23 +08:00
|
|
|
end
|
|
|
|
end
|
2015-09-19 03:12:37 +08:00
|
|
|
end
|
2021-01-09 00:47:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe "fallbacks" do
|
2021-01-09 00:47:28 +08:00
|
|
|
before do
|
|
|
|
I18n.backend = I18n::Backend::DiscourseI18n.new
|
|
|
|
I18n.fallbacks = I18n::Backend::FallbackLocaleList.new
|
|
|
|
I18n.reload!
|
|
|
|
I18n.init_accelerator!
|
|
|
|
end
|
2017-02-24 18:35:33 +08:00
|
|
|
|
2021-01-09 00:47:28 +08:00
|
|
|
it "finds the fallback translation" do
|
|
|
|
I18n.backend.store_translations(:en, test: "en test")
|
|
|
|
|
|
|
|
I18n.with_locale("pl_PL") { expect(I18n.t("test")).to eq("en test") }
|
2015-09-19 03:12:37 +08:00
|
|
|
end
|
2017-02-24 18:35:33 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when in a multi-threaded environment" do
|
2021-01-09 00:47:28 +08:00
|
|
|
it "finds the fallback translation" do
|
|
|
|
I18n.backend.store_translations(:en, test: "en test")
|
|
|
|
|
|
|
|
thread = Thread.new { I18n.with_locale("pl_PL") { expect(I18n.t("test")).to eq("en test") } }
|
|
|
|
|
|
|
|
begin
|
|
|
|
thread.join
|
|
|
|
ensure
|
|
|
|
thread.exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|