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

前端 未结 2 1782
我寻月下人不归
我寻月下人不归 2020-12-29 08:56

translating from English to French for example

submit:
  create: \'Create %{model}\'
  update: \'Update %{model}\'
  submit: \'Save %{model}\'
相关标签:
2条回答
  • 2020-12-29 09:18

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

    0 讨论(0)
  • 2020-12-29 09:28

    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
    
    0 讨论(0)
提交回复
热议问题