Commit 90f218ad authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch 'trans-levels-master-patch-26507' into 'master'

Show translation levels in UI

See merge request gitlab-org/gitlab!59420
parents 5551b2c8 7aa06e08
......@@ -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')
......@@ -431,7 +431,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: Add the translation level for each language in the user profile language selector
merge_request: 59420
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 }
@selectable_locales ||= 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
......
......@@ -37795,6 +37795,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
......@@ -63,17 +64,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,23 @@ RSpec.describe PreferencesHelper do
end
end
describe '#language_choices' do
it 'lists all the selectable language options with their translation percent' do
stub_const(
'Gitlab::I18n::TRANSLATION_LEVELS',
'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,21 @@
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
it 'does not return languages with low translation levels' do
stub_const(
'Gitlab::I18n::TRANSLATION_LEVELS',
'pt_BR' => 0,
'en' => 100,
'es' => 65
)
expect(described_class.selectable_locales).to eq({
'en' => 'English',
'es' => 'Spanish - español'
})
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