i18n.rb 1.5 KB
Newer Older
1 2
module Gitlab
  module I18n
3 4
    extend self

5
    AVAILABLE_LANGUAGES = {
6 7
      'en' => 'English',
      'es' => 'Español',
黄涛 已提交
8
      'de' => 'Deutsch',
9
      'fr' => 'Français',
10
      'pt_BR' => 'Português (Brasil)',
H
Huang Tao 已提交
11
      'zh_CN' => '简体中文',
12 13
      'zh_HK' => '繁體中文 (香港)',
      'zh_TW' => '繁體中文 (臺灣)',
14
      'bg' => 'български',
黄涛 已提交
15
      'ru' => 'Русский',
黄涛 已提交
16
      'eo' => 'Esperanto',
H
Huang Tao 已提交
17
      'it' => 'Italiano',
黄涛 已提交
18
      'uk' => 'Українська',
H
Huang Tao 已提交
19
      'ja' => '日本語',
20
      'ko' => '한국어',
21 22 23 24
      'nl_NL' => 'Nederlands',
      'tr_TR' => 'Türkçe',
      'id_ID' => 'Bahasa Indonesia',
      'fil_PH' => 'Filipino'
25
    }.freeze
26 27 28 29

    def available_locales
      AVAILABLE_LANGUAGES.keys
    end
30

31 32
    def locale
      FastGettext.locale
33 34
    end

35 36 37 38 39 40 41
    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
42
      FastGettext.set_locale(::I18n.default_locale)
43 44
      ::I18n.locale = ::I18n.default_locale
    end
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    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
62 63
  end
end