如何避免 Globalize3 将属性的后备翻译返回到特定上下文中? [英] How to avoid Globalize3 from returning fallback translations for an attribute into a specific context?
问题描述
我正在使用 Globalize3 和 easy_globalize_accesors 对网站进行国际化/本地化,现在我正在调整表单以管理可能翻译的字段.假设我有一个名为 Role 的类:
I'm working in the internationalization/localization of web site using Globalize3 and easy_globalize_accesors and right now I'm adapting the forms to manage fields with possible translations. Suppose I have a class named Role:
class Role
translates :name, :fallbacks_for_empty_translations => true
# rest of class definition
我这样做是因为如果当前语言环境中没有翻译或为空,我想显示默认翻译,这可以按预期工作.
但是,在我的表单中,我想要相反的情况:我希望每个引用与默认语言环境不同的语言环境的输入不显示任何值,除非 role_translations 表中存在该属性的值.以下是我创建输入的方式:
I've done this because I want to show the default translation if there isn't a translation or is empty in the current locale and this works as expected.
But, in my form I want the opposite: I would like to have each input who refers to a different locale than default locale to show no value unless there is a value for that attribute in the role_translations table. Here is how I've created the inputs:
<%= textfield 'role', "name_#{locale}", :class => ... %>
目前,如果我创建了一个仅包含默认语言环境翻译的新角色,当我想编辑角色以将翻译添加到其他语言环境时,每个输入都会显示默认翻译的值.
提前致谢
Currently, what happens to me is if I have created a new Role with only the translation for the default locale, when I want to edit the role to add translations to other locales, each input show me the value of default translation.
Thanks in advance
推荐答案
你可以在你的模型中实现这个方法:
You can implement this method in your model:
def read_translated_attribute(name)
globalize.stash.contains?(Globalize.locale, name) ? globalize.stash.read(Globalize.locale, name) : translation_for(Globalize.locale).send(name)
end
然后您只需在表单中明确设置输入值,如下所示:
Then you'll just need to set the input values in your form explicitely, like this:
<%= text_field 'role', "name_#{locale}", :value => @role.read_translated_attribute(:name), :class => ... %>
这篇关于如何避免 Globalize3 将属性的后备翻译返回到特定上下文中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!