mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 07:19:21 +08:00
FEATURE: Allow showing site text search in selected locale (#28453)
When searching for site texts for admin using the english version of the text, previously we would show the english version in the results _even if_ there was another locale translated version available when a locale was selected from the dropdown. This commit adds a "Only show results in selected locale" checkbox option which will instead make it so the results shown are in the target locale, making it easier for translators to tell when there is actually translations vs. missing tranlsations.
This commit is contained in:
@ -22,6 +22,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||
overridden = params[:overridden] == "true"
|
||||
outdated = params[:outdated] == "true"
|
||||
untranslated = params[:untranslated] == "true"
|
||||
only_selected_locale = params[:only_selected_locale] == "true"
|
||||
extras = {}
|
||||
|
||||
query = params[:q] || ""
|
||||
@ -32,7 +33,8 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||
extras[:recommended] = true
|
||||
results = self.class.preferred_keys.map { |k| record_for(key: k, locale: locale) }
|
||||
else
|
||||
results = find_translations(query, overridden, outdated, locale, untranslated)
|
||||
results =
|
||||
find_translations(query, overridden, outdated, locale, untranslated, only_selected_locale)
|
||||
|
||||
if results.any?
|
||||
extras[:regex] = I18n::Backend::DiscourseI18n.create_search_regexp(query, as_string: true)
|
||||
@ -173,6 +175,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||
def record_for(key:, value: nil, locale:)
|
||||
en_key = TranslationOverride.transform_pluralized_key(key)
|
||||
value ||= I18n.with_locale(locale) { I18n.t(key) }
|
||||
|
||||
interpolation_keys =
|
||||
I18nInterpolationKeysFinder.find(I18n.overrides_disabled { I18n.t(en_key, locale: :en) })
|
||||
custom_keys = TranslationOverride.custom_interpolation_keys(en_key)
|
||||
@ -203,7 +206,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||
raise Discourse::NotFound
|
||||
end
|
||||
|
||||
def find_translations(query, overridden, outdated, locale, untranslated)
|
||||
def find_translations(query, overridden, outdated, locale, untranslated, only_selected_locale)
|
||||
translations = Hash.new { |hash, key| hash[key] = {} }
|
||||
search_results =
|
||||
I18n.with_locale(locale) do
|
||||
@ -232,7 +235,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||
next unless I18n.exists?(key, :en)
|
||||
|
||||
if value.is_a?(Hash)
|
||||
fix_plural_keys(key, value, locale).each do |plural|
|
||||
fix_plural_keys(key, value, locale, only_selected_locale).each do |plural|
|
||||
plural_key = plural[0]
|
||||
plural_value = plural[1]
|
||||
|
||||
@ -243,6 +246,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||
)
|
||||
end
|
||||
else
|
||||
value = I18n.with_locale(locale) { I18n.t(key) } if only_selected_locale
|
||||
results << record_for(key: key, value: value, locale: locale)
|
||||
end
|
||||
end
|
||||
@ -250,7 +254,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||
results
|
||||
end
|
||||
|
||||
def fix_plural_keys(key, value, locale)
|
||||
def fix_plural_keys(key, value, locale, only_selected_locale = false)
|
||||
value = value.with_indifferent_access
|
||||
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) }
|
||||
@ -258,6 +262,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||
fallback_value = I18n.t(key, locale: :en, default: {})
|
||||
plural_keys.map do |k|
|
||||
if value[k]
|
||||
value[k] = I18n.with_locale(locale) { I18n.t("#{key}.#{k}") } if only_selected_locale
|
||||
[k, value[k], locale]
|
||||
else
|
||||
[k, fallback_value[k] || fallback_value[:other], :en]
|
||||
|
Reference in New Issue
Block a user