如何本地化Rails插件? [英] How to localize a Rails plugin?

查看:70
本文介绍了如何本地化Rails插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将OpenIdAuthentication插件翻译成另一种语言,但我不想直接更改该插件.

这是我要翻译的邮件的基本结构:

module OpenIdAuthentication

  class Result
    ERROR_MESSAGES = {
      :missing      => "Sorry, the OpenID server couldn't be found",
      :invalid      => "Sorry, but this does not appear to be a valid OpenID",
      :canceled     => "OpenID verification was canceled",
      :failed       => "OpenID verification failed",
      :setup_needed => "OpenID verification needs setup"
    } 
  end

end

无需直接更改插件就可以翻译它们吗?

谢谢!

解决方案

在插件加载后的任何时间,您都可以通过重新定义OpenIdAuthentication::Result::ERROR_MESSAGES来简单地覆盖它.

您可以通过其他插件(在OpenIdAuthentication之后加载)或从插件加载后所需的文件(例如environment.rb中的require lib/open_id_authentication_suppl.rb)执行此操作:

该代码本质上将是一个复制粘贴作业,如下所示:

module OpenIdAuthentication

  class Result
    ERROR_MESSAGES = {
      :missing      => "<message in foreign language>",
      :invalid      => "<message in foreign language>",
      :canceled     => "<message in foreign language>",
      :failed       => "<message in foreign language>",
      :setup_needed => "<message in foreign language>"
    } 
  end

将此与 I18N-rails (内置于Rails中)集成2.2.2(在以前的版本中可作为gem/插件提供),请执行以下操作:

  class I18NResultMessages
    def [](key)
      I18n.t(key, :scope => 'openidauthentication.errors.messages')
    end
  end

  class Result
    ERROR_MESSAGES = I18NResultMessages.new
  end

然后在Rails启动时为openidauthentication.errors.messages的各种语言环境定义和加载您的I18n yml文件,并且不要忘记在每次基于登录用户的语言环境开始处理控制器操作时都设置您的I18n.locale

I'd like to translate the OpenIdAuthentication plugin into another language but I'd like not to change the plugin directly.

Here's the basic structure of the messages I want to translate:

module OpenIdAuthentication

  class Result
    ERROR_MESSAGES = {
      :missing      => "Sorry, the OpenID server couldn't be found",
      :invalid      => "Sorry, but this does not appear to be a valid OpenID",
      :canceled     => "OpenID verification was canceled",
      :failed       => "OpenID verification failed",
      :setup_needed => "OpenID verification needs setup"
    } 
  end

end

It is something possible to translate them without changing the plugin directly?

Thanks!

解决方案

You can simply overwrite OpenIdAuthentication::Result::ERROR_MESSAGES by redefining it at any time after the plugin loads.

You may do so through a different plugin (that loads after OpenIdAuthentication), or from a file required after the plugin loads (e.g. require lib/open_id_authentication_suppl.rb in environment.rb):

The code will essentially be a copy-paste job, as follows:

module OpenIdAuthentication

  class Result
    ERROR_MESSAGES = {
      :missing      => "<message in foreign language>",
      :invalid      => "<message in foreign language>",
      :canceled     => "<message in foreign language>",
      :failed       => "<message in foreign language>",
      :setup_needed => "<message in foreign language>"
    } 
  end

To integrate this with I18N-rails (built into Rails 2.2.2, available as a gem/plugin in previous versions), do:

  class I18NResultMessages
    def [](key)
      I18n.t(key, :scope => 'openidauthentication.errors.messages')
    end
  end

  class Result
    ERROR_MESSAGES = I18NResultMessages.new
  end

Then define and load your I18n yml file for openidauthentication.errors.messages's various locales on Rails startup, and don't forget to set your I18n.locale every time you start processing a controller action based on the logged-in user's locale.

这篇关于如何本地化Rails插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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