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
24 lines
639 B
Ruby
24 lines
639 B
Ruby
require 'i18n/exceptions'
|
|
require 'action_view/helpers/tag_helper'
|
|
|
|
module ActionView
|
|
module Helpers
|
|
module TranslationHelper
|
|
include TagHelper
|
|
|
|
def t(key, options = {})
|
|
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?
|
|
translation
|
|
else
|
|
translate(key, options)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|