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

前端 未结 3 1454
情话喂你
情话喂你 2021-01-19 01:43

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 po

3条回答
  •  终归单人心
    2021-01-19 02:23

    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 :)

提交回复
热议问题