Skip to content
Browse files

Introduce i18n translator class that can be replaced by a custom tran…

…slator

Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information...
1 parent 14dc9a7 commit de35f99dd9c078975cc38c647bf251f9858ef3e5 @m4n m4n committed with
Showing with 56 additions and 13 deletions.
  1. +20 −12 lib/authlogic/i18n.rb
  2. +15 −0 lib/authlogic/i18n/translator.rb
  3. +21 −1 test/i18n_test.rb
View
32 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
@@ -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:
#
@@ -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
@@ -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
View
15 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
View
22 test/i18n_test.rb
@@ -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.
Something went wrong with that request. Please try again.