Don't force translations for the current locale

Globalize creates a translation for the current locale, and the only way
I've found to change this behaviour is to monkey-patch it.

The original code uses `translation.locale` instead of
`Globalize.locale`. Since `translation.locale` loads the translation
with empty attributes. It both makes the record invalid if there are
validations and it makes it almost impossible to create a record with
translations which don't include the current locale.

See also the following convertations:

https://github.com/globalize/globalize/pull/328
https://github.com/globalize/globalize/issues/468
https://github.com/globalize/globalize/pull/578
https://github.com/shioyama/mobility/wiki/Migrating-from-Globalize#blank-translations
This commit is contained in:
Javi Martín
2018-10-11 01:42:42 +02:00
parent 124b8496de
commit 3b5a12b0ab
6 changed files with 58 additions and 12 deletions

View File

@@ -10,12 +10,17 @@ module GlobalizeHelper
end end
end end
def display_translation?(locale) def display_translation?(resource, locale)
if !resource || resource.translations.blank? ||
resource.translations.map(&:locale).include?(I18n.locale)
locale == I18n.locale locale == I18n.locale
else
locale == resource.translations.first.locale
end
end end
def display_translation_style(locale) def display_translation_style(resource, locale)
"display: none;" unless display_translation?(locale) "display: none;" unless display_translation?(resource, locale)
end end
def translation_enabled_tag(locale, enabled) def translation_enabled_tag(locale, enabled)
@@ -29,11 +34,12 @@ module GlobalizeHelper
def enable_locale?(resource, locale) def enable_locale?(resource, locale)
# Use `map` instead of `pluck` in order to keep the `params` sent # Use `map` instead of `pluck` in order to keep the `params` sent
# by the browser when there's invalid data # by the browser when there's invalid data
resource.translations.reject(&:_destroy).map(&:locale).include?(locale) || locale == I18n.locale (resource.translations.blank? && locale == I18n.locale) ||
resource.translations.reject(&:_destroy).map(&:locale).include?(locale)
end end
def highlight_class(locale) def highlight_class(resource, locale)
"is-active" if display_translation?(locale) "is-active" if display_translation?(resource, locale)
end end
def globalize(locale, &block) def globalize(locale, &block)

View File

@@ -74,7 +74,7 @@ module TranslatableFormHelper
end end
def display_style def display_style
@template.display_translation_style(locale) @template.display_translation_style(@object.globalized_model, locale)
end end
private private

View File

@@ -1,7 +1,7 @@
<% I18n.available_locales.each do |locale| %> <% I18n.available_locales.each do |locale| %>
<%= link_to t("admin.translations.remove_language"), "#", <%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{locale}", id: "delete-#{locale}",
style: display_translation_style(locale), style: display_translation_style(resource, locale),
class: 'float-right delete js-delete-language', class: 'float-right delete js-delete-language',
data: { locale: locale } %> data: { locale: locale } %>
@@ -12,7 +12,7 @@
<li class="tabs-title"> <li class="tabs-title">
<%= link_to name_for_locale(locale), "#", <%= link_to name_for_locale(locale), "#",
style: enable_translation_style(resource, locale), style: enable_translation_style(resource, locale),
class: "js-globalize-locale-link #{highlight_class(locale)}", class: "js-globalize-locale-link #{highlight_class(resource, locale)}",
data: { locale: locale }, data: { locale: locale },
remote: true %> remote: true %>
</li> </li>

View File

@@ -1,7 +1,7 @@
<% I18n.available_locales.each do |locale| %> <% I18n.available_locales.each do |locale| %>
<%= link_to t("admin.translations.remove_language"), "#", <%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{locale}", id: "delete-#{locale}",
style: display_translation_style(locale), style: site_customization_display_translation_style(locale),
class: 'float-right delete js-delete-language', class: 'float-right delete js-delete-language',
data: { locale: locale } %> data: { locale: locale } %>
@@ -12,7 +12,7 @@
<li class="tabs-title"> <li class="tabs-title">
<%= link_to name_for_locale(locale), "#", <%= link_to name_for_locale(locale), "#",
style: site_customization_display_translation_style(locale), style: site_customization_display_translation_style(locale),
class: "js-globalize-locale-link #{highlight_class(locale)}", class: "js-globalize-locale-link #{highlight_class(nil, locale)}",
data: { locale: locale }, data: { locale: locale },
remote: true %> remote: true %>
</li> </li>

View File

@@ -0,0 +1,13 @@
module Globalize
module ActiveRecord
module InstanceMethods
def save(*)
# Credit for this code belongs to Jack Tomaszewski:
# https://github.com/globalize/globalize/pull/578
Globalize.with_locale(Globalize.locale || I18n.default_locale) do
super
end
end
end
end
end

View File

@@ -110,6 +110,33 @@ feature 'Admin banners magement' do
expect(page).to have_link 'Such banner many text wow link', href: 'https://www.url.com' expect(page).to have_link 'Such banner many text wow link', href: 'https://www.url.com'
end end
scenario "Publish a banner with a translation different than the current locale", :js do
visit new_admin_banner_path
expect(page).to have_link "English"
click_link "Remove language"
select "Français", from: "translation_locale"
fill_in "Title", with: "En Français"
fill_in "Description", with: "Link en Français"
fill_in "Link", with: "https://www.url.com"
last_week = Time.current - 1.week
next_week = Time.current + 1.week
fill_in "Post started at", with: last_week.strftime("%d/%m/%Y")
fill_in "Post ended at", with: next_week.strftime("%d/%m/%Y")
click_button "Save changes"
click_link "Edit banner"
expect(page).to have_link "Français"
expect(page).not_to have_link "English"
expect(page).to have_field "Title", with: "En Français"
end
scenario "Update banner color when changing from color picker or text_field", :js do scenario "Update banner color when changing from color picker or text_field", :js do
visit new_admin_banner_path visit new_admin_banner_path