Module: Gitlab::I18n

Extended by:
I18n
Included in:
I18n
Defined in:
lib/gitlab/i18n.rb,
lib/gitlab/i18n/po_linter.rb,
lib/gitlab/i18n/metadata_entry.rb,
lib/gitlab/i18n/translation_entry.rb

Defined Under Namespace

Classes: MetadataEntry, PoLinter, TranslationEntry

Constant Summary collapse

NOT_AVAILABLE_IN_UI =

Languages with less then 2% of available translations will not be available in the UI. gitlab.com/gitlab-org/gitlab/-/issues/221012

%w[
  fil_PH
  pl_PL
  nl_NL
  id_ID
  cs_CZ
  bg
  eo
  gl_ES
].freeze
AVAILABLE_LANGUAGES =
{
  'bg' => 'Bulgarian - български',
  'cs_CZ' => 'Czech - čeština',
  'de' => 'German - Deutsch',
  'en' => 'English',
  'eo' => 'Esperanto - esperanto',
  'es' => 'Spanish - español',
  'fil_PH' => 'Filipino',
  'fr' => 'French - français',
  'gl_ES' => 'Galician - galego',
  'id_ID' => 'Indonesian - Bahasa Indonesia',
  'it' => 'Italian - italiano',
  'ja' => 'Japanese - 日本語',
  'ko' => 'Korean - 한국어',
  'nl_NL' => 'Dutch - Nederlands',
  'pl_PL' => 'Polish - polski',
  'pt_BR' => 'Portuguese (Brazil) - português (Brasil)',
  'ru' => 'Russian - Русский',
  'tr_TR' => 'Turkish - Türkçe',
  'uk' => 'Ukrainian - українська',
  'zh_CN' => 'Chinese, Simplified - 简体中文',
  'zh_HK' => 'Chinese, Traditional (Hong Kong) - 繁體中文 (香港)',
  'zh_TW' => 'Chinese, Traditional (Taiwan) - 繁體中文 (台灣)'
}.freeze

Instance Method Summary collapse

Instance Method Details

#available_localesObject


50
51
52
# File 'lib/gitlab/i18n.rb', line 50

def available_locales
  AVAILABLE_LANGUAGES.keys
end

#localeObject


54
55
56
# File 'lib/gitlab/i18n.rb', line 54

def locale
  FastGettext.locale
end

#locale=(locale_string) ⇒ Object


58
59
60
61
62
# File 'lib/gitlab/i18n.rb', line 58

def locale=(locale_string)
  requested_locale = locale_string || ::I18n.default_locale
  new_locale = FastGettext.set_locale(requested_locale)
  ::I18n.locale = new_locale
end

#selectable_localesObject


46
47
48
# File 'lib/gitlab/i18n.rb', line 46

def selectable_locales
  AVAILABLE_LANGUAGES.reject { |key, _value| NOT_AVAILABLE_IN_UI.include? key }
end

#use_default_localeObject


64
65
66
67
# File 'lib/gitlab/i18n.rb', line 64

def use_default_locale
  FastGettext.set_locale(::I18n.default_locale)
  ::I18n.locale = ::I18n.default_locale
end

#with_default_locale(&block) ⇒ Object


82
83
84
# File 'lib/gitlab/i18n.rb', line 82

def with_default_locale(&block)
  with_locale(::I18n.default_locale, &block)
end

#with_locale(locale_string) ⇒ Object


69
70
71
72
73
74
75
76
# File 'lib/gitlab/i18n.rb', line 69

def with_locale(locale_string)
  original_locale = locale

  self.locale = locale_string
  yield
ensure
  self.locale = original_locale
end

#with_user_locale(user, &block) ⇒ Object


78
79
80
# File 'lib/gitlab/i18n.rb', line 78

def with_user_locale(user, &block)
  with_locale(user&.preferred_language, &block)
end