Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[48451] Include default language in valid languages

Christophe Bliard52303a1fe96f
diff --git a/app/components/settings/text_setting_component.html.erb b/app/components/settings/text_setting_component.html.erb
index 4347267e48e..30677e5cc7a 100644
--- a/app/components/settings/text_setting_component.html.erb
+++ b/app/components/settings/text_setting_component.html.erb
@@ -45,7 +45,7 @@ See COPYRIGHT and LICENSE files for more details.
</div>
</div>
-<% Setting.available_languages.each do |lang| %>
+<% Redmine::I18n.valid_languages.each do |lang| %>
<%=
hidden_field_tag(
"settings[#{name}][#{lang}]",
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 06b068c61c3..118a59e423c 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -212,7 +212,7 @@ class ApplicationController < ActionController::Base
end
def reset_i18n_fallbacks
- fallbacks = [I18n.default_locale] + Setting.available_languages.map(&:to_sym)
+ fallbacks = [I18n.default_locale] + Redmine::I18n.valid_languages.map(&:to_sym)
return if I18n.fallbacks.defaults == fallbacks
I18n.fallbacks = nil
diff --git a/config/initializers/i18n.rb b/config/initializers/i18n.rb
index 1382533fd72..03cbf2356e8 100644
--- a/config/initializers/i18n.rb
+++ b/config/initializers/i18n.rb
@@ -11,7 +11,7 @@ Rails.application.reloader.to_prepare do
# to the default locale.
# When other locales are available, fall back to them.
if Setting.table_exists? # don't want to prevent migrations
- defaults = Set.new I18n.fallbacks.defaults + Setting.available_languages.map(&:to_sym)
+ defaults = Set.new(I18n.fallbacks.defaults + Redmine::I18n.valid_languages.map(&:to_sym))
I18n.fallbacks.defaults = defaults
end
end
diff --git a/lib_static/redmine/i18n.rb b/lib_static/redmine/i18n.rb
index 5f1901598ef..49d7a6c56b6 100644
--- a/lib_static/redmine/i18n.rb
+++ b/lib_static/redmine/i18n.rb
@@ -45,6 +45,10 @@ module Redmine
.sort
end
+ def self.valid_languages
+ all_languages & (Setting.available_languages + [Setting.default_language])
+ end
+
def l_or_humanize(s, options = {})
k = "#{options[:prefix]}#{s}".to_sym
::I18n.t(k, default: s.to_s.humanize)
@@ -152,7 +156,7 @@ module Redmine
end
def valid_languages
- all_languages & Setting.available_languages
+ Redmine::I18n.valid_languages
end
def all_languages
diff --git a/spec/features/auth/omniauth_spec.rb b/spec/features/auth/omniauth_spec.rb
index 85f464ebad7..3ca237e7590 100644
--- a/spec/features/auth/omniauth_spec.rb
+++ b/spec/features/auth/omniauth_spec.rb
@@ -175,8 +175,7 @@ RSpec.describe 'Omniauth authentication' do
context 'register on the fly',
with_settings: {
self_registration?: true,
- self_registration: Setting::SelfRegistration.automatic,
- available_languages: ['en']
+ self_registration: Setting::SelfRegistration.automatic
} do
let(:user) do
User.new(force_password_change: false,
diff --git a/spec/features/localization_spec.rb b/spec/features/localization_spec.rb
index 30faf36a478..a21ccc20f3a 100644
--- a/spec/features/localization_spec.rb
+++ b/spec/features/localization_spec.rb
@@ -31,20 +31,28 @@ require 'spec_helper'
RSpec.describe 'Localization', with_settings: { login_required?: false,
available_languages: %w[de en],
default_language: 'en' } do
- it 'set localization' do
- Capybara.current_session.driver.header('Accept-Language', 'de,de-de;q=0.8,en-us;q=0.5,en;q=0.3')
+ context 'with a HTTP header Accept-Language having a valid supported language' do
+ before do
+ Capybara.current_session.driver.header('Accept-Language', 'de,de-de;q=0.8,en-us;q=0.5,en;q=0.3')
+ end
- # a french user
- visit projects_path
+ it 'uses the language from HTTP header Accept-Language' do
+ visit projects_path
- expect(page)
- .to have_content('Projekte')
+ expect(page)
+ .to have_content('Projekte')
+ end
+ end
+
+ context 'with a HTTP header Accept-Language having an unsupported language' do
+ before do
+ Capybara.current_session.driver.header('Accept-Language', 'zz')
+ end
- # not a supported language: default language should be used
- Capybara.current_session.driver.header('Accept-Language', 'zz')
- visit projects_path
+ it 'uses the default language configured in administration' do
+ visit projects_path
- expect(page)
- .to have_content('Projects')
+ expect(page).to have_content('Projects')
+ end
end
end
diff --git a/spec/lib/redmine/i18n_spec.rb b/spec/lib/redmine/i18n_spec.rb
index 3331f9b3213..4057fa8a710 100644
--- a/spec/lib/redmine/i18n_spec.rb
+++ b/spec/lib/redmine/i18n_spec.rb
@@ -97,17 +97,31 @@ module OpenProject
end
describe 'valid_languages' do
- it 'allows only languages that are available' do
+ it 'allows languages that are available' do
with_settings(available_languages: ['en'])
expect(valid_languages).to eq ['en']
end
+ it 'allows language which is not in available languages list but is the default language' do
+ with_settings(available_languages: ['en'], default_language: 'fr')
+
+ expect(valid_languages).to eq ['en', 'fr']
+ end
+
it 'allows only languages that exist' do
with_settings(available_languages: ['en', 'de', 'klingon'])
expect(valid_languages).to contain_exactly('en', 'de')
end
+
+ it 'is sorted alphabetically' do
+ with_settings(available_languages: ['de', 'en'], default_language: 'fr')
+ expect(valid_languages).to eq(valid_languages.sort)
+
+ with_settings(available_languages: ['en', 'fr'], default_language: 'de')
+ expect(valid_languages).to eq(valid_languages.sort)
+ end
end
describe 'set_language_if_valid' do
@@ -141,11 +155,11 @@ module OpenProject
describe 'find_language' do
before do
- with_settings(available_languages: ['de'])
+ with_settings(available_languages: ['de'], default_language: 'en')
end
- it 'is nil if language is not active' do
- expect(find_language(:en)).to be_nil
+ it 'is nil if language is not available nor the default language' do
+ expect(find_language(:fr)).to be_nil
end
it 'is nil if no language is given' do
@@ -153,14 +167,21 @@ module OpenProject
expect(find_language(nil)).to be_nil
end
- it 'is the language if it is active' do
+ it 'is the language if it is in available languages' do
expect(find_language(:de)).to eq 'de'
expect(find_language('de')).to eq 'de'
end
- it 'can be found by uppercase if it is active' do
+ it 'is the language if it is the default language' do
+ expect(find_language(:en)).to eq 'en'
+ expect(find_language('en')).to eq 'en'
+ end
+
+ it 'can be found by uppercase' do
expect(find_language(:DE)).to eq 'de'
expect(find_language('DE')).to eq 'de'
+ expect(find_language(:EN)).to eq 'en'
+ expect(find_language('EN')).to eq 'en'
end
it 'is nil if non valid string is passed' do