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

3

5

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

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

Durfee answered 5/11, 2013 at 20:20 Comment(0)
C
6

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 => ... %>
Civilized answered 13/11, 2013 at 2:40 Comment(0)
U
3

You could use

<%= text_field 'role', "name_#{locale}", :value => @role.name_translations[locale], :class => ... %>
Unmannerly answered 26/3, 2015 at 13:33 Comment(0)
D
0

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

Divulgence answered 12/11, 2013 at 15:19 Comment(2)
define_method at runtime is bad for the Global method cache counter. Essentially calling define_method causes every method to have to be re-looked up to see where it should be defined. Not good for performance!Plod
See: github.com/charliesome/charlie.bz/blob/master/posts/…Plod

© 2022 - 2024 — McMap. All rights reserved.