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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[48451] Select default language in languages settings page
Christophe Bliardf7b799e3ba7c
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 3d8526955d8..4020f51ba7e 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -310,6 +310,16 @@ module ApplicationHelper.sort_by(&:last)end+ def highlight_default_language(lang_options)+ lang_options.map do |(language_name, code)|+ if code == Setting.default_language+ [I18n.t('settings.language_name_being_default', language_name:), code, { disabled: true, checked: true }]+ else+ [language_name, code]+ end+ end+ end+def labelled_tabular_form_for(record, options = {}, &)options.reverse_merge!(builder: TabularFormBuilder, html: {})options[:html][:class] = 'form' unless options[:html].has_key?(:class)diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rbindex 365665de099..a8b9be3a5f7 100644--- a/app/helpers/settings_helper.rb+++ b/app/helpers/settings_helper.rb@@ -82,11 +82,11 @@ module SettingsHelperhidden = with_empty_unless_writable(setting) dohidden_field_tag("settings[#{setting}][]", '')end+ multiselect_choices = choices.map do |choice|+ setting_multiselect_choice(setting, choice, options)+ end- hidden +- choices.map do |choice|- setting_multiselect_choice(setting, choice, options)- end.join.html_safe # rubocop:disable Rails/OutputSafety+ safe_join([hidden, *multiselect_choices])endend@@ -247,20 +247,32 @@ module SettingsHelperchoice_options[:id] = "#{setting}_#{value}"content_tag(:label, class: 'form--label-with-check-box') do- styled_check_box_tag("settings[#{setting}][]", value,- Setting.send(setting).include?(value), choice_options) + text.to_s+ checked = Setting.send(setting).include?(value)+ check_box_tag = styled_check_box_tag("settings[#{setting}][]", value, checked, choice_options)++ # Adds an hidden field if the checkbox is explicitly checked and disabled+ # so the value can be submitted.+ if choice_options[:checked] && choice_options[:disabled] && writable_setting?(setting)+ hidden_checked_input = hidden_field_tag("settings[#{setting}][]", value, id: "#{choice_options[:id]}_hidden")+ end++ safe_join([check_box_tag, text, hidden_checked_input])endenddef disabled_setting_option(setting)- { disabled: !Setting.send(:"#{setting}_writable?") }+ { disabled: !writable_setting?(setting) }enddef with_empty_unless_writable(setting)- if Setting.send(:"#{setting}_writable?")+ if writable_setting?(setting)yieldelse''.html_safeendend++ def writable_setting?(setting)+ Setting.send(:"#{setting}_writable?")+ endenddiff --git a/app/views/admin/settings/languages_settings/show.html.erb b/app/views/admin/settings/languages_settings/show.html.erbindex cbee95da875..53ea58f8b87 100644--- a/app/views/admin/settings/languages_settings/show.html.erb+++ b/app/views/admin/settings/languages_settings/show.html.erb@@ -33,7 +33,7 @@ See COPYRIGHT and LICENSE files for more details.<section class="form--section"><div class="form--field" id="setting_available_languages">- <%= setting_multiselect :available_languages, all_lang_options_for_select %>+ <%= setting_multiselect :available_languages, highlight_default_language(all_lang_options_for_select) %></div><div class="form--field">diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 859f5630fc6..6bb8ae10a55 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -2702,6 +2702,7 @@ en:<br/>Leave empty to allow any file type to be uploaded.Multiple values allowed (one line for each value).+ language_name_being_default: '%{language_name} (default)'notifications:events_explanation: 'Governs for which event an email is sent out. Work packages are excluded from this list as the notifications for them can be configured specifically for every user.'delay_minutes_explanation: "Email sending can be delayed to allow users with configured in app notification to confirm the notification within the application before a mail is sent out. Users who read a notification within the application will not receive an email for the already read notification."diff --git a/spec/features/admin/languages_settings_spec.rb b/spec/features/admin/languages_settings_spec.rbnew file mode 100644index 00000000000..a8e17c96353--- /dev/null+++ b/spec/features/admin/languages_settings_spec.rb@@ -0,0 +1,79 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++RSpec.describe 'Languages settings page', js: true do+ current_user { create(:admin) }++ let(:languages_page) { Pages::Admin::SystemSettings::Languages.new }++ context 'when available languages setting is writable' do+ before do+ Setting.default_language = 'fr'+ Setting.available_languages = %w[de en]+ end++ it 'adds default language to available languages when saving' do+ languages_page.visit!++ expect(languages_page).to have_field('available_languages_de', checked: true)+ expect(languages_page).to have_field('available_languages_en', checked: true)+ expect(languages_page).to have_field('available_languages_fr', checked: true, disabled: true)+ expect(languages_page).to have_field('available_languages_ja', checked: false)++ expect { languages_page.save }+ .to change { Setting.find_by(name: 'available_languages').value }+ .from(%w[de en])+ .to(%w[de en fr])+ end+ end++ context 'when available languages setting is not writable (set by env var)', :settings_reset do+ before do+ Setting.default_language = 'fr'+ stub_const('ENV', { 'OPENPROJECT_AVAILABLE__LANGUAGES' => 'de en' })+ reset(:available_languages)+ end++ it 'does not change the available languages setting' do+ expect(Setting.available_languages_writable?).to be false++ languages_page.visit!++ expect(languages_page).to have_field('available_languages_de', checked: true, disabled: true)+ expect(languages_page).to have_field('available_languages_en', checked: true, disabled: true)+ expect(languages_page).to have_field('available_languages_fr', checked: true, disabled: true)+ expect(languages_page).to have_field('available_languages_ja', checked: false, disabled: true)++ languages_page.save++ expect(Setting.where(name: 'available_languages')).not_to exist+ end+ end+enddiff --git a/spec/support/pages/admin/system_settings/general.rb b/spec/support/pages/admin/system_settings/general.rbindex a5c95462ebd..bbb5449a08b 100644--- a/spec/support/pages/admin/system_settings/general.rb+++ b/spec/support/pages/admin/system_settings/general.rb@@ -26,18 +26,14 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'support/pages/page'+require 'support/pages/admin/system_settings/page'module Pages::Admin::SystemSettings- class General < ::Pages::Page+ class General < Pagedef path"/admin/settings/general"end- def toast_type- :rails- end-def welcome_text_editorComponents::WysiwygEditor.new welcome_text_selectorend@@ -45,10 +41,5 @@ module Pages::Admin::SystemSettingsdef welcome_text_selector'ckeditor-augmented-textarea[textarea-selector="#settings_welcome_text"]'end-- def press_save_button- scroll_to(:bottom)- click_button('Save')- endendenddiff --git a/spec/support/pages/admin/system_settings/languages.rb b/spec/support/pages/admin/system_settings/languages.rbnew file mode 100644index 00000000000..6a1b1aa7ea4--- /dev/null+++ b/spec/support/pages/admin/system_settings/languages.rb@@ -0,0 +1,44 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require 'support/pages/admin/system_settings/page'++module Pages::Admin::SystemSettings+ class Languages < Page+ def path+ admin_settings_languages_path+ end++ def save+ press_save_button+ expect_and_dismiss_toaster+ end+ end+enddiff --git a/spec/support/pages/admin/system_settings/page.rb b/spec/support/pages/admin/system_settings/page.rbnew file mode 100644index 00000000000..be3137f7ccd--- /dev/null+++ b/spec/support/pages/admin/system_settings/page.rb@@ -0,0 +1,45 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require 'support/pages/page'++module Pages::Admin::SystemSettings+ class Page < ::Pages::Page+ def toast_type+ :rails+ end++ def press_save_button+ scroll_to(:bottom)+ click_button('Save')+ self+ end+ end+end