mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Add sketch of I18n error translation
This commit is contained in:
@@ -48,6 +48,7 @@ end
|
|||||||
require "liquid/version"
|
require "liquid/version"
|
||||||
require 'liquid/lexer'
|
require 'liquid/lexer'
|
||||||
require 'liquid/parser'
|
require 'liquid/parser'
|
||||||
|
require 'liquid/i18n'
|
||||||
require 'liquid/drop'
|
require 'liquid/drop'
|
||||||
require 'liquid/extensions'
|
require 'liquid/extensions'
|
||||||
require 'liquid/errors'
|
require 'liquid/errors'
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
require 'yaml'
|
||||||
|
require 'delegate'
|
||||||
|
|
||||||
|
module Liquid
|
||||||
|
class I18n
|
||||||
|
class TranslationError < StandardError
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(path)
|
||||||
|
@path = path
|
||||||
|
end
|
||||||
|
|
||||||
|
def translate(name, vars = {})
|
||||||
|
interpolate(deep_fetch_translation(name), vars)
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def interpolate(name, vars)
|
||||||
|
name.gsub(/:(\w+)/) do
|
||||||
|
vars[$1.to_sym] or raise TranslationError, translate("errors.i18n.undefined_interpolation", :key => $1, :name => name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def deep_fetch_translation(name)
|
||||||
|
name.split('.').reduce(locale) do |level, cur|
|
||||||
|
level[cur] or raise TranslationError, translate("errors.i18n.unknown_translation", :name => name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def locale
|
||||||
|
@locale ||= YAML.load_file(@path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
errors:
|
||||||
|
i18n:
|
||||||
|
unknown_translation: "Translation for :name does not exist in locale"
|
||||||
|
undefined_interpolation: "Undefined key :key for interpolation in translation :name"
|
||||||
|
template:
|
||||||
|
argument_hash_or_context: "Expect Hash or Liquid::Context as parameter"
|
||||||
|
syntax_error:
|
||||||
|
tag_termination: "Tag ':token' was not properly terminated with regexp: :inspection"
|
||||||
@@ -119,7 +119,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, "Expect Hash or Liquid::Context as parameter"
|
raise ArgumentError, I18n.translate("errors.template.argument_hash_or_context")
|
||||||
end
|
end
|
||||||
|
|
||||||
case args.last
|
case args.last
|
||||||
|
|||||||
Vendored
+9
@@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
simple: "less is more"
|
||||||
|
whatever: "something :something"
|
||||||
|
errors:
|
||||||
|
i18n:
|
||||||
|
undefined_interpolation: "undefined key :key"
|
||||||
|
unknown_translation: "translation ':name' wasn't found"
|
||||||
|
syntax:
|
||||||
|
oops: "something wasn't right"
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class I18nTest < Test::Unit::TestCase
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@i18n = I18n.new("./test/fixtures/en_locale.yml")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_simple_translate_string
|
||||||
|
assert_equal "less is more", @i18n.translate("simple")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_nested_translate_string
|
||||||
|
assert_equal "something wasn't right", @i18n.translate("errors.syntax.oops")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_single_string_interpolation
|
||||||
|
assert_equal "something different", @i18n.translate("whatever", :something => "different")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_raises_keyerror_on_undefined_interpolation_key
|
||||||
|
assert_raise I18n::TranslationError do
|
||||||
|
@i18n.translate("whatever", :oopstypos => "yes")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_raises_unknown_translation
|
||||||
|
assert_raise I18n::TranslationError do
|
||||||
|
@i18n.translate("doesnt_exist")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
+3
-1
@@ -7,7 +7,9 @@ begin
|
|||||||
rescue LoadError
|
rescue LoadError
|
||||||
puts "Couldn't load ruby-debug. gem install ruby-debug if you need it."
|
puts "Couldn't load ruby-debug. gem install ruby-debug if you need it."
|
||||||
end
|
end
|
||||||
require File.join(File.dirname(__FILE__), '..', 'lib', 'liquid')
|
|
||||||
|
$:.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib'))
|
||||||
|
require 'liquid.rb'
|
||||||
|
|
||||||
mode = :strict
|
mode = :strict
|
||||||
if env_mode = ENV['LIQUID_PARSER_MODE']
|
if env_mode = ENV['LIQUID_PARSER_MODE']
|
||||||
|
|||||||
Reference in New Issue
Block a user