Rename errors.yml to strings.yml and Error to Translator, since I plan on using it for general strings
This commit is contained in:
parent
4e9653f95e
commit
13c05de702
|
@ -110,7 +110,7 @@ module Vagrant
|
|||
@key = key
|
||||
@data = data
|
||||
|
||||
message = Vagrant::Util::Errors.error_string(key, data)
|
||||
message = Vagrant::Util::Translator.t(key, data)
|
||||
super(message)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,7 +15,7 @@ module Vagrant
|
|||
=====================================================================
|
||||
Vagrant experienced an error!
|
||||
|
||||
#{Errors.error_string(key, data).chomp}
|
||||
#{Translator.error_string(key, data).chomp}
|
||||
=====================================================================
|
||||
error
|
||||
end
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
require 'yaml'
|
||||
|
||||
module Vagrant
|
||||
module Util
|
||||
# This class is responsible for outputting errors. It retrieves the errors,
|
||||
# based on their key, from the error file, and then outputs it.
|
||||
class Errors
|
||||
@@errors = nil
|
||||
|
||||
class <<self
|
||||
# Resets the internal errors hash to nil, forcing a reload on the next
|
||||
# access of {errors}.
|
||||
def reset!
|
||||
@@errors = nil
|
||||
end
|
||||
|
||||
# Returns the hash of errors from the error YML files. This only loads once,
|
||||
# then returns a cached value until {reset!} is called.
|
||||
#
|
||||
# @return [Hash]
|
||||
def errors
|
||||
@@errors ||= YAML.load_file(File.join(PROJECT_ROOT, "templates", "errors.yml"))
|
||||
end
|
||||
|
||||
# Renders the error with the given key and data parameters and returns
|
||||
# the rendered result.
|
||||
#
|
||||
# @return [String]
|
||||
def error_string(key, data = {})
|
||||
template = errors[key] || "Unknown error key: #{key}"
|
||||
TemplateRenderer.render_string(template, data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,35 @@
|
|||
require 'yaml'
|
||||
|
||||
module Vagrant
|
||||
module Util
|
||||
# This class is responsible for reading static messages from the strings.yml file.
|
||||
class Translator
|
||||
@@strings = nil
|
||||
|
||||
class <<self
|
||||
# Resets the internal strings hash to nil, forcing a reload on the next
|
||||
# access of {strings}.
|
||||
def reset!
|
||||
@@strings = nil
|
||||
end
|
||||
|
||||
# Returns the hash of strings from the error YML files. This only loads once,
|
||||
# then returns a cached value until {reset!} is called.
|
||||
#
|
||||
# @return [Hash]
|
||||
def strings
|
||||
@@strings ||= YAML.load_file(File.join(PROJECT_ROOT, "templates", "strings.yml"))
|
||||
end
|
||||
|
||||
# Renders the string with the given key and data parameters and returns
|
||||
# the rendered result.
|
||||
#
|
||||
# @return [String]
|
||||
def t(key, data = {})
|
||||
template = strings[key] || "Unknown strings key: #{key}"
|
||||
TemplateRenderer.render_string(template, data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,56 +1,60 @@
|
|||
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
|
||||
|
||||
class ErrorsUtilTest < Test::Unit::TestCase
|
||||
class TranslatorUtilTest < Test::Unit::TestCase
|
||||
include Vagrant::Util
|
||||
|
||||
setup do
|
||||
@klass = Translator
|
||||
end
|
||||
|
||||
context "loading the errors from the YML" do
|
||||
setup do
|
||||
YAML.stubs(:load_file)
|
||||
Errors.reset!
|
||||
@klass.reset!
|
||||
end
|
||||
|
||||
should "load the file initially, then never again unless reset" do
|
||||
YAML.expects(:load_file).with(File.join(PROJECT_ROOT, "templates", "errors.yml")).once
|
||||
Errors.errors
|
||||
Errors.errors
|
||||
Errors.errors
|
||||
Errors.errors
|
||||
YAML.expects(:load_file).with(File.join(PROJECT_ROOT, "templates", "strings.yml")).once
|
||||
@klass.strings
|
||||
@klass.strings
|
||||
@klass.strings
|
||||
@klass.strings
|
||||
end
|
||||
|
||||
should "reload if reset! is called" do
|
||||
YAML.expects(:load_file).with(File.join(PROJECT_ROOT, "templates", "errors.yml")).twice
|
||||
Errors.errors
|
||||
Errors.reset!
|
||||
Errors.errors
|
||||
YAML.expects(:load_file).with(File.join(PROJECT_ROOT, "templates", "strings.yml")).twice
|
||||
@klass.strings
|
||||
@klass.reset!
|
||||
@klass.strings
|
||||
end
|
||||
end
|
||||
|
||||
context "getting the error string" do
|
||||
context "getting the string translated" do
|
||||
setup do
|
||||
@errors = {}
|
||||
@errors[:foo] = "foo bar baz"
|
||||
Errors.stubs(:errors).returns(@errors)
|
||||
@strings = {}
|
||||
@strings[:foo] = "foo bar baz"
|
||||
@klass.stubs(:strings).returns(@strings)
|
||||
end
|
||||
|
||||
should "render the error string" do
|
||||
TemplateRenderer.expects(:render_string).with(@errors[:foo], anything).once
|
||||
Errors.error_string(:foo)
|
||||
TemplateRenderer.expects(:render_string).with(@strings[:foo], anything).once
|
||||
@klass.t(:foo)
|
||||
end
|
||||
|
||||
should "pass in any data entries" do
|
||||
data = mock("data")
|
||||
TemplateRenderer.expects(:render_string).with(@errors[:foo], data).once
|
||||
Errors.error_string(:foo, data)
|
||||
TemplateRenderer.expects(:render_string).with(@strings[:foo], data).once
|
||||
@klass.t(:foo, data)
|
||||
end
|
||||
|
||||
should "return the result of the render" do
|
||||
result = mock("result")
|
||||
TemplateRenderer.expects(:render_string).returns(result)
|
||||
assert_equal result, Errors.error_string(:foo)
|
||||
assert_equal result, @klass.t(:foo)
|
||||
end
|
||||
|
||||
should "return an unknown if the key doesn't exist" do
|
||||
result = Errors.error_string(:unknown)
|
||||
result = @klass.t(:unknown)
|
||||
assert result =~ /Unknown/i
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue