如何本地化Rails插件?

时间:2022-07-23 02:48:21

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

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

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!

2 个解决方案

#1


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

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

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):

您可以通过另一个插件(在OpenIdAuthentication之后加载)或插件加载后所需的文件(例如,在environment.rb中需要lib / open_id_authentication_suppl.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:

要将其与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

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启动时为openidauthentication.errors.messages的各种语言环境定义和加载I18n yml文件,并且不要忘记每次根据登录用户的语言环境开始处理控制器操作时设置I18n.locale。

#2


Copy that code into a file in /lib, then require it in environment.rb. It really is that easy.

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

#1


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

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

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):

您可以通过另一个插件(在OpenIdAuthentication之后加载)或插件加载后所需的文件(例如,在environment.rb中需要lib / open_id_authentication_suppl.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:

要将其与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

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启动时为openidauthentication.errors.messages的各种语言环境定义和加载I18n yml文件,并且不要忘记每次根据登录用户的语言环境开始处理控制器操作时设置I18n.locale。

#2


Copy that code into a file in /lib, then require it in environment.rb. It really is that easy.

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