# Move custom language to default one (ES\_XX to es)

**URL:** <https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598>\
**Category:** Self-hosting\
**Created:** [May 19, 2023, 8:47pm 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

## AI-generated summary

The discussion is about moving a custom language (`es_XX`) to the default language (`es`) in a Discourse instance. [matenauta](/t/-/265598/1) asks for the proper way to do it, and [gerhard](/t/-/265598/2) provides a script to execute in a rails console to update the locale in the database. However, [matenauta](/t/-/265598/3) encounters an issue with overridden strings in the destination locale, and [gerhard](/t/-/265598/4) suggests running additional commands to clear the cache and reload translations. Despite this, [matenauta](/t/-/265598/5) still encounters a `PG::UniqueViolation` error due to a duplicate key in the `translation_overrides` table.

<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:** [May 19, 2023, 8:47pm UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/1 "2023-05-19T20:47:29Z")

</div>

What’s the proper way to do it? From mysql, using queries within Discourse, maybe with some script?

> It seems that [using a plugin to create custom language](https://meta.discourse.org/t/trust-level-names-in-spanish/232351/12) causes some issues in our instance.

---

<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:** [May 22, 2023, 3:01pm UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/2 "2023-05-22T15:01:32Z")

</div>

Executing the following in a rails console should work:

```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:** [May 24, 2023, 1:03am UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/3 "2023-05-24T01:03:03Z")

</div>

Thanks! What’s the most effective method to deal with overrided strings in locale?

Code seems to work but not with overrided in destination ‘default’ 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:** [May 24, 2023, 1:16pm UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/4 "2023-05-24T13:16:34Z")

</div>

Running the following in the rails console should help:

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

```

If it doesn’t, restarting the server should definitely work.

---

<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:** [May 24, 2023, 2:51pm UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/5 "2023-05-24T14:51:26Z")

</div>

Thanks, it seems to be related to something in my instance but maybe I didn’t express myself well in my previous message 🙂

I copy the error message, just in case:

```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:** [September 11, 2024, 10:25pm UTC](https://meta.discourse.org/t/move-custom-language-to-default-one-es-xx-to-es/265598/6 "2024-09-11T22:25:36Z")

</div>

Closed on September 11, 2024, 10:25pm UTC

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