discourse-translator/spec/models/hyphenate_locales_spec.rb
Natalie Tay 644a165818
DEV: Hyphenate locales (#206)
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.
2025-02-11 16:04:22 +08:00

63 lines
2.2 KiB
Ruby

# frozen_string_literal: true
require_relative "../../db/migrate/20250210171147_hyphenate_translator_locales"
module DiscourseTranslator
describe HyphenateTranslatorLocales do
let(:migration) { described_class.new }
it "normalizes underscores to dashes in all translator tables" do
topic = Fabricate(:topic)
post = Fabricate(:post, topic: topic)
DiscourseTranslator::TopicTranslation.create!(topic:, locale: "en_GB", translation: "test")
DiscourseTranslator::PostTranslation.create!(post:, locale: "fr_CA", translation: "test")
DiscourseTranslator::TopicLocale.create!(topic:, detected_locale: "es_MX")
DiscourseTranslator::PostLocale.create!(post:, detected_locale: "pt_BR")
migration.up
expect(DiscourseTranslator::TopicTranslation.last.locale).to eq("en-GB")
expect(DiscourseTranslator::PostTranslation.last.locale).to eq("fr-CA")
expect(DiscourseTranslator::TopicLocale.last.detected_locale).to eq("es-MX")
expect(DiscourseTranslator::PostLocale.last.detected_locale).to eq("pt-BR")
end
it "handles multiple batches" do
described_class.const_set(:BATCH_SIZE, 2)
topic = Fabricate(:topic)
post = Fabricate(:post, topic: topic)
5.times { |i| post.set_translation("en_#{i}", "test#{i}") }
5.times { |i| post.set_translation("en-#{i + 10}", "test#{i}") }
5.times { |i| post.set_translation("en_#{i + 20}", "test#{i}") }
migration.up
locales = DiscourseTranslator::PostTranslation.pluck(:locale)
expect(locales).to all(match(/\A[a-z]+-\d+\z/))
expect(locales).not_to include(match(/_/))
end
it "only updates records containing underscores" do
topic = Fabricate(:topic)
topic.set_translation("en_GB", "test")
DiscourseTranslator::TopicTranslation.create!(
topic: topic,
locale: "fr_CA",
translation: "test2",
)
expect { migration.up }.to change {
DiscourseTranslator::TopicTranslation.where("locale LIKE ? ESCAPE '\\'", "%\\_%").count
}.from(1).to(0)
expect(DiscourseTranslator::TopicTranslation.pluck(:locale)).to contain_exactly(
"en-GB",
"fr-CA",
)
end
end
end