views:

250

answers:

2

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!

+1  A: 

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.

Cheers, V.

vladr
Thank you! This code does work! So I think there's no way to use the yml file in locales directly because the plugin isn't ready for i18n. Am I right?
collimarco
See my I18n update
vladr
+1  A: 

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

Sarah Mei