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) %> -
- <%= text_with_links milestone.description %> -
- <% end %> ++ <%= text_with_links milestone.description %> +
<% if milestone.documents.present? %>