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

MINIMUM_TRANSLATION_LEVEL =

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

2

Instance Method Summary collapse

Instance Method Details

#available_localesObject


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

def available_locales
  AVAILABLE_LANGUAGES.keys
end

#localeObject


86
87
88
# File 'lib/gitlab/i18n.rb', line 86

def locale
  FastGettext.locale
end

#locale=(locale_string) ⇒ Object


90
91
92
93
94
# File 'lib/gitlab/i18n.rb', line 90

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

#percentage_translated_for(code) ⇒ Object


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

def percentage_translated_for(code)
  TRANSLATION_LEVELS.fetch(code, 0)
end

#selectable_localesObject


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

def selectable_locales
  AVAILABLE_LANGUAGES.reject do |code, _name|
    percentage_translated_for(code) < MINIMUM_TRANSLATION_LEVEL
  end
end

#use_default_localeObject


96
97
98
99
# File 'lib/gitlab/i18n.rb', line 96

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

#with_default_locale(&block) ⇒ Object


114
115
116
# File 'lib/gitlab/i18n.rb', line 114

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

#with_locale(locale_string) ⇒ Object


101
102
103
104
105
106
107
108
# File 'lib/gitlab/i18n.rb', line 101

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


110
111
112
# File 'lib/gitlab/i18n.rb', line 110

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