FIX: I18n couldn't find translations. (#11774)

"I18n.t(key, locale: locale)" fails to find the correct translation in some cases. We should always wrap it with the "I18n.with_locale(locale)" method.

Also, reverting an override wasn't always possible because the serializer always used "I18n.locale" as the locale.
This commit is contained in:
Roman Rizzi 2021-01-20 17:43:00 -03:00 committed by GitHub
parent 3b2f6e129a
commit 82d2284ce6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 7 deletions

View File

@ -80,7 +80,7 @@ class Admin::SiteTextsController < Admin::AdminController
site_text = find_site_text(locale) site_text = find_site_text(locale)
value = site_text[:value] = params.dig(:site_text, :value) value = site_text[:value] = params.dig(:site_text, :value)
id = site_text[:id] id = site_text[:id]
old_value = I18n.t(id, locale: locale) old_value = I18n.with_locale(locale) { I18n.t(id) }
translation_override = TranslationOverride.upsert!(locale, id, value) translation_override = TranslationOverride.upsert!(locale, id, value)
@ -108,7 +108,7 @@ class Admin::SiteTextsController < Admin::AdminController
site_text = find_site_text(locale) site_text = find_site_text(locale)
id = site_text[:id] id = site_text[:id]
old_text = I18n.t(id, locale: locale) old_text = I18n.with_locale(locale) { I18n.t(id) }
TranslationOverride.revert!(locale, id) TranslationOverride.revert!(locale, id)
site_text = find_site_text(locale) site_text = find_site_text(locale)
@ -162,8 +162,8 @@ class Admin::SiteTextsController < Admin::AdminController
value = override&.first value = override&.first
end end
value ||= I18n.t(key, locale: locale) value ||= I18n.with_locale(locale) { I18n.t(key) }
{ id: key, value: value } { id: key, value: value, locale: locale }
end end
PLURALIZED_REGEX = /(.*)\.(zero|one|two|few|many|other)$/ PLURALIZED_REGEX = /(.*)\.(zero|one|two|few|many|other)$/
@ -187,8 +187,11 @@ class Admin::SiteTextsController < Admin::AdminController
def find_translations(query, overridden, locale) def find_translations(query, overridden, locale)
translations = Hash.new { |hash, key| hash[key] = {} } translations = Hash.new { |hash, key| hash[key] = {} }
search_results = I18n.with_locale(locale) do
I18n.search(query, overridden: overridden)
end
I18n.search(query, overridden: overridden, locale: locale).each do |key, value| search_results.each do |key, value|
if PLURALIZED_REGEX.match(key) if PLURALIZED_REGEX.match(key)
translations[$1][$2] = value translations[$1][$2] = value
else else
@ -220,7 +223,7 @@ class Admin::SiteTextsController < Admin::AdminController
def fix_plural_keys(key, value, locale) def fix_plural_keys(key, value, locale)
value = value.with_indifferent_access value = value.with_indifferent_access
plural_keys = I18n.t('i18n.plural.keys', locale: locale) plural_keys = I18n.with_locale(locale) { I18n.t('i18n.plural.keys') }
return value if value.keys.size == plural_keys.size && plural_keys.all? { |k| value.key?(k) } return value if value.keys.size == plural_keys.size && plural_keys.all? { |k| value.key?(k) }
fallback_value = I18n.t(key, locale: :en, default: {}) fallback_value = I18n.t(key, locale: :en, default: {})

View File

@ -15,7 +15,7 @@ class SiteTextSerializer < ApplicationSerializer
if options[:overridden_keys] if options[:overridden_keys]
options[:overridden_keys].include?(object[:id]) options[:overridden_keys].include?(object[:id])
else else
TranslationOverride.exists?(locale: I18n.locale, translation_key: object[:id]) TranslationOverride.exists?(locale: object[:locale], translation_key: object[:id])
end end
end end