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:
@@ -10,12 +10,17 @@ module GlobalizeHelper
|
||||
end
|
||||
end
|
||||
|
||||
def display_translation?(locale)
|
||||
locale == I18n.locale
|
||||
def display_translation?(resource, locale)
|
||||
if !resource || resource.translations.blank? ||
|
||||
resource.translations.map(&:locale).include?(I18n.locale)
|
||||
locale == I18n.locale
|
||||
else
|
||||
locale == resource.translations.first.locale
|
||||
end
|
||||
end
|
||||
|
||||
def display_translation_style(locale)
|
||||
"display: none;" unless display_translation?(locale)
|
||||
def display_translation_style(resource, locale)
|
||||
"display: none;" unless display_translation?(resource, locale)
|
||||
end
|
||||
|
||||
def translation_enabled_tag(locale, enabled)
|
||||
@@ -29,11 +34,12 @@ module GlobalizeHelper
|
||||
def enable_locale?(resource, locale)
|
||||
# Use `map` instead of `pluck` in order to keep the `params` sent
|
||||
# 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
|
||||
|
||||
def highlight_class(locale)
|
||||
"is-active" if display_translation?(locale)
|
||||
def highlight_class(resource, locale)
|
||||
"is-active" if display_translation?(resource, locale)
|
||||
end
|
||||
|
||||
def globalize(locale, &block)
|
||||
|
||||
@@ -74,7 +74,7 @@ module TranslatableFormHelper
|
||||
end
|
||||
|
||||
def display_style
|
||||
@template.display_translation_style(locale)
|
||||
@template.display_translation_style(@object.globalized_model, locale)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<% I18n.available_locales.each do |locale| %>
|
||||
<%= link_to t("admin.translations.remove_language"), "#",
|
||||
id: "delete-#{locale}",
|
||||
style: display_translation_style(locale),
|
||||
style: display_translation_style(resource, locale),
|
||||
class: 'float-right delete js-delete-language',
|
||||
data: { locale: locale } %>
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
<li class="tabs-title">
|
||||
<%= link_to name_for_locale(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 },
|
||||
remote: true %>
|
||||
</li>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<% I18n.available_locales.each do |locale| %>
|
||||
<%= link_to t("admin.translations.remove_language"), "#",
|
||||
id: "delete-#{locale}",
|
||||
style: display_translation_style(locale),
|
||||
style: site_customization_display_translation_style(locale),
|
||||
class: 'float-right delete js-delete-language',
|
||||
data: { locale: locale } %>
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
<li class="tabs-title">
|
||||
<%= link_to name_for_locale(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 },
|
||||
remote: true %>
|
||||
</li>
|
||||
|
||||
13
config/initializers/globalize.rb
Normal file
13
config/initializers/globalize.rb
Normal 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
|
||||
@@ -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'
|
||||
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
|
||||
visit new_admin_banner_path
|
||||
|
||||
|
||||
Reference in New Issue
Block a user