diff --git a/app/registries/foreman/settings/general.rb b/app/registries/foreman/settings/general.rb index 96b41976375e..e4f6ec66474e 100644 --- a/app/registries/foreman/settings/general.rb +++ b/app/registries/foreman/settings/general.rb @@ -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 diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb index 16a6895c9f35..07873b5e6291 100644 --- a/app/views/users/_form.html.erb +++ b/app/views/users/_form.html.erb @@ -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') }) %> diff --git a/config/puma/production.rb b/config/puma/production.rb index 28fb8eb482ae..85cb03a67545 100644 --- a/config/puma/production.rb +++ b/config/puma/production.rb @@ -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 diff --git a/extras/dynflow-sidekiq.rb b/extras/dynflow-sidekiq.rb index 892e01c7cf43..cae01e76a9a7 100644 --- a/extras/dynflow-sidekiq.rb +++ b/extras/dynflow-sidekiq.rb @@ -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) diff --git a/lib/foreman/gettext/support.rb b/lib/foreman/gettext/support.rb index 3057c3c9cfc4..814b6ecf9b57 100644 --- a/lib/foreman/gettext/support.rb +++ b/lib/foreman/gettext/support.rb @@ -3,6 +3,8 @@ module Foreman module Gettext module Support + @@mutex = Mutex.new + def self.detect_locale_type if Rails.env.development? :po @@ -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