Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to translate to a language with genders in rails

translating from English to French for example

submit:
  create: 'Create %{model}'
  update: 'Update %{model}'
  submit: 'Save %{model}'

would become

    submit:
      create: "Créer un(e) %{model}"
      update: "Modifier ce(tte) %{model}"
      submit: "Enregistrer ce(tte) %{model}"

What is the best way to implement the text in parenthesis (genderized) to work with any model passed. Thanks!

like image 625
montrealmike Avatar asked Nov 25 '10 01:11

montrealmike


2 Answers

There is also i18n-inflector-rails Rails plug-in which allows to register so called inflection methods in controllers.

These methods will transparently feed the I18n Inflector with data about gender or any other inflection kind you like.

Example:

fr:
  i18n:
    inflections:
      gender:
        h:        "hommes"
        f:        "femmes"
        n:        "neutre"
        m:        @h
        default:  n

  welcome:  "@{h,n:Cher|f:Chère|Bonjour}{ }{h:Monsieur|f:Dame|n:Vous|à tous}"

And then in controllers:

class ApplicationController < ActionController::Base

  before_filter :set_gender

  inflection_method :gender

  # inflection method for the kind gender
  def gender
    @gender
  end

  def set_gender
    if user_signed_in?              # assuming Devise is in use
      @gender = current_user.gender # assuming there is @gender attribute
    else
      @gender = nil
    end
  end

end

class UsersController < ApplicationController

  def say_welcome

    t('welcome')

    # => "Cher Vous"    (for empty gender or gender == :n)
    # => "Chère Dame"   (for gender == :f)
    # etc.

  end

end
like image 142
siefca Avatar answered Jan 01 '23 22:01

siefca


Take a look to i18n-inflector, it seems an interesting project.

like image 39
Sinetris Avatar answered Jan 01 '23 21:01

Sinetris