mirror of
https://ghfast.top/https://github.com/discourse/discourse-translator.git
synced 2026-05-03 17:52:20 +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.
41 lines
1.1 KiB
Ruby
41 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class HyphenateTranslatorLocales < ActiveRecord::Migration[7.2]
|
|
BATCH_SIZE = 1000
|
|
|
|
def up
|
|
normalize_table("discourse_translator_topic_translations", "locale")
|
|
normalize_table("discourse_translator_post_translations", "locale")
|
|
normalize_table("discourse_translator_topic_locales", "detected_locale")
|
|
normalize_table("discourse_translator_post_locales", "detected_locale")
|
|
end
|
|
|
|
def down
|
|
raise ActiveRecord::IrreversibleMigration
|
|
end
|
|
|
|
private
|
|
|
|
def normalize_table(table_name, column)
|
|
start_id = 0
|
|
loop do
|
|
result = DB.query_single(<<~SQL, start_id: start_id, batch_size: BATCH_SIZE)
|
|
WITH batch AS (
|
|
SELECT id
|
|
FROM #{table_name}
|
|
WHERE #{column} LIKE '%\\_%' ESCAPE '\\'
|
|
AND id > :start_id
|
|
ORDER BY id
|
|
LIMIT :batch_size
|
|
)
|
|
UPDATE #{table_name}
|
|
SET #{column} = REGEXP_REPLACE(#{column}, '_', '-')
|
|
WHERE id IN (SELECT id FROM batch)
|
|
RETURNING id
|
|
SQL
|
|
|
|
break if result.empty?
|
|
start_id = result.max
|
|
end
|
|
end
|
|
end
|