diff --git a/app/controllers/admin/site_customization/information_texts_controller.rb b/app/controllers/admin/site_customization/information_texts_controller.rb new file mode 100644 index 000000000..25594fc4a --- /dev/null +++ b/app/controllers/admin/site_customization/information_texts_controller.rb @@ -0,0 +1,90 @@ +class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomization::BaseController + include Translatable + + def index + fetch_existing_keys + append_or_create_keys + @content = @content[@tab.to_s] + end + + def update + content_params.each do |content| + values = content[:values].slice(*translation_params(content[:values])) + + unless values.empty? + values.each do |key, value| + locale = key.split("_").last + + if value == t(content[:id], locale: locale) || value.match(/translation missing/) + next + else + text = I18nContent.find_or_create_by(key: content[:id]) + Globalize.locale = locale + text.update(value: value) + end + end + end + + end + + redirect_to admin_site_customization_information_texts_path, + notice: t('flash.actions.update.translation') + end + + private + + def i18n_content_params + attributes = [:key, :value] + params.require(:information_texts).permit(*attributes, translation_params(params[:information_texts])) + end + + def resource_model + I18nContent + end + + def resource + resource_model.find(content_params[:id]) + end + + def content_params + params.require(:contents).values + end + + def delete_translations + languages_to_delete = params[:delete_translations].select { |k, v| params[:delete_translations][k] == '1' }.keys + languages_to_delete.each do |locale| + I18nContentTranslation.destroy_all(locale: locale) + end + end + + def fetch_existing_keys + @existing_keys = {} + @tab = params[:tab] || :debates + + I18nContent.begins_with_key(@tab) + .all + .map{ |content| @existing_keys[content.key] = content } + end + + def append_or_create_keys + @content = {} + I18n.backend.send(:init_translations) unless I18n.backend.initialized? + + locale = params[:locale] || I18n.locale + translations = I18n.backend.send(:translations)[locale.to_sym] + + translations.each do |k, v| + @content[k.to_s] = flat_hash(v).keys + .map { |s| @existing_keys["#{k.to_s}.#{s}"].nil? ? + I18nContent.new(key: "#{k.to_s}.#{s}") : + @existing_keys["#{k.to_s}.#{s}"] } + end + end + + def flat_hash(h, f = nil, g = {}) + return g.update({ f => h }) unless h.is_a? Hash + h.each { |k, r| flat_hash(r, [f,k].compact.join('.'), g) } + return g + end + +end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index 8a32e5630..772dcb9d3 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -49,7 +49,7 @@ module AdminHelper end def menu_customization? - ["pages", "banners"].include?(controller_name) || menu_homepage? + ["pages", "banners", "information_texts"].include?(controller_name) || menu_homepage? end def menu_homepage? diff --git a/app/helpers/site_customization_helper.rb b/app/helpers/site_customization_helper.rb new file mode 100644 index 000000000..d9318fd9b --- /dev/null +++ b/app/helpers/site_customization_helper.rb @@ -0,0 +1,5 @@ +module SiteCustomizationHelper + def site_customization_display_translation?(locale) + I18nContentTranslation.existing_languages.include?(neutral_locale(locale)) || locale == I18n.locale ? "" : "display: none" + end +end diff --git a/app/models/i18n_content.rb b/app/models/i18n_content.rb new file mode 100644 index 000000000..c291e696c --- /dev/null +++ b/app/models/i18n_content.rb @@ -0,0 +1,11 @@ +class I18nContent < ActiveRecord::Base + + scope :by_key, -> (key){ where(key: key) } + scope :begins_with_key, -> (key){ where("key ILIKE ?", "#{key}?%") } + + validates :key, uniqueness: true + + translates :value, touch: true + globalize_accessors locales: [:en, :es, :fr, :nl] + +end diff --git a/app/models/i18n_content_translation.rb b/app/models/i18n_content_translation.rb new file mode 100644 index 000000000..76447d83e --- /dev/null +++ b/app/models/i18n_content_translation.rb @@ -0,0 +1,5 @@ +class I18nContentTranslation < ActiveRecord::Base + def self.existing_languages + self.select(:locale).uniq.map{ |l| l.locale.to_sym }.to_a + end +end diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 8385b8742..911bc0713 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -121,6 +121,10 @@
  • > <%= link_to t("admin.menu.banner"), admin_banners_path %>
  • + +
  • > + <%= link_to t("admin.menu.site_customization.information_texts"), admin_site_customization_information_texts_path %> +
  • diff --git a/app/views/admin/site_customization/information_texts/_form.html.erb b/app/views/admin/site_customization/information_texts/_form.html.erb new file mode 100644 index 000000000..861315691 --- /dev/null +++ b/app/views/admin/site_customization/information_texts/_form.html.erb @@ -0,0 +1,16 @@ +<%= render "globalize_locales" %> + +<%= form_tag admin_site_customization_information_texts_path do %> + <% I18n.available_locales.each do |l| %> + <%= hidden_field_tag "delete_translations[#{l}]", 0 %> + <% end %> + <% contents.each do |group| %> + <% group.each do |content| %> + <%= content.key %> + <% content.globalize_locales.each do |locale| %> + <%= render "form_field", content: content, locale: locale %> + <% end %> + <% end %> + <% end %> + <%= submit_tag t("admin.menu.site_customization.buttons.save"), class: "button" %> +<% 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 new file mode 100644 index 000000000..54945dfb6 --- /dev/null +++ b/app/views/admin/site_customization/information_texts/_form_field.html.erb @@ -0,0 +1,19 @@ +<% globalize(locale) do %> + + <% i18n_content = I18nContent.where(key: content.key).first %> + <% if i18n_content.present? %> + <% i18n_content_translation = I18nContentTranslation.where(i18n_content_id: i18n_content.id, locale: locale).first.try(:value) %> + <% else %> + <% i18n_content_translation = false %> + <% end %> + + <%= hidden_field_tag "contents[content_#{content.key}][id]", content.key %> + <%= 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 } + } %> +<% 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 new file mode 100644 index 000000000..9e90283cd --- /dev/null +++ b/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb @@ -0,0 +1,27 @@ +<% I18n.available_locales.each do |locale| %> + <%= link_to t("admin.milestones.form.remove_language"), "#", + id: "delete-#{neutral_locale(locale)}", + style: show_delete?(locale), + class: 'float-right delete js-delete-language', + data: { locale: neutral_locale(locale) } %> + +<% end %> + + + +
    + <%= select_tag :translation_locale, + options_for_locale_select, + prompt: t("admin.milestones.form.add_language"), + class: "js-globalize-locale" %> +
    diff --git a/app/views/admin/site_customization/information_texts/_tabs.html.erb b/app/views/admin/site_customization/information_texts/_tabs.html.erb new file mode 100644 index 000000000..ca5061bc3 --- /dev/null +++ b/app/views/admin/site_customization/information_texts/_tabs.html.erb @@ -0,0 +1,7 @@ + diff --git a/app/views/admin/site_customization/information_texts/index.html.erb b/app/views/admin/site_customization/information_texts/index.html.erb new file mode 100644 index 000000000..f41102d97 --- /dev/null +++ b/app/views/admin/site_customization/information_texts/index.html.erb @@ -0,0 +1,10 @@ +

    <%= t("admin.menu.site_customization.information_texts") %>

    + +
    + <%= render 'tabs' %> + + + +
    diff --git a/app/views/welcome/welcome.html.erb b/app/views/welcome/welcome.html.erb index ec51e225f..d647ba8e6 100644 --- a/app/views/welcome/welcome.html.erb +++ b/app/views/welcome/welcome.html.erb @@ -1,9 +1,7 @@

    <%= t("welcome.welcome.title") %>

    -

    <%= t("welcome.welcome.user_permission_info") %>

    -