如何本地化Rails插件?

时间:2009-03-07 18:20:37

标签: ruby-on-rails plugins internationalization

我想将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

有可能在不直接更改插件的情况下翻译它们吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以在插件加载后随时重新定义OpenIdAuthentication::Result::ERROR_MESSAGES来覆盖它。

您可以通过其他插件(在OpenIdAuthentication之后加载)或插件加载后所需的文件(例如require lib/open_id_authentication_suppl.rb中的environment.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 -in用户的语言环境。

答案 1 :(得分:1)

将该代码复制到/lib中的文件中,然后在environment.rb中将其复制。这真的很容易。