From c8f67ae241271cd832416d710a8e35680cdbecf3 Mon Sep 17 00:00:00 2001 From: Oleh Fedorenko Date: Fri, 13 Oct 2023 17:16:35 +0000 Subject: [PATCH] Fixes #36574 - Update fast_gettext to ~> 2.1 --- Gemfile | 2 +- config/initializers/1_fast_gettext.rb | 4 +--- lib/foreman/gettext/all_domains.rb | 18 ++++++++++++------ lib/foreman/gettext/debug.rb | 16 ++++++++++++---- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index 6a7acce2e47..5a9de0d2b67 100644 --- a/Gemfile +++ b/Gemfile @@ -22,7 +22,7 @@ gem 'validates_lengths_from_database', '~> 0.5' gem 'friendly_id', '>= 5.4.2', '< 6' gem 'secure_headers', '~> 6.3' gem 'safemode', '>= 1.4', '< 2' -gem 'fast_gettext', '~> 1.4' +gem 'fast_gettext', '~> 2.1' gem 'gettext_i18n_rails', '~> 1.8' gem 'rails-i18n', '~> 7.0' gem 'logging', '>= 1.8.0', '< 3.0.0' diff --git a/config/initializers/1_fast_gettext.rb b/config/initializers/1_fast_gettext.rb index 4f39ddbc5e3..6f89ad7ae36 100644 --- a/config/initializers/1_fast_gettext.rb +++ b/config/initializers/1_fast_gettext.rb @@ -19,9 +19,6 @@ FastGettext.default_locale = "en" FastGettext.locale = "en" -# work in all domains context by default (for plugins) -include FastGettext::TranslationMultidomain - # Keep TRANSLATORS comments Rails.application.config.gettext_i18n_rails.xgettext = %w[--add-comments=TRANSLATORS:] # Disable fuzzy .po merging @@ -32,5 +29,6 @@ if SETTINGS[:mark_translated] && !Rails.env.test? include Foreman::Gettext::Debug else + # work in all domains context by default (for plugins) include Foreman::Gettext::AllDomains end diff --git a/lib/foreman/gettext/all_domains.rb b/lib/foreman/gettext/all_domains.rb index ff9704598df..0826d5658bb 100644 --- a/lib/foreman/gettext/all_domains.rb +++ b/lib/foreman/gettext/all_domains.rb @@ -1,23 +1,29 @@ -require 'fast_gettext' - # include this module to translate in all domains by default module Foreman module Gettext module AllDomains + class Localizer + prepend FastGettext::TranslationMultidomain + end + + def self.localizer + @localizer ||= Localizer.new + end + def _(key) - FastGettext::TranslationMultidomain.D_(key) + Foreman::Gettext::AllDomains.localizer.D_(key) end def n_(*keys) - FastGettext::TranslationMultidomain.Dn_(*keys) + Foreman::Gettext::AllDomains.localizer.Dn_(*keys) end def s_(key, separator = nil) - FastGettext::TranslationMultidomain.Ds_(key, separator) + Foreman::Gettext::AllDomains.localizer.Ds_(key, separator) end def ns_(*keys) - FastGettext::TranslationMultidomain.Dns_(*keys) + Foreman::Gettext::AllDomains.localizer.Dns_(*keys) end end end diff --git a/lib/foreman/gettext/debug.rb b/lib/foreman/gettext/debug.rb index 1826820db92..277a77059da 100644 --- a/lib/foreman/gettext/debug.rb +++ b/lib/foreman/gettext/debug.rb @@ -7,10 +7,18 @@ module Debug DL = "\u00BB".encode("UTF-8") rescue '>' DR = "\u00AB".encode("UTF-8") rescue '<' + class Localizer + prepend FastGettext::TranslationMultidomain + end + + def self.localizer + @localizer ||= Localizer.new + end + # slightly modified copy of fast_gettext D_* method def _(key) FastGettext.translation_repositories.each_key do |domain| - result = FastGettext::TranslationMultidomain.d_(domain, key) { nil } + result = Foreman::Gettext::Debug.localizer.d_(domain, key) { nil } return DL + result.to_s + DR unless result.nil? end DL + key.to_s + DR @@ -19,7 +27,7 @@ def _(key) # slightly modified copy of fast_gettext D_* method def n_(*keys) FastGettext.translation_repositories.each_key do |domain| - result = FastGettext::TranslationMultidomain.dn_(domain, *keys) { nil } + result = Foreman::Gettext::Debug.localizer.dn_(domain, *keys) { nil } return DL + result.to_s + DR unless result.nil? end DL + keys[-3].split(keys[-2] || FastGettext::NAMESPACE_SEPARATOR).last.to_s + DR @@ -28,7 +36,7 @@ def n_(*keys) # slightly modified copy of fast_gettext D_* method def s_(key, separator = nil) FastGettext.translation_repositories.each_key do |domain| - result = FastGettext::TranslationMultidomain.ds_(domain, key, separator) { nil } + result = Foreman::Gettext::Debug.localizer.ds_(domain, key, separator) { nil } return DL + result.to_s + DR unless result.nil? end DL + key.split(separator || FastGettext::NAMESPACE_SEPARATOR).last.to_s + DR @@ -37,7 +45,7 @@ def s_(key, separator = nil) # slightly modified copy of fast_gettext D_* method def ns_(*keys) FastGettext.translation_repositories.each_key do |domain| - result = FastGettext::TranslationMultidomain.dns_(domain, *keys) { nil } + result = Foreman::Gettext::Debug.localizer.dns_(domain, *keys) { nil } return DL + result.to_s + DR unless result.nil? end DL + keys[-2].split(FastGettext::NAMESPACE_SEPARATOR).last.to_s + DR