2017-04-13 00:23:02 -04:00
|
|
|
module Gitlab
|
|
|
|
module I18n
|
2017-05-03 00:36:36 -04:00
|
|
|
extend self
|
|
|
|
|
2017-04-20 00:19:24 -04:00
|
|
|
AVAILABLE_LANGUAGES = {
|
2017-05-04 19:44:19 -04:00
|
|
|
'en' => 'English',
|
|
|
|
'es' => 'Español',
|
2017-05-23 08:31:09 -04:00
|
|
|
'de' => 'Deutsch',
|
2017-06-06 00:59:55 -04:00
|
|
|
'pt_BR' => 'Português(Brasil)',
|
2017-05-26 23:34:55 -04:00
|
|
|
'zh_CN' => '简体中文',
|
|
|
|
'zh_HK' => '繁體中文(香港)',
|
2017-06-06 22:10:49 -04:00
|
|
|
'zh_TW' => '繁體中文(臺灣)',
|
|
|
|
'bg' => 'български'
|
2017-04-20 00:19:24 -04:00
|
|
|
}.freeze
|
2017-05-03 00:36:36 -04:00
|
|
|
|
|
|
|
def available_locales
|
|
|
|
AVAILABLE_LANGUAGES.keys
|
|
|
|
end
|
2017-05-03 22:05:38 -04:00
|
|
|
|
2017-05-25 11:22:45 -04:00
|
|
|
def locale
|
|
|
|
FastGettext.locale
|
2017-05-03 22:05:38 -04:00
|
|
|
end
|
|
|
|
|
2017-05-25 11:22:45 -04:00
|
|
|
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
|
2017-05-04 19:44:19 -04:00
|
|
|
FastGettext.set_locale(::I18n.default_locale)
|
2017-05-03 22:05:38 -04:00
|
|
|
::I18n.locale = ::I18n.default_locale
|
|
|
|
end
|
2017-05-25 11:22:45 -04:00
|
|
|
|
|
|
|
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
|
2017-04-13 00:23:02 -04:00
|
|
|
end
|
|
|
|
end
|