debian-mirror-gitlab/lib/gitlab/i18n.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

164 lines
4.3 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module I18n
extend self
AVAILABLE_LANGUAGES = {
2020-05-24 23:13:21 +05:30
'bg' => 'Bulgarian - български',
'cs_CZ' => 'Czech - čeština',
2021-11-11 11:23:49 +05:30
'da_DK' => 'Danish - dansk',
2020-05-24 23:13:21 +05:30
'de' => 'German - Deutsch',
2017-08-17 22:00:37 +05:30
'en' => 'English',
2020-05-24 23:13:21 +05:30
'eo' => 'Esperanto - esperanto',
'es' => 'Spanish - español',
2018-11-08 19:23:39 +05:30
'fil_PH' => 'Filipino',
2020-05-24 23:13:21 +05:30
'fr' => 'French - français',
'gl_ES' => 'Galician - galego',
'id_ID' => 'Indonesian - Bahasa Indonesia',
'it' => 'Italian - italiano',
'ja' => 'Japanese - 日本語',
'ko' => 'Korean - 한국어',
2021-11-11 11:23:49 +05:30
'nb_NO' => 'Norwegian (Bokmål) - norsk (bokmål)',
2020-05-24 23:13:21 +05:30
'nl_NL' => 'Dutch - Nederlands',
'pl_PL' => 'Polish - polski',
'pt_BR' => 'Portuguese (Brazil) - português (Brasil)',
2021-11-11 11:23:49 +05:30
'ro_RO' => 'Romanian - română',
'ru' => 'Russian - русский',
2022-08-13 15:12:31 +05:30
'si_LK' => 'Sinhalese - සිංහල',
2020-05-24 23:13:21 +05:30
'tr_TR' => 'Turkish - Türkçe',
'uk' => 'Ukrainian - українська',
'zh_CN' => 'Chinese, Simplified - 简体中文',
'zh_HK' => 'Chinese, Traditional (Hong Kong) - 繁體中文 (香港)',
'zh_TW' => 'Chinese, Traditional (Taiwan) - 繁體中文 (台灣)'
2017-08-17 22:00:37 +05:30
}.freeze
2021-06-08 01:23:25 +05:30
private_constant :AVAILABLE_LANGUAGES
# Languages with less then MINIMUM_TRANSLATION_LEVEL% of available translations will not
# be available in the UI.
# https://gitlab.com/gitlab-org/gitlab/-/issues/221012
MINIMUM_TRANSLATION_LEVEL = 2
# Currently monthly updated manually by ~group::import PM.
# https://gitlab.com/gitlab-org/gitlab/-/issues/18923
TRANSLATION_LEVELS = {
2021-10-27 15:23:28 +05:30
'bg' => 0,
2021-11-11 11:23:49 +05:30
'cs_CZ' => 0,
2023-06-20 00:43:36 +05:30
'da_DK' => 32,
2023-05-27 22:25:52 +05:30
'de' => 15,
2021-06-08 01:23:25 +05:30
'en' => 100,
2021-10-27 15:23:28 +05:30
'eo' => 0,
2023-05-27 22:25:52 +05:30
'es' => 32,
2021-10-27 15:23:28 +05:30
'fil_PH' => 0,
2023-04-23 21:23:45 +05:30
'fr' => 99,
2021-10-27 15:23:28 +05:30
'gl_ES' => 0,
2021-06-08 01:23:25 +05:30
'id_ID' => 0,
2022-06-21 17:19:12 +05:30
'it' => 1,
2023-06-20 00:43:36 +05:30
'ja' => 32,
2023-05-27 22:25:52 +05:30
'ko' => 19,
'nb_NO' => 22,
2021-10-27 15:23:28 +05:30
'nl_NL' => 0,
2022-11-25 23:54:43 +05:30
'pl_PL' => 3,
2023-05-27 22:25:52 +05:30
'pt_BR' => 56,
2023-06-20 00:43:36 +05:30
'ro_RO' => 87,
'ru' => 24,
2023-05-27 22:25:52 +05:30
'si_LK' => 10,
2023-03-17 16:20:25 +05:30
'tr_TR' => 10,
2023-05-27 22:25:52 +05:30
'uk' => 53,
2023-06-20 00:43:36 +05:30
'zh_CN' => 99,
2022-08-27 11:52:29 +05:30
'zh_HK' => 1,
2023-04-23 21:23:45 +05:30
'zh_TW' => 98
2021-06-08 01:23:25 +05:30
}.freeze
private_constant :TRANSLATION_LEVELS
2017-08-17 22:00:37 +05:30
2023-03-04 22:38:38 +05:30
def selectable_locales(minimum_translation_level = MINIMUM_TRANSLATION_LEVEL)
2021-06-08 01:23:25 +05:30
AVAILABLE_LANGUAGES.reject do |code, _name|
2023-03-04 22:38:38 +05:30
percentage_translated_for(code) < minimum_translation_level
2021-06-08 01:23:25 +05:30
end
end
def percentage_translated_for(code)
TRANSLATION_LEVELS.fetch(code, 0)
2020-10-24 23:57:45 +05:30
end
2017-08-17 22:00:37 +05:30
def available_locales
AVAILABLE_LANGUAGES.keys
end
2017-09-10 17:25:29 +05:30
def locale
FastGettext.locale
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
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-08-17 22:00:37 +05:30
FastGettext.set_locale(::I18n.default_locale)
::I18n.locale = ::I18n.default_locale
end
2017-09-10 17:25:29 +05:30
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
2023-04-23 21:23:45 +05:30
def setup(domain:, default_locale:)
2023-05-27 22:25:52 +05:30
custom_pluralization
2023-04-23 21:23:45 +05:30
setup_repositories(domain)
setup_default_locale(default_locale)
end
private
2023-05-27 22:25:52 +05:30
def custom_pluralization
Gitlab::I18n::Pluralization.install_on(FastGettext)
end
2023-04-23 21:23:45 +05:30
def setup_repositories(domain)
translation_repositories = [
(po_repository(domain, 'jh/locale') if Gitlab.jh?),
po_repository(domain, 'locale')
].compact
FastGettext.add_text_domain(
domain,
type: :chain,
chain: translation_repositories,
ignore_fuzzy: true
)
FastGettext.default_text_domain = domain
end
def po_repository(domain, path)
FastGettext::TranslationRepository.build(
domain,
path: Rails.root.join(path),
type: :po,
ignore_fuzzy: true
)
end
def setup_default_locale(locale)
FastGettext.default_locale = locale
FastGettext.default_available_locales = available_locales
::I18n.available_locales = available_locales
end
2017-08-17 22:00:37 +05:30
end
end