# 将自定义语言移动到默认语言（ES\_XX 到 es）

**URL:** https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598
**Category:** Self-hosting
**Created:** [2023 年5 月 19 日 20:47 UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598 "2023-05-19T20:47:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![satonotdead](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/satonotdead/32/447830_2.png) [@satonotdead](https://meta.discourse.org/u/satonotdead)
#### Post date: [2023 年5 月 19 日 20:47 UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/1 "2023-05-19T20:47:29Z")

</div>

有什么正确的方法可以做到这一点？通过 MySQL，在 Discourse 中使用查询，或者通过某个脚本？

> 似乎[使用插件创建自定义语言](https://meta.discourse.org/t/trust-level-names-in-spanish/232351/12?u=matenauta)在我们的实例中会引起一些问题。

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [2023 年5 月 22 日 15:01 UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/2 "2023-05-22T15:01:32Z")

</div>

在 rails 控制台中执行以下命令即可生效：

```ruby
old_locale = "es_XX"
new_locale = "es"

DB.exec <<~SQL
  UPDATE users
  SET locale = '#{new_locale}'
  WHERE locale = '#{old_locale}'
SQL

DB.exec <<~SQL
  UPDATE site_settings
  SET value = '#{new_locale}'
  WHERE name = 'default_locale' AND value = '#{old_locale}'
SQL

DB.exec <<~SQL
  UPDATE translation_overrides
  SET locale = '#{new_locale}'
  WHERE locale = '#{old_locale}'
SQL

DB.exec <<~SQL
  UPDATE theme_translation_overrides
  SET locale = '#{new_locale}'
  WHERE locale = '#{old_locale}'
SQL

def fix_search_data(model)
  key = "#{model}_id"
  table = "#{model}_search_data"

  puts "Migrating #{table} to new_locale locale."

  sql = <<~SQL
    UPDATE #{table}
        SET locale = '#{new_locale}'
      WHERE #{key} IN (
            SELECT #{key}
              FROM #{table}
              WHERE locale = '#{old_locale}'
              LIMIT 100000
          )
  SQL

  loop do
    count = DB.exec(sql)
    break if count == 0
    puts "Migrated #{count} rows of #{table} to new locale."
  end
end
  
%w[category tag topic user].each { |model| fix_search_data(model) }

```

---

<div class="post-metadata">

### Author: ![satonotdead](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/satonotdead/32/447830_2.png) [@satonotdead](https://meta.discourse.org/u/satonotdead)
#### Post date: [2023 年5 月 24 日 01:03 UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/3 "2023-05-24T01:03:03Z")

</div>

谢谢！处理 locale 中被覆盖字符串的最有效方法是什么？

代码似乎有效，但不能与目标“默认”locale 中被覆盖的字符串一起使用。

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [2023 年5 月 24 日 13:16 UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/4 "2023-05-24T13:16:34Z")

</div>

在 Rails 控制台中运行以下命令应该会有帮助：

```ruby
I18n.reload!
ExtraLocalesController.clear_cache!
MessageBus.publish("/i18n-flush", refresh: true)

```

如果这不起作用，重启服务器肯定会奏效。

---

<div class="post-metadata">

### Author: ![satonotdead](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/satonotdead/32/447830_2.png) [@satonotdead](https://meta.discourse.org/u/satonotdead)
#### Post date: [2023 年5 月 24 日 14:51 UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/5 "2023-05-24T14:51:26Z")

</div>

谢谢，这似乎与我的实例中的某些内容有关，但我可能在上一条消息中没有很好地表达自己 🙂

我复制了错误消息，以防万一：

```plaintext
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_translation_overrides_on_locale_and_translation_key"
DETAIL: Key (locale, translation_key)=(es, js.docs.categories) already exists.

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2024 年9 月 11 日 22:25 UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/6 "2024-09-11T22:25:36Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
