Question

I have to translate a website in French. Now, French, as many other languages, has genders and inflection. Questions are:

  • How to create yaml files containing messages with variable parts being either male or female...
  • How to modify the i18n generator to support this?
  • Are there any gems or plugins supporting this?
Was it helpful?

Solution

This is a common problem in Italian too.

One solution which doesn't require plugins would be to use gender specific keys.

it: 
  presentation_f: "Sig.ra %{customer_name}" 
  presentation_m: "Sig. %{customer_name}" 

You can create a method in your model that takes a key in input and returns the gender modified key:

module Genderize
  def genderize(key)
    "#{key}_#{self.gender}" 
  end
end 

class Customer 
  include Genderize

  def gender
    gender_field_from_database_or_wherever || 'm' 
  end
end

In your controller or views you could do something like:

t(@person.genderize('presentation'), customer_name: @person.name)

The example is a bit contrived but you get the picture.

One other thing you can do is to write a tg function (meaning t-genderized) which takes as second argument the model where to get the gender method:

def tg(key, model, options={})
  gender_key = "#{key}_#{model.gender}" 
  I18n.t(gender_key, options)
end

Which is basically the same as above without polluting the models, using a global function instead.

i18n-inflector-rails looks like an interesting project, but in my experience it's not so rare to pass yaml files to non technical translators, and having that additional complexity to explain:

en:
  welcome:  "Dear @{f:Lady|m:Sir|n:You|All}"

may be too much for them. While this one:

en:
  welcome_f: "Dear Lady"
  welcome_m: "Dear Sir"
  welcome_n: "Dear You" 

It's much easier to read an to explain.

I would also suggest (this is a free advice not related to the question) to keep yaml files as flat as possibile preferring namespaced strings as keys, intstead of nested structure. For instance do this:

it: 
  home_page_welcome: "Welcome to the home page!" 

instead of this:

it: 
  home_page: 
    welcome: "Welcome to the home page!" 

You pay a little bit more verbosity in controllers and views, but you end up with code you can move around much easier without having to rearrange the tree structure in yaml fies. It's going to be also much easier to find out messed indentation when translated files come back to you.

OTHER TIPS

Have you tried i18n-inflector-rails ?

For files with variable parts depending on gender (in your en.yml):

welcome:  "Dear @{f:Lady|m:Sir|n:You|All}"

then you can define f, m and n like this:

en:
  i18n:
    inflections:
      gender:
        f:        "female"
        m:        "male"
        n:        "neuter"
        female:   @f
        male:     @m
        neuter:   @n
        man:      @male
        woman:    @female
        default:  n

Just make sure to define a inflection_method in your controller so it can detect the correct gem for a given case.

This example is for English but I believe a translation to French is straightforward.

EDIT:

I believe you need to specify the gender as a parameter in the translation, this is specially useful if you have a collection, here is how you do this:

t('.welcome', :gender => model.gender)

Remember that the inflection_method defined in the controller is just a helper so you don't need specify the gender as parameter multiple times, but in case you need to change it within the same view you can send it as a parameter like I said.

If you are going to use a lot this structure I recommend that you create a helper like this:

module TranslationHelper
  def translate_model(key, model)
    gender = model.try(:gender) || 'male'
    I18n.t(key, :gender => gender)
  end
end

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top