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