diff --git a/app/helpers/globalize_helper.rb b/app/helpers/globalize_helper.rb index 3dcd9520d..cff0b4ccd 100644 --- a/app/helpers/globalize_helper.rb +++ b/app/helpers/globalize_helper.rb @@ -6,20 +6,12 @@ module GlobalizeHelper def locale_options I18n.available_locales.map do |locale| - [name_for_locale(locale), neutral_locale(locale)] + [name_for_locale(locale), locale] end end def display_translation?(locale) - same_locale?(neutral_locale(I18n.locale), neutral_locale(locale)) ? "" : "display: none" - end - - def render_translations_to_delete(resource) - capture do - resource.globalize_locales.each do |locale| - concat translation_enabled_tag(locale, enable_locale?(resource, locale)) - end - end + same_locale?(I18n.locale, locale) ? "" : "display: none;" end def translation_enabled_tag(locale, enabled) @@ -27,11 +19,11 @@ module GlobalizeHelper end def css_to_display_translation?(resource, locale) - enable_locale?(resource, locale) ? "" : "display: none" + enable_locale?(resource, locale) ? "" : "display: none;" end def enable_locale?(resource, locale) - resource.translated_locales.include?(neutral_locale(locale)) || locale == I18n.locale + resource.translated_locales.include?(locale) || locale == I18n.locale end def highlight_current?(locale) @@ -42,10 +34,6 @@ module GlobalizeHelper display_translation?(locale) end - def neutral_locale(locale) - locale.to_s.downcase.underscore.to_sym - end - def globalize(locale, &block) Globalize.with_locale(locale) do yield diff --git a/app/helpers/site_customization_helper.rb b/app/helpers/site_customization_helper.rb index 9e54fbf4e..1b4968b6c 100644 --- a/app/helpers/site_customization_helper.rb +++ b/app/helpers/site_customization_helper.rb @@ -1,9 +1,9 @@ module SiteCustomizationHelper def site_customization_enable_translation?(locale) - I18nContentTranslation.existing_languages.include?(neutral_locale(locale)) || locale == I18n.locale + I18nContentTranslation.existing_languages.include?(locale) || locale == I18n.locale end def site_customization_display_translation?(locale) - site_customization_enable_translation?(locale) ? "" : "display: none" + site_customization_enable_translation?(locale) ? "" : "display: none;" end end diff --git a/app/helpers/translatable_form_helper.rb b/app/helpers/translatable_form_helper.rb new file mode 100644 index 000000000..41aa1181a --- /dev/null +++ b/app/helpers/translatable_form_helper.rb @@ -0,0 +1,56 @@ +module TranslatableFormHelper + def translatable_form_for(record_or_record_path, options = {}) + object = record_or_record_path.is_a?(Array) ? record_or_record_path.last : record_or_record_path + + form_for(record_or_record_path, options.merge(builder: TranslatableFormBuilder)) do |f| + + object.globalize_locales.each do |locale| + concat translation_enabled_tag(locale, enable_locale?(object, locale)) + end + + yield(f) + end + end + + def merge_translatable_field_options(options, locale) + options.merge( + class: "#{options[:class]} js-globalize-attribute".strip, + style: "#{options[:style]} #{display_translation?(locale)}".strip, + data: options.fetch(:data, {}).merge(locale: locale), + label_options: { + class: "#{options.dig(:label_options, :class)} js-globalize-attribute".strip, + style: "#{options.dig(:label_options, :style)} #{display_translation?(locale)}".strip, + data: (options.dig(:label_options, :data) || {}) .merge(locale: locale) + } + ) + end + + class TranslatableFormBuilder < FoundationRailsHelper::FormBuilder + + def translatable_text_field(method, options = {}) + translatable_field(:text_field, method, options) + end + + def translatable_text_area(method, options = {}) + translatable_field(:text_area, method, options) + end + + private + + def translatable_field(field_type, method, options = {}) + @template.capture do + @object.globalize_locales.each do |locale| + Globalize.with_locale(locale) do + localized_attr_name = @object.localized_attr_name_for(method, locale) + + label_without_locale = @object.class.human_attribute_name(method) + final_options = @template.merge_translatable_field_options(options, locale) + .reverse_merge(label: label_without_locale) + + @template.concat send(field_type, localized_attr_name, final_options) + end + end + end + end + end +end diff --git a/app/models/banner.rb b/app/models/banner.rb index 8ad7b4409..37824d01f 100644 --- a/app/models/banner.rb +++ b/app/models/banner.rb @@ -5,7 +5,7 @@ class Banner < ActiveRecord::Base translates :title, touch: true translates :description, touch: true - globalize_accessors locales: [:en, :es, :fr, :nl, :val, :pt_br] + globalize_accessors validates :title, presence: true, length: { minimum: 2 } diff --git a/app/models/budget/investment/milestone.rb b/app/models/budget/investment/milestone.rb index b00744138..1790f7323 100644 --- a/app/models/budget/investment/milestone.rb +++ b/app/models/budget/investment/milestone.rb @@ -8,7 +8,7 @@ class Budget accepted_content_types: [ "application/pdf" ] translates :title, :description, touch: true - globalize_accessors locales: [:en, :es, :fr, :nl, :val, :pt_br] + globalize_accessors belongs_to :investment belongs_to :status, class_name: 'Budget::Investment::Status' diff --git a/app/views/admin/banners/_form.html.erb b/app/views/admin/banners/_form.html.erb index 105a872fb..28e8a7192 100644 --- a/app/views/admin/banners/_form.html.erb +++ b/app/views/admin/banners/_form.html.erb @@ -1,11 +1,9 @@ <%= render "admin/shared/globalize_locales", resource: @banner %> -<%= form_for [:admin, @banner] do |f| %> +<%= translatable_form_for [:admin, @banner] do |f| %> <%= render 'errors' %> - <%= render_translations_to_delete(@banner) %> -
<% date_started_at = @banner.post_started_at.present? ? I18n.localize(@banner.post_started_at) : "" %>
@@ -31,19 +29,10 @@
- <%= f.label :title, t("admin.banners.banner.title") %> - - <% @banner.globalize_locales.each do |locale| %> - <% globalize(locale) do %> - <%= f.text_field "title_#{locale}", - placeholder: t("admin.banners.banner.title"), - class: "js-globalize-attribute", - data: {js_banner_title: "js_banner_title", - locale: locale}, - style: display_translation?(locale), - label: false %> - <% end %> - <% end %> + <%= f.translatable_text_field :title, + placeholder: t("admin.banners.banner.title"), + data: {js_banner_title: "js_banner_title"}, + label: t("admin.banners.banner.title") %>
@@ -56,18 +45,10 @@
- <%= f.label :description, t("admin.banners.banner.description") %> - <% @banner.globalize_locales.each do |locale| %> - <% globalize(locale) do %> - <%= f.text_field "description_#{locale}", - placeholder: t("admin.banners.banner.description"), - class: "js-globalize-attribute", - data: {js_banner_description: "js_banner_description", - locale: locale}, - style: display_translation?(locale), - label: false %> - <% end %> - <% end %> + <%= f.translatable_text_field :description, + placeholder: t("admin.banners.banner.description"), + data: {js_banner_description: "js_banner_description"}, + label: t("admin.banners.banner.description") %>
diff --git a/app/views/admin/budget_investment_milestones/_form.html.erb b/app/views/admin/budget_investment_milestones/_form.html.erb index 5598a51cd..ab3ee6cf6 100644 --- a/app/views/admin/budget_investment_milestones/_form.html.erb +++ b/app/views/admin/budget_investment_milestones/_form.html.erb @@ -1,8 +1,6 @@ <%= render "admin/shared/globalize_locales", resource: @milestone %> -<%= form_for [:admin, @investment.budget, @investment, @milestone] do |f| %> - - <%= render_translations_to_delete(@milestone) %> +<%= translatable_form_for [:admin, @investment.budget, @investment, @milestone] do |f| %> <%= f.hidden_field :title, value: l(Time.current, format: :datetime), maxlength: Budget::Investment::Milestone.title_max_length %> @@ -16,16 +14,9 @@ admin_budget_investment_statuses_path %>
- <%= f.label :description, t("admin.milestones.new.description") %> - <% @milestone.globalize_locales.each do |locale| %> - <% globalize(locale) do %> - <%= f.text_area "description_#{locale}", rows: 5, - class: "js-globalize-attribute", - data: { locale: locale }, - style: display_translation?(locale), - label: false %> - <% end %> - <% end %> + <%= f.translatable_text_area :description, + rows: 5, + label: t("admin.milestones.new.description") %> <%= f.label :publication_date, t("admin.milestones.new.date") %> <%= f.text_field :publication_date, diff --git a/app/views/admin/shared/_globalize_locales.html.erb b/app/views/admin/shared/_globalize_locales.html.erb index 977fa6d9d..c4a0cf5bc 100644 --- a/app/views/admin/shared/_globalize_locales.html.erb +++ b/app/views/admin/shared/_globalize_locales.html.erb @@ -1,9 +1,9 @@ <% I18n.available_locales.each do |locale| %> <%= link_to t("admin.translations.remove_language"), "#", - id: "delete-#{neutral_locale(locale)}", + id: "delete-#{locale}", style: show_delete?(locale), class: 'float-right delete js-delete-language', - data: { locale: neutral_locale(locale) } %> + data: { locale: locale } %> <% end %> @@ -13,7 +13,7 @@ <%= link_to name_for_locale(locale), "#", style: css_to_display_translation?(resource, locale), class: "js-globalize-locale-link #{highlight_current?(locale)}", - data: { locale: neutral_locale(locale) }, + data: { locale: locale }, remote: true %> <% end %> diff --git a/app/views/admin/site_customization/information_texts/_form_field.html.erb b/app/views/admin/site_customization/information_texts/_form_field.html.erb index 54945dfb6..11dc02b20 100644 --- a/app/views/admin/site_customization/information_texts/_form_field.html.erb +++ b/app/views/admin/site_customization/information_texts/_form_field.html.erb @@ -11,9 +11,5 @@ <%= text_area_tag "contents[content_#{content.key}]values[value_#{locale}]", i18n_content_translation || t(content.key, locale: locale), - { rows: 5, - class: "js-globalize-attribute", - style: display_translation?(locale), - data: { locale: locale } - } %> + merge_translatable_field_options({rows: 5}, locale) %> <% end %> diff --git a/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb b/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb index f69e48897..f472ca263 100644 --- a/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb +++ b/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb @@ -1,9 +1,9 @@ <% I18n.available_locales.each do |locale| %> <%= link_to t("admin.translations.remove_language"), "#", - id: "delete-#{neutral_locale(locale)}", + id: "delete-#{locale}", style: show_delete?(locale), class: 'float-right delete js-delete-language', - data: { locale: neutral_locale(locale) } %> + data: { locale: locale } %> <% end %> @@ -13,7 +13,7 @@ <%= link_to name_for_locale(locale), "#", style: site_customization_display_translation?(locale), class: "js-globalize-locale-link #{highlight_current?(locale)}", - data: { locale: neutral_locale(locale) }, + data: { locale: locale }, remote: true %> <% end %> diff --git a/app/views/budgets/investments/milestones/_milestone.html.erb b/app/views/budgets/investments/milestones/_milestone.html.erb index 79c8c05a0..7f71d0f29 100644 --- a/app/views/budgets/investments/milestones/_milestone.html.erb +++ b/app/views/budgets/investments/milestones/_milestone.html.erb @@ -24,11 +24,9 @@ <%= image_tag(milestone.image_url(:large), { id: "image_#{milestone.id}", alt: milestone.image.title, class: "margin" }) if milestone.image.present? %> - <% globalize(neutral_locale(locale)) do %> -

- <%= text_with_links milestone.description %> -

- <% end %> +

+ <%= text_with_links milestone.description %> +

<% if milestone.documents.present? %>