Display only translations for the current language

After creating a translation in spanish, it was also displaying it when selecting
the english locale.

This was due to the code picking the first translation available

With this commit, we are checking for an existing translation in the current locale
and displaying it if it exists
This commit is contained in:
rgarcia
2018-07-27 03:01:38 +02:00
committed by Angel Perez
parent 12f6f06ade
commit 4c8b174274
4 changed files with 17 additions and 15 deletions

View File

@@ -15,7 +15,7 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
values.each do |key, value|
locale = key.split("_").last
if value == I18n.backend.translate(locale, content[:id])
if value == t(content[:id], locale: locale)
next
else
text = I18nContent.find_or_create_by(key: content[:id])

View File

@@ -7,5 +7,4 @@ class I18nContent < ActiveRecord::Base
translates :value, touch: true
globalize_accessors locales: [:en, :es, :fr, :nl, :val]
end

View File

@@ -1,7 +1,15 @@
<% 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}]",
I18nContent.where(key: content.key).first.try(:value) ||
i18n_content_translation ||
t(content.key, locale: locale),
{ rows: 5,
class: "js-globalize-attribute",

View File

@@ -7,21 +7,16 @@ module ActionView
include TagHelper
def t(key, options = {})
translation = I18nContent.by_key(key)
current_locale = options[:locale].present? ? options[:locale] : I18n.locale
i18_content = I18nContent.by_key(key).first
translation = I18nContentTranslation.where(i18n_content_id: i18_content&.id,
locale: current_locale).first&.value
if translation.present?
Globalize.with_locale(locale) do
string = translation.first.value
options.each do |key, value|
string.sub! "%{#{key}}", (value || "%{#{key}}")
end
return string.html_safe unless string.nil?
end
translation
else
translate(key, options)
end
translate(key, options)
end
end
end