Module: Authentication::Logic::I18n

Defined in:
lib/auth/logic/i18n.rb,
lib/auth/logic/i18n/translator.rb

Overview

This class allows any message in Authentication::Logic to use internationalization. In earlier versions of Authentication::Logic each message was translated via configuration. This cluttered up the configuration and cluttered up Authentication::Logic. So all translation has been extracted out into this class. Now all messages pass through this class, making it much easier to implement in I18n library / plugin you want. Use this as a layer that sits between Authentication::Logic and whatever I18n library you want to use.

By default this uses the rails I18n library, if it exists. If it doesn’t exist it just returns the default English message. The Authentication::Logic I18n class works EXACTLY like the rails I18n class. This is because the arguments are delegated to this class.

Here is how all messages are translated internally with Authentication::Logic:

Authentication::Logic::I18n.t('error_messages.password_invalid', :default => "is invalid")

If you use a different I18n library just replace the build-in I18n::Translator class with your own. For example:

class MyAuthentication::LogicI18nTranslator
  def translate(key, options = {})
    # you will have key which will be something like:
    # "error_messages.password_invalid"
    # you will also have options[:default], which will be the default
    # English version of the message
    # do whatever you want here with the arguments passed to you.
  end
end

Authentication::Logic::I18n.translator = MyAuthentication::LogicI18nTranslator.new

That it’s! Here is a complete list of the keys that are passed. Just define these however you wish:

Authentication::Logic:
  error_messages:
    login_blank: can not be blank
    login_not_found: is not valid
    login_invalid: should use only letters, numbers, spaces, and .-_@+ please.
    consecutive_failed_logins_limit_exceeded: >
      Consecutive failed logins limit exceeded, account is disabled.
    email_invalid: should look like an email address.
    email_invalid_international: should look like an international email address.
    password_blank: can not be blank
    password_invalid: is not valid
    not_active: Your account is not active
    not_confirmed: Your account is not confirmed
    not_approved: Your account is not approved
    no_authentication_details: You did not provide any details for authentication.
    general_credentials_error: Login/Password combination is not valid
    session_invalid: Your session is invalid and has the following errors:
  models:
    user_session: UserSession (or whatever name you are using)
  attributes:
    user_session: (or whatever name you are using)
      login: login
      email: email
      password: password
      remember_me: remember me

Defined Under Namespace

Classes: Translator

Constant Summary collapse

@@scope =
:auth_logic
@@translator =
nil

Class Method Summary collapse

Class Method Details

.scopeObject

Returns the current scope. Defaults to :auth_logic



73
74
75
# File 'lib/auth/logic/i18n.rb', line 73

def scope
  @@scope
end

.scope=(scope) ⇒ Object

Sets the current scope. Used to set a custom scope.



78
79
80
# File 'lib/auth/logic/i18n.rb', line 78

def scope=(scope)
  @@scope = scope
end

.translate(key, options = {}) ⇒ Object Also known as: t

All message translation is passed to this method. The first argument is the key for the message. The second is options, see the rails I18n library for a list of options used.



95
96
97
# File 'lib/auth/logic/i18n.rb', line 95

def translate(key, options = {})
  translator.translate key, { scope: I18n.scope }.merge(options)
end

.translatorObject

Returns the current translator. Defaults to Translator.



83
84
85
# File 'lib/auth/logic/i18n.rb', line 83

def translator
  @@translator ||= Translator.new
end

.translator=(translator) ⇒ Object

Sets the current translator. Used to set a custom translator.



88
89
90
# File 'lib/auth/logic/i18n.rb', line 88

def translator=(translator)
  @@translator = translator
end