diff options
author | Ruben Davila <rdavila84@gmail.com> | 2017-05-25 18:22:45 +0300 |
---|---|---|
committer | Ruben Davila <rdavila84@gmail.com> | 2017-05-25 18:22:45 +0300 |
commit | 5c921809cd86e1031a6a5075da142fef3bb01d6d (patch) | |
tree | 0c04cf9d24f9e94c2bf5dfd709c22f142bec5e77 /lib/gitlab/i18n.rb | |
parent | d07e85e1033f8026f155c6171cb3ebcd6b93d9ed (diff) |
Bugfix: Always use the default language when generating emails.
There was a race condition issue when the application was generating an
email and was using a language that was previously being used in other
request.
Diffstat (limited to 'lib/gitlab/i18n.rb')
-rw-r--r-- | lib/gitlab/i18n.rb | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/gitlab/i18n.rb b/lib/gitlab/i18n.rb index 3411516319f..5ab3eeb3aff 100644 --- a/lib/gitlab/i18n.rb +++ b/lib/gitlab/i18n.rb @@ -12,15 +12,36 @@ module Gitlab AVAILABLE_LANGUAGES.keys end - def set_locale(current_user) - requested_locale = current_user&.preferred_language || ::I18n.default_locale - locale = FastGettext.set_locale(requested_locale) - ::I18n.locale = locale + def locale + FastGettext.locale end - def reset_locale + def locale=(locale_string) + requested_locale = locale_string || ::I18n.default_locale + new_locale = FastGettext.set_locale(requested_locale) + ::I18n.locale = new_locale + end + + def use_default_locale FastGettext.set_locale(::I18n.default_locale) ::I18n.locale = ::I18n.default_locale end + + def with_locale(locale_string) + original_locale = locale + + self.locale = locale_string + yield + ensure + self.locale = original_locale + end + + def with_user_locale(user, &block) + with_locale(user&.preferred_language, &block) + end + + def with_default_locale(&block) + with_locale(::I18n.default_locale, &block) + end end end |