Skip to content

Commit

Permalink
Introduce i18n translator class that can be replaced by a custom tran…
Browse files Browse the repository at this point in the history
…slator

Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information
m4n authored and binarylogic committed Sep 12, 2009
1 parent 14dc9a7 commit de35f99
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
32 changes: 20 additions & 12 deletions lib/authlogic/i18n.rb
@@ -1,3 +1,5 @@
require "authlogic/i18n/translator"

module Authlogic
# This class allows any message in Authlogic to use internationalization. In earlier versions of Authlogic each message was translated via configuration.
# This cluttered up the configuration and cluttered up Authlogic. So all translation has been extracted out into this class. Now all messages pass through
Expand All @@ -11,18 +13,17 @@ module Authlogic
#
# Authlogic::I18n.t('error_messages.password_invalid', :default => "is invalid")
#
# If you use a different I18n library or plugin just redefine the t method in the Authlogic::I18n class to do whatever you want with those options. For example:
# If you use a different I18n library just replace the build-in I18n::Translator class with your own. For example:
#
# # config/initializers/authlogic.rb
# module MyAuthlogicI18nAdapter
# def t(key, options = {})
# class MyAuthlogicI18nTranslator
# 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
#
# Authlogic::I18n.extend MyAuthlogicI18nAdapter
# Authlogic::I18n.translator = MyAuthlogicI18nTranslator.new
#
# That it's! Here is a complete list of the keys that are passed. Just define these however you wish:
#
Expand All @@ -49,6 +50,7 @@ module Authlogic
# remember_me: remember me
module I18n
@@scope = :authlogic
@@translator = nil

class << self
# Returns the current scope. Defaults to :authlogic
Expand All @@ -60,16 +62,22 @@ def scope
def scope=(scope)
@@scope = scope
end

# Returns the current translator. Defaults to +Translator+.
def translator
@@translator ||= Translator.new
end

# Sets the current translator. Used to set a custom translator.
def translator=(translator)
@@translator = translator
end

# 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.
def t(key, options = {})
if defined?(::I18n)
::I18n.t(key, options.merge(:scope => I18n.scope))
else
options[:default]
end
def translate(key, options = {})
translator.translate key, { :scope => I18n.scope }.merge(options)
end
alias_method :translate, :t
alias :t :translate
end
end
end
15 changes: 15 additions & 0 deletions lib/authlogic/i18n/translator.rb
@@ -0,0 +1,15 @@
module Authlogic
module I18n
class Translator
# If the I18n gem is present, calls +I18n.translate+ passing all
# arguments, else returns +options[:default]+.
def translate(key, options = {})
if defined?(::I18n)
::I18n.translate key, options
else
options[:default]
end
end
end
end
end
22 changes: 21 additions & 1 deletion test/i18n_test.rb
Expand Up @@ -6,8 +6,28 @@ def test_uses_authlogic_as_scope_by_default
end

def test_can_set_scope
assert_nothing_raised{ Authlogic::I18n.scope = [:a, :b] }
assert_nothing_raised { Authlogic::I18n.scope = [:a, :b] }
assert_equal [:a, :b], Authlogic::I18n.scope
Authlogic::I18n.scope = :authlogic
end

def test_uses_built_in_translator_by_default
assert_equal Authlogic::I18n::Translator, Authlogic::I18n.translator.class
end

def test_can_set_custom_translator
old_translator = Authlogic::I18n.translator

assert_nothing_raised do
Authlogic::I18n.translator = Class.new do
def translate(key, options = {})
"Translated: #{key}"
end
end.new
end

assert_equal "Translated: x", Authlogic::I18n.translate(:x)

Authlogic::I18n.translator = old_translator
end
end

0 comments on commit de35f99

Please sign in to comment.