Add sketch of I18n error translation

This commit is contained in:
Simon Eskildsen
2013-08-14 23:28:40 -04:00
parent 0e41c2c6e9
commit f37a984fd7
7 changed files with 102 additions and 2 deletions

View File

@@ -48,6 +48,7 @@ end
require "liquid/version"
require 'liquid/lexer'
require 'liquid/parser'
require 'liquid/i18n'
require 'liquid/drop'
require 'liquid/extensions'
require 'liquid/errors'

46
lib/liquid/i18n.rb Normal file
View File

@@ -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

View File

@@ -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"

View File

@@ -119,7 +119,7 @@ module Liquid
when nil
Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits)
else
raise ArgumentError, "Expect Hash or Liquid::Context as parameter"
raise ArgumentError, I18n.translate("errors.template.argument_hash_or_context")
end
case args.last

9
test/fixtures/en_locale.yml vendored Normal file
View File

@@ -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"

33
test/liquid/i18n_test.rb Normal file
View File

@@ -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

View File

@@ -7,7 +7,9 @@ begin
rescue LoadError
puts "Couldn't load ruby-debug. gem install ruby-debug if you need it."
end
require File.join(File.dirname(__FILE__), '..', 'lib', 'liquid')
$:.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib'))
require 'liquid.rb'
mode = :strict
if env_mode = ENV['LIQUID_PARSER_MODE']