ruby-on-railsrubyinternationalizationglobalize

How to check if Globalize has used a fallback


I am using:

gem 'rails', '4.0.0'
gem 'globalize', '~> 4.0.2'

In my Image.rb model I have two columns I want translate (English and German):

translates :name, :description

In application.rb I set:

config.i18n.fallbacks = true

Everthing works fine. I have an English description and if I change language to German it shows the German description in case there is one, otherwise the English text. Proplem is that still most image description do not have a German translation, so I would like to add a text message to the German sites saying that currently there is no German translation but we show the English text until there is a German translation available.

I planned to add into my view somthing like

if fallback.true?
  Message: This text has not been translated yet and is shown in English

Is there a way to check if Globalize has used the fallback option and display a message in this case?


Solution

  • Globalize adds a translated_locales method to instances of the translated model.

    translated_locales returns an array of all available locales for a specific instance. When this array doesn't include the current I18n.locale then the fallback will be used.

    You can used that like this:

    <%= image.name %>
    <%= image.description %>
    
    <% unless image.translated_locales.include?(I18n.locale) %>
      Message: This text has not been translated yet and is shown in English
    <% end %>