Skip to content

Commit

Permalink
Make it possible for gems to change the behaviour/signature of I18n. …
Browse files Browse the repository at this point in the history
…This is useful for other translation engines that do not want to use the key based syntax (tr8n). Also makes it a lot easier to put a cache in front of I18n.
  • Loading branch information
Bertg committed Oct 15, 2011
1 parent 541a05f commit fd91aa7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/i18n.rb
Expand Up @@ -12,7 +12,7 @@ module I18n
RESERVED_KEYS = [:scope, :default, :separator, :resolve, :object, :fallback, :format, :cascade, :throw, :raise, :rescue_format]
RESERVED_KEYS_PATTERN = /%\{(#{RESERVED_KEYS.join("|")})\}/

class << self
module Base
# Gets I18n configuration object.
def config
Thread.current[:i18n_config] ||= I18n::Config.new
Expand Down Expand Up @@ -327,4 +327,6 @@ def default_exception_handler(exception, locale, key, options)
exception.is_a?(MissingTranslation) ? exception.message : raise(exception)
end
end

extend Base
end
41 changes: 41 additions & 0 deletions test/api/override_test.rb
@@ -0,0 +1,41 @@
require 'test_helper'

class I18nOverrideTest < Test::Unit::TestCase
module OverrideInverse

def translate(*args)
super(*args).reverse
end
alias :t :translate

end

module OverrideSignature

def translate(*args)
args.first
end
alias :t :translate

end

def setup
I18n.backend = I18n::Backend::Simple.new
I18n.extend OverrideInverse
super
end

test "make sure modules can overwrite I18n methods" do
I18n.backend.store_translations('en', :foo => 'bar')

assert_equal 'rab', I18n.translate(:foo, :locale => 'en')
assert_equal 'rab', I18n.t(:foo, :locale => 'en')
assert_equal 'rab', I18n.translate!(:foo, :locale => 'en')
assert_equal 'rab', I18n.t!(:foo, :locale => 'en')
end

test "make sure modules can overwrite I18n signature" do
assert I18n.translate("Hello", "Welcome message on home page", :tokenize => true) # tr8n example
end

end

0 comments on commit fd91aa7

Please sign in to comment.