Skip to content

Commit

Permalink
Fixes #36574 - Update fast_gettext to ~> 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren authored and evgeni committed Mar 4, 2024
1 parent de48fb5 commit bbca473
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 1 addition & 3 deletions config/initializers/1_fast_gettext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
18 changes: 12 additions & 6 deletions lib/foreman/gettext/all_domains.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 12 additions & 4 deletions lib/foreman/gettext/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit bbca473

Please sign in to comment.