在rails中翻译成具有性别的语言的最佳方法是什么 [英] What is the best way to translate to a language with genders in rails

查看:23
本文介绍了在rails中翻译成具有性别的语言的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如从英语翻译成法语

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

会变成

    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!

推荐答案

还有i18n-inflector-rails Rails 插件,允许在控制器中注册所谓的变形方法.

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

这些方法将透明地向 I18n Inflector 提供有关性别或您喜欢的任何其他变形类型的数据.

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

例子:

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}"

然后在控制器中:

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

这篇关于在rails中翻译成具有性别的语言的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆