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
Introduce setting multi-lang helper
Oliver Günther66a94b2be297
diff --git a/app/forms/settings/form_decorator.rb b/app/forms/settings/form_decorator.rbnew file mode 100644index 00000000000..0192dfcecd9--- /dev/null+++ b/app/forms/settings/form_decorator.rb@@ -0,0 +1,172 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++# Decorates a form object to provide a more convenient interface for+# rendering settings.+#+# It automatically sets the label, value, and disabled properties from the+# setting name and its definition attributes.+module Settings+ class FormDecorator+ include ::SettingsHelper+ include ::ApplicationHelper+ include FormHelper++ attr_reader :form++ # Initializes a new Settings::FormDecorator+ #+ # @param form [Object] The form object to be decorated+ def initialize(form)+ @form = form+ end++ def method_missing(method, ...)+ form.send(method, ...)+ end++ def respond_to_missing?(method, include_private = false)+ form.respond_to?(method, include_private)+ end++ # Creates a text field input for a setting.+ #+ # The text field label is set from translating the key "setting_<name>".+ #+ # Any options passed to this method will override the default options.+ #+ # @param name [Symbol] The name of the setting+ # @param options [Hash] Additional options for the text field+ # @return [Object] The text field input+ def text_field(name:, **options)+ options.reverse_merge!(+ label: setting_label(name),+ value: setting_value(name),+ disabled: setting_disabled?(name)+ )+ form.text_field(name:, **options)+ end++ # Creates a check box input for a setting.+ #+ # The check box label is set from translating the key "setting_<name>".+ #+ # Any options passed to this method will override the default options.+ #+ # @param name [Symbol] The name of the setting+ # @param options [Hash] Additional options for the check box+ # @return [Object] The check box input+ def check_box(name:, **options)+ options.reverse_merge!(+ label: setting_label(name),+ checked: setting_value(name),+ disabled: setting_disabled?(name)+ )+ form.check_box(name:, **options)+ end++ # Creates a radio button group for a setting.+ #+ # The radio button group label is set from translating the key+ # "setting_<name>". The radio button label are set from translating the+ # key "setting_<name>_<value>". The caption is set from translating the+ # key "setting_<name>_<value>_caption_html", which will be rendered as HTML,+ # or "setting_<name>_<value>_caption", or nothing if none of the above+ # are defined.+ #+ # Any options passed to this method will override the default options.+ #+ # @param name [Symbol] The name of the setting+ # @param values [Array] The values for the radio buttons. Default to the+ # setting's allowed values.+ # @param disabled [Boolean] Force the radio button group to be disabled when+ # true, will be disabled if the setting is not writable when false (default)+ # @param button_options [Hash] Options for individual radio buttons+ # @param options [Hash] Additional options for the radio button group+ # @return [Object] The radio button group+ def radio_button_group(name:, values: [], disabled: false, button_options: {}, **options)+ values = values.presence || setting_allowed_values(name)+ radio_group_options = options.reverse_merge(+ label: setting_label(name),+ disabled: disabled || setting_disabled?(name)+ )+ form.radio_button_group(+ name:,+ **radio_group_options+ ) do |radio_group|+ values.each do |value|+ radio_group.radio_button(+ **button_options.reverse_merge(+ value:,+ checked: setting_value(name) == value,+ autocomplete: "off",+ label: setting_label(name, value),+ caption: setting_caption(name, value)+ )+ )+ end+ end+ end++ def multi_language_text_select(name:, current_language: I18n.locale.to_s)+ # Add select list to switch+ form.select_list(+ name: :"#{name}_lang", # Should be excluded by settings params+ input_width: :small,+ id: "lang-for-#{name}",+ class: "lang-select-switch",+ label: setting_label(name),+ caption: setting_caption(name),+ include_blank: false+ ) do |select|+ lang_options_for_select(false).each do |label, value|+ select.option(+ value:,+ label:,+ selected: value == current_language+ )+ end+ end++ form.fields_for(name) do |builder|+ MultiLangForm.new(builder, name:, current_language:)+ end+ end++ # Creates a save button to submit the form+ #+ # @return [Object] The submit button+ def submit+ form.submit(name: "submit",+ label: I18n.t("button_save"),+ scheme: :primary)+ end+ end+enddiff --git a/app/forms/settings/form_helper.rb b/app/forms/settings/form_helper.rbnew file mode 100644index 00000000000..c203e84443e--- /dev/null+++ b/app/forms/settings/form_helper.rb@@ -0,0 +1,90 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++module Settings+ module FormHelper+ # Returns a translated string for a setting name.+ #+ # The translation key is "setting_<name>". Add additional names to the key+ # to allow for translations with more context:+ # "setting_<name>_<param2>_<param3>_...".+ #+ # @param names [Array<String | Symbol>] The name(s) of the setting+ # @return [String] The translated label+ def setting_label(*names)+ I18n.t("setting_#{names.join('_')}")+ end++ # Generates an HTML-safe caption for a setting.+ #+ # The translation key is "setting_<name>_caption". If not present, it will+ # return nil.+ #+ # The translation will be marked as html_safe automatically if it ends with+ # "_html", allowing to have HTML in the caption.+ #+ # Add additional names to the key to allow for translations with more context:+ # "setting_<name>_<context>_caption_html" for instance.+ #+ # @param names [Array<Symbol>] The name(s) of the setting+ # @return [String] The translated HTML-safe caption+ def setting_caption(*names)+ I18n.t("setting_#{names.join('_')}_caption_html", default: nil)&.html_safe \+ || I18n.t("setting_#{names.join('_')}_caption", default: nil)+ end++ # Retrieves the current value of a setting+ #+ # @param name [Symbol] The name of the setting+ # @return [Object] The value of the setting+ def setting_value(name)+ Setting[name]+ end++ # Retrieves the allowed values for a setting's definition+ #+ # @param name [Symbol] The name of the setting+ # @return [Array] The allowed values for the setting+ def setting_allowed_values(name)+ Settings::Definition[name].allowed+ end++ # Checks if a setting is disabled.+ #+ # Any non-writable setting set by environment variables will be considered+ # disabled.+ #+ # @param name [Symbol] The name of the setting+ # @return [Boolean] `true` if the setting is disabled, `false` otherwise+ def setting_disabled?(name)+ !Setting.send(:"#{name}_writable?")+ end+ end+enddiff --git a/app/forms/settings/multi_lang_form.rb b/app/forms/settings/multi_lang_form.rbnew file mode 100644index 00000000000..eed7b6caf64--- /dev/null+++ b/app/forms/settings/multi_lang_form.rb@@ -0,0 +1,69 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+# ++++module Settings+ class MultiLangForm < ApplicationForm+ include FormHelper++ attr_reader :name, :current_language++ def initialize(name:, current_language:)+ super()++ @name = name+ @current_language = current_language+ end++ form do |f|+ # Add hidden languages+ Redmine::I18n.valid_languages.each do |lang|+ f.hidden(+ name: lang,+ value: Setting.send(name)[lang],+ id: "lang-for-#{name}-#{lang}"+ )+ end++ # Add WYSIWYG+ f.rich_text_area(+ name: current_language,+ value: setting_value(name)[current_language],+ label: setting_label(name),+ disabled: setting_disabled?(name),+ visually_hide_label: true,+ rich_text_options: {+ text_area_id: "settings-#{name}",+ turboMode: true,+ showAttachments: false+ }+ )+ end+ end+enddiff --git a/app/forms/settings_form_decorator.rb b/app/forms/settings_form_decorator.rbdeleted file mode 100644index d7601d7816f..00000000000--- a/app/forms/settings_form_decorator.rb+++ /dev/null@@ -1,200 +0,0 @@-# frozen_string_literal: true--#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 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.-#++--# Decorates a form object to provide a more convenient interface for-# rendering settings.-#-# It automatically sets the label, value, and disabled properties from the-# setting name and its definition attributes.-class SettingsFormDecorator- attr_reader :form-- # Initializes a new SettingsFormDecorator- #- # @param form [Object] The form object to be decorated- def initialize(form)- @form = form- end-- def method_missing(method, ...)- form.send(method, ...)- end-- def respond_to_missing?(method, include_private = false)- form.respond_to?(method, include_private)- end-- # Creates a text field input for a setting.- #- # The text field label is set from translating the key "setting_<name>".- #- # Any options passed to this method will override the default options.- #- # @param name [Symbol] The name of the setting- # @param options [Hash] Additional options for the text field- # @return [Object] The text field input- def text_field(name:, **options)- options.reverse_merge!(- label: setting_label(name),- value: setting_value(name),- disabled: setting_disabled?(name)- )- form.text_field(name:, **options)- end-- # Creates a check box input for a setting.- #- # The check box label is set from translating the key "setting_<name>".- #- # Any options passed to this method will override the default options.- #- # @param name [Symbol] The name of the setting- # @param options [Hash] Additional options for the check box- # @return [Object] The check box input- def check_box(name:, **options)- options.reverse_merge!(- label: setting_label(name),- checked: setting_value(name),- disabled: setting_disabled?(name)- )- form.check_box(name:, **options)- end-- # Creates a radio button group for a setting.- #- # The radio button group label is set from translating the key- # "setting_<name>". The radio button label are set from translating the- # key "setting_<name>_<value>". The caption is set from translating the- # key "setting_<name>_<value>_caption_html", which will be rendered as HTML,- # or "setting_<name>_<value>_caption", or nothing if none of the above- # are defined.- #- # Any options passed to this method will override the default options.- #- # @param name [Symbol] The name of the setting- # @param values [Array] The values for the radio buttons. Default to the- # setting's allowed values.- # @param disabled [Boolean] Force the radio button group to be disabled when- # true, will be disabled if the setting is not writable when false (default)- # @param button_options [Hash] Options for individual radio buttons- # @param options [Hash] Additional options for the radio button group- # @return [Object] The radio button group- def radio_button_group(name:, values: [], disabled: false, button_options: {}, **options)- values = values.presence || setting_allowed_values(name)- radio_group_options = options.reverse_merge(- label: setting_label(name),- disabled: disabled || setting_disabled?(name)- )- form.radio_button_group(- name:,- **radio_group_options- ) do |radio_group|- values.each do |value|- radio_group.radio_button(- **button_options.reverse_merge(- value:,- checked: setting_value(name) == value,- autocomplete: "off",- label: setting_label(name, value),- caption: setting_caption(name, value)- )- )- end- end- end-- # Creates a save button to submit the form- #- # @return [Object] The submit button- def submit- form.submit(name: "submit",- label: I18n.t("button_save"),- scheme: :primary)- end-- protected-- # Returns a translated string for a setting name.- #- # The translation key is "setting_<name>". Add additional names to the key- # to allow for translations with more context:- # "setting_<name>_<param2>_<param3>_...".- #- # @param names [Array<String | Symbol>] The name(s) of the setting- # @return [String] The translated label- def setting_label(*names)- I18n.t("setting_#{names.join('_')}")- end-- # Generates an HTML-safe caption for a setting.- #- # The translation key is "setting_<name>_caption". If not present, it will- # return nil.- #- # The translation will be marked as html_safe automatically if it ends with- # "_html", allowing to have HTML in the caption.- #- # Add additional names to the key to allow for translations with more context:- # "setting_<name>_<context>_caption_html" for instance.- #- # @param names [Array<Symbol>] The name(s) of the setting- # @return [String] The translated HTML-safe caption- def setting_caption(*names)- I18n.t("setting_#{names.join('_')}_caption_html", default: nil)&.html_safe \- || I18n.t("setting_#{names.join('_')}_caption", default: nil)- end-- # Retrieves the current value of a setting- #- # @param name [Symbol] The name of the setting- # @return [Object] The value of the setting- def setting_value(name)- Setting[name]- end-- # Retrieves the allowed values for a setting's definition- #- # @param name [Symbol] The name of the setting- # @return [Array] The allowed values for the setting- def setting_allowed_values(name)- Settings::Definition[name].allowed- end-- # Checks if a setting is disabled.- #- # Any non-writable setting set by environment variables will be considered- # disabled.- #- # @param name [Symbol] The name of the setting- # @return [Boolean] `true` if the setting is disabled, `false` otherwise- def setting_disabled?(name)- !Setting.send(:"#{name}_writable?")- end-enddiff --git a/app/helpers/tabs_helper.rb b/app/helpers/tabs_helper.rbindex bbb1ed2e6c6..3772ac008c6 100644--- a/app/helpers/tabs_helper.rb+++ b/app/helpers/tabs_helper.rb@@ -39,8 +39,23 @@ module TabsHelperendend+ def render_tab_header_nav(header, tabs)+ header.with_tab_nav(label: nil) do |tab_nav|+ tabs.each do |tab|+ tab_nav.with_tab(selected: selected_tab(tabs) == tab, href: tab[:path]) do |t|+ feature = tab[:enterprise_feature]++ if feature && !EnterpriseToken.allows_to?(feature)+ t.with_icon(icon: :"op-enterprise-addons", classes: "upsell-colored")+ end+ t.with_text { I18n.t(tab[:label]) }+ end+ end+ end+ end+def selected_tab(tabs)- tabs.detect { |t| t[:name] == params[:tab] } || tabs.first+ tabs.detect { |t| t[:name].to_s == params[:tab].to_s } || tabs.firstenddef tabs_for_key(key, params = {})diff --git a/app/views/common/_tabs.html.erb b/app/views/common/_tabs.html.erbindex 532d369af58..ade5ac4a876 100644--- a/app/views/common/_tabs.html.erb+++ b/app/views/common/_tabs.html.erb@@ -33,6 +33,6 @@ See COPYRIGHT and LICENSE files for more details.<% end %><%= content_tag "div",- render(partial: selected_tab[:partial], locals: { f: f, tab: selected_tab }),+ render(partial: selected_tab[:partial], locals: local_assigns.merge(tab: selected_tab)),id: "tab-content-#{selected_tab[:name]}",class: "tab-content" %>