如何检查 Globalize 是否使用了回退

How to check if Globalize has used a fallback

我正在使用:

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

在我的 Image.rb 模型中,我有两列要翻译(英语和德语):

translates :name, :description

在application.rb我设置:

config.i18n.fallbacks = true

一切正常。我有一个英文说明,如果我将语言更改为德语,它会显示德文说明(以防万一),否则显示英文文本。问题是大多数图片描述仍然没有德语翻译,所以我想在德语网站上添加一条短信,说目前没有德语翻译,但我们会显示英语文本,直到有德语翻译可用。

我打算在我的视图中添加类似

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

有没有办法检查 Globalize 是否使用了回退选项并在这种情况下显示消息?

Globalize 将 translated_locales 方法添加到已翻译模型的实例中。

translated_locales returns 特定实例的所有可用语言环境的数组。当此数组不包含当前 I18n.locale 时,将使用回退。

你可以这样使用:

<%= 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 %>