Commit 50fd1ade authored by Thong Kuah's avatar Thong Kuah

Merge branch 'kassio/add-translation-percentage-level-to-ui' into 'master'

i18n: Show translation levels for each language in the UI

See merge request gitlab-org/gitlab!60905
parents 74da7a21 563a1a00
......@@ -4,8 +4,8 @@
module PreferencesHelper
def layout_choices
[
['Fixed', :fixed],
['Fluid', :fluid]
['Fixed', :fixed],
['Fluid', :fluid]
]
end
......@@ -76,7 +76,7 @@ module PreferencesHelper
def language_choices
options_for_select(
Gitlab::I18n.selectable_locales.map(&:reverse).sort,
selectable_locales_with_translation_level.sort,
current_user.preferred_language
)
end
......@@ -107,6 +107,18 @@ module PreferencesHelper
def default_first_day_of_week
first_day_of_week_choices.rassoc(Gitlab::CurrentSettings.first_day_of_week).first
end
def selectable_locales_with_translation_level
Gitlab::I18n.selectable_locales.map do |code, language|
[
s_("i18n|%{language} (%{percent_translated}%% translated)") % {
language: language,
percent_translated: Gitlab::I18n.percentage_translated_for(code)
},
code
]
end
end
end
PreferencesHelper.prepend_if_ee('EE::PreferencesHelper')
......@@ -433,7 +433,7 @@ class User < ApplicationRecord
def preferred_language
read_attribute('preferred_language') ||
I18n.default_locale.to_s.presence_in(Gitlab::I18n::AVAILABLE_LANGUAGES.keys) ||
I18n.default_locale.to_s.presence_in(Gitlab::I18n.available_locales) ||
'en'
end
......
---
title: 'i18n: Show translation levels for each language in the UI'
merge_request: 60905
author:
type: changed
......@@ -78,3 +78,10 @@ recreate it with the following steps:
1. Select the `gitlab-org/gitlab` repository.
1. In `Select Branches for Translation`, select `master`.
1. Ensure the `Service Branch Name` is `master-i18n`.
## Manually update the translation levels
There's no automated way to pull the translation levels from CrowdIn, to display
this information in the language selection dropdown. Therefore, the translation
levels are hard-coded in the `TRANSLATION_LEVELS` constant in [`i18n.rb`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/i18n.rb),
and must be regularly updated.
......@@ -4,20 +4,6 @@ module Gitlab
module I18n
extend self
# Languages with less then 2% of available translations will not
# be available in the UI.
# https://gitlab.com/gitlab-org/gitlab/-/issues/221012
NOT_AVAILABLE_IN_UI = %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',
......@@ -42,9 +28,49 @@ module Gitlab
'zh_HK' => 'Chinese, Traditional (Hong Kong) - 繁體中文 (香港)',
'zh_TW' => 'Chinese, Traditional (Taiwan) - 繁體中文 (台灣)'
}.freeze
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 = {
'bg' => 1,
'cs_CZ' => 1,
'de' => 20,
'en' => 100,
'eo' => 1,
'es' => 44,
'fil_PH' => 1,
'fr' => 14,
'gl_ES' => 1,
'id_ID' => 0,
'it' => 3,
'ja' => 49,
'ko' => 15,
'nl_NL' => 1,
'pl_PL' => 1,
'pt_BR' => 23,
'ru' => 34,
'tr_TR' => 18,
'uk' => 46,
'zh_CN' => 78,
'zh_HK' => 3,
'zh_TW' => 4
}.freeze
private_constant :TRANSLATION_LEVELS
def selectable_locales
AVAILABLE_LANGUAGES.reject { |key, _value| NOT_AVAILABLE_IN_UI.include? key }
AVAILABLE_LANGUAGES.reject do |code, _name|
percentage_translated_for(code) < MINIMUM_TRANSLATION_LEVEL
end
end
def percentage_translated_for(code)
TRANSLATION_LEVELS.fetch(code, 0)
end
def available_locales
......
......@@ -38107,6 +38107,9 @@ msgstr ""
msgid "https://your-bitbucket-server"
msgstr ""
msgid "i18n|%{language} (%{percent_translated}%% translated)"
msgstr ""
msgid "image diff"
msgstr ""
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'User edit preferences profile', :js do
include StubLanguagesTranslationPercentage
# Empty value doesn't change the levels
let(:language_percentage_levels) { nil }
let(:user) { create(:user) }
before do
stub_languages_translation_percentage(language_percentage_levels)
stub_feature_flags(user_time_settings: true)
sign_in(user)
visit(profile_preferences_path)
......@@ -63,17 +69,4 @@ RSpec.describe 'User edit preferences profile', :js do
expect(page).to have_content('Failed to save preferences.')
end
end
describe 'User language' do
let(:user) { create(:user, preferred_language: :es) }
it 'shows the user preferred language by default' do
expect(page).to have_select(
'user[preferred_language]',
selected: 'Spanish - español',
options: Gitlab::I18n.selectable_locales.values,
visible: :all
)
end
end
end
......@@ -121,6 +121,20 @@ RSpec.describe PreferencesHelper do
end
end
describe '#language_choices' do
include StubLanguagesTranslationPercentage
it 'lists all the selectable language options with their translation percent' do
stub_languages_translation_percentage(en: 100, es: 65)
stub_user(preferred_language: :en)
expect(helper.language_choices).to eq([
'<option selected="selected" value="en">English (100% translated)</option>',
'<option value="es">Spanish - español (65% translated)</option>'
].join("\n"))
end
end
def stub_user(messages = {})
if messages.empty?
allow(helper).to receive(:current_user).and_return(nil)
......
......@@ -3,13 +3,18 @@
require 'spec_helper'
RSpec.describe Gitlab::I18n do
let(:user) { create(:user, preferred_language: 'es') }
let(:user) { create(:user, preferred_language: :es) }
describe '.selectable_locales' do
it 'does not return languages that should not be available in the UI' do
Gitlab::I18n::NOT_AVAILABLE_IN_UI.each do |language|
expect(described_class.selectable_locales).not_to include(language)
end
include StubLanguagesTranslationPercentage
it 'does not return languages with low translation levels' do
stub_languages_translation_percentage(pt_BR: 0, en: 100, es: 65)
expect(described_class.selectable_locales).to eq({
'en' => 'English',
'es' => 'Spanish - español'
})
end
end
......
# frozen_string_literal: true
module StubLanguagesTranslationPercentage
# Stubs the translation percentage of the i18n languages
# - When a `blank?` list is given no stubbing is done;
# - When the list is not empty, the languages in the list
# are stubbed with the given values, any other language
# will have the translation percent set to 0;
def stub_languages_translation_percentage(list = {})
return if list.blank?
expect(Gitlab::I18n)
.to receive(:percentage_translated_for)
.at_least(:once)
.and_wrap_original do |_original, code|
list.with_indifferent_access[code].to_i
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment