From 40fba9ee6ccb397f9af7d7fca0d4b4cf33ddf0c4 Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Thu, 15 Aug 2013 10:22:29 -0400 Subject: [PATCH] Add locale to context registers --- lib/liquid/i18n.rb | 21 +++++++-------------- lib/liquid/template.rb | 5 +++-- test/liquid/i18n_test.rb | 4 ++++ test/liquid/template_test.rb | 16 +++++++++++++++- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/liquid/i18n.rb b/lib/liquid/i18n.rb index 39e623e..7e0687c 100644 --- a/lib/liquid/i18n.rb +++ b/lib/liquid/i18n.rb @@ -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 diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index aaf7ca2..1a6dc01 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -61,7 +61,8 @@ module Liquid end # creates a new Template from an array of tokens. Use Template.parse 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 diff --git a/test/liquid/i18n_test.rb b/test/liquid/i18n_test.rb index afdda27..e6e0d7a 100644 --- a/test/liquid/i18n_test.rb +++ b/test/liquid/i18n_test.rb @@ -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 diff --git a/test/liquid/template_test.rb b/test/liquid/template_test.rb index 04f1e50..40ee971 100644 --- a/test/liquid/template_test.rb +++ b/test/liquid/template_test.rb @@ -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