Skip to content

Commit

Permalink
Refs #36574 - Avoid patching Fastgettext
Browse files Browse the repository at this point in the history
The code defines an additional method on Fastgettext but there's no
reason to do so. It can just as well define a method on itself.
  • Loading branch information
ekohl committed Jul 7, 2023
1 parent ac22bb7 commit d93941a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/registries/foreman/settings/general.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Foreman::SettingManager.define(:foreman) do
locales = -> { Hash['' => _("Browser locale")].merge(Hash[FastGettext.human_available_locales.map { |lang| [lang[1], lang[0]] }]) }
locales = -> { Hash['' => _("Browser locale")].merge(Hash[Foreman::Gettext::Support.human_available_locales.map { |lang| [lang[1], lang[0]] }]) }
timezones = -> { Hash['' => _("Browser timezone")].merge(Hash[ActiveSupport::TimeZone.all.map { |tz| [tz.name, "(GMT #{tz.formatted_offset}) #{tz.name}"] }]) }

category(:general, N_('General')) do
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<% end %>
<%= textarea_f f, :description, :rows=> 5, :size => "col-md-4" %>
<%= selectable_f(f, :locale, FastGettext.human_available_locales,
<%= selectable_f(f, :locale, Foreman::Gettext::Support.human_available_locales,
{ :include_blank => _('Browser locale') } , { :label => _('Language') } ) %>
<%= time_zone_select_f(f, :timezone, Time.find_zone(@user.timezone) || Time.find_zone!('UTC'), { :include_blank => _('Browser timezone') }) %>
Expand Down
2 changes: 1 addition & 1 deletion config/puma/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@
# in development environment and little less on production. Let's eager load languages
# for production before forking to save memory on CoW operating systems.
before_fork do
FastGettext.human_available_locales
Foreman::Gettext::Support.human_available_locales
end
2 changes: 1 addition & 1 deletion extras/dynflow-sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Loading and initializing of all gettext languages takes about 100ms per language
# in development environment and little less on production. Let's eager load languages
# but only for production.
FastGettext.human_available_locales
Foreman::Gettext::Support.human_available_locales
end

rails_env_file = File.expand_path('./config/environment.rb', rails_root)
Expand Down
36 changes: 15 additions & 21 deletions lib/foreman/gettext/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Foreman
module Gettext
module Support
@@mutex = Mutex.new

def self.detect_locale_type
if Rails.env.development?
:po
Expand Down Expand Up @@ -44,29 +46,21 @@ def self.add_text_domain(locale_domain, locale_dir)

# Loading all available locales to get the language translation is slow
# (2 seconds for 20 languages), so let's lazy load that.
FastGettext.class_eval do
attr_accessor :mutex, :locales

self.mutex = Mutex.new

def human_available_locales
original_locale = FastGettext.locale
mutex.synchronize do
return locales if locales
Rails.logger.debug "Loading and caching all available locales"
locales = []
FastGettext.default_available_locales.sort.each do |locale|
FastGettext.locale = locale
# TRANSLATORS: Provide locale name in native language (e.g. Deutsch instead of German)
human_locale = _("locale_name")
human_locale = locale if human_locale == "locale_name"
locales << [human_locale, locale]
end
locales
def self.human_available_locales
original_locale = FastGettext.locale
@@mutex.synchronize do
return @@locales if @@locales
Rails.logger.debug "Loading and caching all available locales"
@@locales = FastGettext.default_available_locales.sort.map do |locale|
FastGettext.locale = locale
# TRANSLATORS: Provide locale name in native language (e.g. Deutsch instead of German)
human_locale = _("locale_name")
human_locale = locale if human_locale == "locale_name"
[human_locale, locale]
end
ensure
FastGettext.locale = original_locale
end
ensure
FastGettext.locale = original_locale
end
end
end
Expand Down

0 comments on commit d93941a

Please sign in to comment.