How to avoid Globalize3 from returning fallback translations for an attribute into a specific context?

℡╲_俬逩灬. 提交于 2019-12-01 18:16:12

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

You could use

<%= text_field 'role', "name_#{locale}", :value => @role.name_translations[locale], :class => ... %>

Globalize creates a def globalize_fallbacks(locale) method which returns the fallback locales. Unfortunately there's no easy way to configure it so it returns no fallback.

What you can do is redefine the globalize_fallbacks method to return whatever locales you want to fallback. As you actually want to disable fallbacks this method would be

def globalize_fallbacks(locale)
  [locale]
end

So you can redefine the method before displaying the form and then revert it. It would be something like

<% Model.send :define_method, :globalize_fallbacks do  |locale|
  [locale] # You only want this locale to be used
end %>

<%= render_form %>

<% Model.send :define_method, :globalize_fallbacks do |locale|
  Globalize.fallbacks(locale) # This is globalize default behaviour
end %>

It feels kind of dirty hacking, but it's a solution :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!