1# frozen_string_literal: true 2 3module Gitlab 4 module I18n 5 extend self 6 7 AVAILABLE_LANGUAGES = { 8 'bg' => 'Bulgarian - български', 9 'cs_CZ' => 'Czech - čeština', 10 'da_DK' => 'Danish - dansk', 11 'de' => 'German - Deutsch', 12 'en' => 'English', 13 'eo' => 'Esperanto - esperanto', 14 'es' => 'Spanish - español', 15 'fil_PH' => 'Filipino', 16 'fr' => 'French - français', 17 'gl_ES' => 'Galician - galego', 18 'id_ID' => 'Indonesian - Bahasa Indonesia', 19 'it' => 'Italian - italiano', 20 'ja' => 'Japanese - 日本語', 21 'ko' => 'Korean - 한국어', 22 'nb_NO' => 'Norwegian (Bokmål) - norsk (bokmål)', 23 'nl_NL' => 'Dutch - Nederlands', 24 'pl_PL' => 'Polish - polski', 25 'pt_BR' => 'Portuguese (Brazil) - português (Brasil)', 26 'ro_RO' => 'Romanian - română', 27 'ru' => 'Russian - русский', 28 'tr_TR' => 'Turkish - Türkçe', 29 'uk' => 'Ukrainian - українська', 30 'zh_CN' => 'Chinese, Simplified - 简体中文', 31 'zh_HK' => 'Chinese, Traditional (Hong Kong) - 繁體中文 (香港)', 32 'zh_TW' => 'Chinese, Traditional (Taiwan) - 繁體中文 (台灣)' 33 }.freeze 34 private_constant :AVAILABLE_LANGUAGES 35 36 # Languages with less then MINIMUM_TRANSLATION_LEVEL% of available translations will not 37 # be available in the UI. 38 # https://gitlab.com/gitlab-org/gitlab/-/issues/221012 39 MINIMUM_TRANSLATION_LEVEL = 2 40 41 # Currently monthly updated manually by ~group::import PM. 42 # https://gitlab.com/gitlab-org/gitlab/-/issues/18923 43 TRANSLATION_LEVELS = { 44 'bg' => 0, 45 'cs_CZ' => 0, 46 'da_DK' => 51, 47 'de' => 15, 48 'en' => 100, 49 'eo' => 0, 50 'es' => 39, 51 'fil_PH' => 0, 52 'fr' => 12, 53 'gl_ES' => 0, 54 'id_ID' => 0, 55 'it' => 2, 56 'ja' => 35, 57 'ko' => 11, 58 'nb_NO' => 33, 59 'nl_NL' => 0, 60 'pl_PL' => 5, 61 'pt_BR' => 49, 62 'ro_RO' => 23, 63 'ru' => 25, 64 'tr_TR' => 15, 65 'uk' => 45, 66 'zh_CN' => 95, 67 'zh_HK' => 2, 68 'zh_TW' => 3 69 }.freeze 70 private_constant :TRANSLATION_LEVELS 71 72 def selectable_locales 73 AVAILABLE_LANGUAGES.reject do |code, _name| 74 percentage_translated_for(code) < MINIMUM_TRANSLATION_LEVEL 75 end 76 end 77 78 def percentage_translated_for(code) 79 TRANSLATION_LEVELS.fetch(code, 0) 80 end 81 82 def available_locales 83 AVAILABLE_LANGUAGES.keys 84 end 85 86 def locale 87 FastGettext.locale 88 end 89 90 def locale=(locale_string) 91 requested_locale = locale_string || ::I18n.default_locale 92 new_locale = FastGettext.set_locale(requested_locale) 93 ::I18n.locale = new_locale 94 end 95 96 def use_default_locale 97 FastGettext.set_locale(::I18n.default_locale) 98 ::I18n.locale = ::I18n.default_locale 99 end 100 101 def with_locale(locale_string) 102 original_locale = locale 103 104 self.locale = locale_string 105 yield 106 ensure 107 self.locale = original_locale 108 end 109 110 def with_user_locale(user, &block) 111 with_locale(user&.preferred_language, &block) 112 end 113 114 def with_default_locale(&block) 115 with_locale(::I18n.default_locale, &block) 116 end 117 end 118end 119