Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make i18n aware of "genders" in gender-sensitive languages like e.g. French

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?
like image 933
Danny Avatar asked Jan 29 '14 17:01

Danny


1 Answers

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.

like image 75
Fabrizio Regini Avatar answered Oct 06 '22 13:10

Fabrizio Regini