mirror of
https://github.com/kemko/liquid.git
synced 2026-01-07 10:45:42 +03:00
Add locale to context registers
This commit is contained in:
@@ -2,10 +2,14 @@ require 'yaml'
|
||||
|
||||
module Liquid
|
||||
class I18n
|
||||
DEFAULT_LOCALE = File.join(File.expand_path(File.dirname(__FILE__)), "locales", "en.yml")
|
||||
|
||||
class TranslationError < StandardError
|
||||
end
|
||||
|
||||
attr_reader :path
|
||||
|
||||
def initialize(path)
|
||||
def initialize(path = DEFAULT_LOCALE)
|
||||
@path = path
|
||||
end
|
||||
|
||||
@@ -14,15 +18,8 @@ module Liquid
|
||||
end
|
||||
alias_method :t, :translate
|
||||
|
||||
class << self
|
||||
def translate(name, vars = {})
|
||||
@@global.translate(name, vars)
|
||||
end
|
||||
alias_method :t, :translate
|
||||
|
||||
def global=(translator)
|
||||
@@global = translator
|
||||
end
|
||||
def locale
|
||||
@locale ||= YAML.load_file(@path)
|
||||
end
|
||||
|
||||
private
|
||||
@@ -37,9 +34,5 @@ module Liquid
|
||||
level[cur] or raise TranslationError, translate("errors.i18n.unknown_translation", :name => name)
|
||||
end
|
||||
end
|
||||
|
||||
def locale
|
||||
@locale ||= YAML.load_file(@path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -61,7 +61,8 @@ module Liquid
|
||||
end
|
||||
|
||||
# creates a new <tt>Template</tt> from an array of tokens. Use <tt>Template.parse</tt> instead
|
||||
def initialize
|
||||
def initialize(options = {})
|
||||
registers[:locale] = I18n.new(options[:locale]) || I18n.new
|
||||
@resource_limits = {}
|
||||
end
|
||||
|
||||
@@ -119,7 +120,7 @@ module Liquid
|
||||
when nil
|
||||
Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits)
|
||||
else
|
||||
raise ArgumentError, I18n.translate("errors.template.argument_hash_or_context")
|
||||
raise ArgumentError, registers[:locale].translate("errors.template.argument_hash_or_context")
|
||||
end
|
||||
|
||||
case args.last
|
||||
|
||||
@@ -30,4 +30,8 @@ class I18nTest < Test::Unit::TestCase
|
||||
@i18n.translate("doesnt_exist")
|
||||
end
|
||||
end
|
||||
|
||||
def test_sets_default_path_to_en
|
||||
assert_equal I18n::DEFAULT_LOCALE, I18n.new.path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -143,4 +143,18 @@ class TemplateTest < Test::Unit::TestCase
|
||||
assert_equal 'bar', t.parse('{{bar}}').render(drop)
|
||||
assert_equal 'haha', t.parse("{{baz}}").render(drop)
|
||||
end
|
||||
end # TemplateTest
|
||||
|
||||
def test_sets_default_localization_in_context
|
||||
t = Template.new(:locale => fixture("en_locale.yml"))
|
||||
|
||||
assert_instance_of I18n, t.registers[:locale]
|
||||
assert_equal fixture("en_locale.yml"), t.registers[:locale].path
|
||||
end
|
||||
|
||||
def test_sets_default_localization_in_context_with_quick_initialization
|
||||
t = Template.parse('{{foo}}', :locale => fixture("en_locale.yml"))
|
||||
|
||||
assert_instance_of I18n, t.registers[:locale]
|
||||
assert_equal fixture("en_locale.yml"), t.registers[:locale].path
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user