mirror of
https://ghfast.top/https://github.com/discourse/discourse-translator.git
synced 2026-07-15 11:36:26 +08:00
Currently (before the custom fields to tables migrations), locales are sometimes saved as "pt-PT" and "pt_BR" due to the API returning the former and us saving the latter through I18n.locale. e.g. we are seeing the following in the custom fields, which would mean that the table migrations (https://github.com/discourse/discourse-translator/pull/201) also have inherited the discrepancies. ``` #<PostCustomField:0x00007faffb49f798 id: 12321231, post_id: 1231241, name: "translated_text", value: "{\"en_GB\":\"\\u003cp\\u003eGreat post my friend \\u00...", # < locale is underscored ...> # and #<PostCustomField:0x00007faffb49dfd8 id: 12313123, post_id: 123123, name: "post_detected_lang", value: "pt-PT", # < locale is hyphenated ...> ``` This commit adds a migration to convert all values to the hyphenated version, ensures we save the hyphenated ones to the db, and introduces a `locale_matches?` on the translatable models.
99 lines
3.1 KiB
Ruby
99 lines
3.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "rails_helper"
|
|
|
|
describe Topic do
|
|
describe "translatable" do
|
|
fab!(:topic)
|
|
|
|
before { SiteSetting.translator_enabled = true }
|
|
|
|
it "should reset translation data when topic title has been updated" do
|
|
Fabricate(:topic_translation, topic:)
|
|
Fabricate(:topic_locale, topic:)
|
|
topic.update!(title: "this is an updated title")
|
|
|
|
expect(DiscourseTranslator::TopicLocale.where(topic:)).to be_empty
|
|
expect(DiscourseTranslator::TopicLocale.find_by(topic:)).to be_nil
|
|
end
|
|
|
|
describe "#set_translation" do
|
|
it "creates new translation" do
|
|
topic.set_translation("en", "Hello")
|
|
|
|
translation = topic.translations.find_by(locale: "en")
|
|
expect(translation.translation).to eq("Hello")
|
|
end
|
|
|
|
it "updates existing translation" do
|
|
topic.set_translation("en", "Hello")
|
|
topic.set_translation("en", "Updated hello")
|
|
|
|
expect(topic.translations.where(locale: "en").count).to eq(1)
|
|
expect(topic.translation_for("en")).to eq("Updated hello")
|
|
end
|
|
|
|
it "converts underscore to hyphen in locale" do
|
|
topic.set_translation("en_US", "Hello")
|
|
|
|
expect(topic.translations.find_by(locale: "en-US")).to be_present
|
|
expect(topic.translations.find_by(locale: "en_US")).to be_nil
|
|
end
|
|
end
|
|
|
|
describe "#translation_for" do
|
|
it "returns nil when translation doesn't exist" do
|
|
expect(topic.translation_for("fr")).to be_nil
|
|
end
|
|
|
|
it "returns translation when it exists" do
|
|
topic.set_translation("es", "Hola")
|
|
expect(topic.translation_for("es")).to eq("Hola")
|
|
end
|
|
end
|
|
|
|
describe "#set_locale" do
|
|
it "creates new locale" do
|
|
topic.set_detected_locale("en-US")
|
|
expect(topic.content_locale.detected_locale).to eq("en-US")
|
|
end
|
|
|
|
it "converts underscore to hyphen" do
|
|
topic.set_detected_locale("en_US")
|
|
expect(topic.content_locale.detected_locale).to eq("en-US")
|
|
end
|
|
end
|
|
|
|
describe "#locale_matches?" do
|
|
it "returns false when detected locale is blank" do
|
|
expect(topic.locale_matches?("en-US")).to eq(false)
|
|
end
|
|
|
|
it "returns false when locale is blank" do
|
|
topic.set_detected_locale("en-US")
|
|
expect(topic.locale_matches?(nil)).to eq(false)
|
|
end
|
|
|
|
[:en, "en", "en-US", :en_US, "en-GB", "en_GB", :en_GB].each do |locale|
|
|
it "returns true when matching normalised #{locale} to \"en\"" do
|
|
topic.set_detected_locale("en")
|
|
expect(topic.locale_matches?(locale)).to eq(true)
|
|
end
|
|
end
|
|
|
|
["en-GB", "en_GB", :en_GB].each do |locale|
|
|
it "returns true when matching #{locale} to \"en_GB\"" do
|
|
topic.set_detected_locale("en_GB")
|
|
expect(topic.locale_matches?(locale, normalise_region: false)).to eq(true)
|
|
end
|
|
end
|
|
|
|
[:en, "en", "en-US", :en_US].each do |locale|
|
|
it "returns false when matching #{locale} to \"en_GB\"" do
|
|
topic.set_detected_locale("en_GB")
|
|
expect(topic.locale_matches?(locale, normalise_region: false)).to eq(false)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|