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| values.each do |key, value|
locale = key.split("_").last locale = key.split("_").last
if value == I18n.backend.translate(locale, content[:id]) if value == t(content[:id], locale: locale)
next next
else else
text = I18nContent.find_or_create_by(key: content[:id]) text = I18nContent.find_or_create_by(key: content[:id])

View File

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

View File

@@ -1,7 +1,15 @@
<% globalize(locale) do %> <% 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 %> <%= hidden_field_tag "contents[content_#{content.key}][id]", content.key %>
<%= text_area_tag "contents[content_#{content.key}]values[value_#{locale}]", <%= 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), t(content.key, locale: locale),
{ rows: 5, { rows: 5,
class: "js-globalize-attribute", class: "js-globalize-attribute",

View File

@@ -7,21 +7,16 @@ module ActionView
include TagHelper include TagHelper
def t(key, options = {}) 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? if translation.present?
Globalize.with_locale(locale) do translation
string = translation.first.value else
translate(key, options)
options.each do |key, value|
string.sub! "%{#{key}}", (value || "%{#{key}}")
end
return string.html_safe unless string.nil?
end
end end
translate(key, options)
end end
end end
end end