如何避免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屋!