diff --git a/app/controllers/admin/site_customization/information_texts_controller.rb b/app/controllers/admin/site_customization/information_texts_controller.rb index 278b6a03d..6aa48cc1a 100644 --- a/app/controllers/admin/site_customization/information_texts_controller.rb +++ b/app/controllers/admin/site_customization/information_texts_controller.rb @@ -9,15 +9,25 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz def update content_params.each do |content| - value = content[:values].slice(*translation_params(content[:values])) - unless value.empty? - text = I18nContent.by_key(content[:id]).last || I18nContent.create(key: content[:id]) - text.update(value) - text.save + values = content[:values].slice(*translation_params(content[:values])) + + unless values.empty? + values.each do |key, value| + locale = key.split("_").last + + if value == I18n.backend.translate(locale, content[:id]) + 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 + redirect_to admin_site_customization_information_texts_path, notice: "Translation updated successfully" end private 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 899d43e2e..f8c811814 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 @@ -1,7 +1,9 @@ <% globalize(locale) do %> +
<%= I18nContent.where(key: content.key).first.try(:value) %>
<%= hidden_field_tag "contents[content_#{content.key}][id]", content.key %> <%= text_area_tag "contents[content_#{content.key}]values[value_#{locale}]", - content.value || t(content.key), + I18nContent.where(key: content.key).first.try(:value) || + I18n.backend.translate(locale, content.key), { rows: 5, class: "js-globalize-attribute", style: display_translation?(locale), diff --git a/spec/features/admin/site_customization/information_texts_spec.rb b/spec/features/admin/site_customization/information_texts_spec.rb index affd32082..424ee59e9 100644 --- a/spec/features/admin/site_customization/information_texts_spec.rb +++ b/spec/features/admin/site_customization/information_texts_spec.rb @@ -17,7 +17,7 @@ feature "Admin custom information texts" do expect(page).to have_content 'Access the community' click_link 'Proposals' - expect(page).to have_content 'Proposal type' + expect(page).to have_content 'Create proposal' within "#information-texts-tabs" do click_link "Polls" @@ -112,4 +112,105 @@ feature "Admin custom information texts" do expect(page).not_to have_content 'Texto en español' end + + context "Globalization" do + + scenario "Add a translation", :js, :focus do + key = "debates.form.debate_title" + + visit admin_site_customization_information_texts_path + + select "Français", from: "translation_locale" + fill_in "contents_content_#{key}values_value_fr", with: 'Titre personalise du débat' + + click_button "Save" + + expect(page).to have_content 'Translation updated successfully' + + select "Français", from: "translation_locale" + + expect(page).to have_content 'Titre personalise du débat' + expect(page).not_to have_content 'Titre du débat' + end + + scenario "Update a translation", :js do + visit @edit_milestone_url + + click_link "Español" + fill_in 'budget_investment_milestone_description_es', with: 'Descripción correcta en Español' + + click_button 'Update milestone' + expect(page).to have_content "Milestone updated successfully" + + visit budget_investment_path(investment.budget, investment) + + click_link("Milestones (1)") + expect(page).to have_content("Description in English") + + select('Español', from: 'locale-switcher') + click_link("Seguimiento (1)") + + expect(page).to have_content("Descripción correcta en Español") + end + + scenario "Remove a translation", :js do + visit @edit_milestone_url + + click_link "Español" + click_link "Remove language" + + expect(page).not_to have_link "Español" + + click_button "Update milestone" + visit @edit_milestone_url + expect(page).not_to have_link "Español" + end + + context "Globalize javascript interface" do + + scenario "Highlight current locale", :js do + visit @edit_milestone_url + + expect(find("a.js-globalize-locale-link.is-active")).to have_content "English" + + select('Español', from: 'locale-switcher') + + expect(find("a.js-globalize-locale-link.is-active")).to have_content "Español" + end + + scenario "Highlight selected locale", :js do + visit @edit_milestone_url + + expect(find("a.js-globalize-locale-link.is-active")).to have_content "English" + + click_link "Español" + + expect(find("a.js-globalize-locale-link.is-active")).to have_content "Español" + end + + scenario "Show selected locale form", :js do + visit @edit_milestone_url + + expect(page).to have_field('budget_investment_milestone_description_en', with: 'Description in English') + + click_link "Español" + + expect(page).to have_field('budget_investment_milestone_description_es', with: 'Descripción en Español') + end + + scenario "Select a locale and add it to the milestone form", :js do + visit @edit_milestone_url + + select "Français", from: "translation_locale" + + expect(page).to have_link "Français" + + click_link "Français" + + expect(page).to have_field('budget_investment_milestone_description_fr') + end + end + + end + end