15

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!

montrealmike
  • 11,433
  • 10
  • 64
  • 86
  • I don't think I18n support gender in any way. So there is no automatic built-in solution... but may be i'm wrong! – plehoux Nov 25 '10 at 02:42
  • The two idea i thought of were: set the gender in the model and then read it when creating the text (some lamba), OR create a tree structure where i would overwrite the text for the models that don't work. I'm assuming i'm not the first person to translate a rails app, so hopefully this has been resolved by someone in an elegant manner. – montrealmike Nov 25 '10 at 21:06

2 Answers2

11

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
siefca
  • 1,007
  • 13
  • 16
  • This example only works when translating the gender of a person which will be the same in every language, whereas in the question "model" can be substituted with any object, like e.g. "book" which will have a different gender in different languages. – Decade Moon Aug 09 '22 at 00:09
1

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

Sinetris
  • 8,288
  • 2
  • 22
  • 17