diff options
author | Douwe Maan <douwe@gitlab.com> | 2017-05-25 18:05:06 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2017-05-25 18:05:06 +0000 |
commit | b3b6c78112c97df32c406f7dd18031e540df3297 (patch) | |
tree | 4abd36a4157b40fd3a6bdab227ca00b415a2f801 /lib | |
parent | 43c3614ebfd3a224b9db5718f19aa1e1eb7e7e61 (diff) | |
parent | 5c921809cd86e1031a6a5075da142fef3bb01d6d (diff) | |
download | gitlab-ce-b3b6c78112c97df32c406f7dd18031e540df3297.tar.gz |
Merge branch '32748-emails-are-being-sent-with-the-wrong-language' into 'master'
Bugfix: Always use the default language when generating emails.
Closes #32748
See merge request !11662
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/i18n.rb | 31 |
2 files changed, 28 insertions, 7 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 52cd7cbe3db..ac113c5200d 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -45,9 +45,9 @@ module API end before { allow_access_with_scope :api } - before { Gitlab::I18n.set_locale(current_user) } + before { Gitlab::I18n.locale = current_user&.preferred_language } - after { Gitlab::I18n.reset_locale } + after { Gitlab::I18n.use_default_locale } rescue_from Gitlab::Access::AccessDeniedError do rack_response({ 'message' => '403 Forbidden' }.to_json, 403) 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 |