Permalink
Browse files

experimenting with load-from-files functionality

  • Loading branch information...
Sven Fuchs
Sven Fuchs committed Aug 9, 2008
1 parent 37be3f5 commit c6428a0bafc48f61e32be4f6a5806653d15c169e
Showing with 94 additions and 0 deletions.
  1. +4 −0 lib/i18n.rb
  2. +21 −0 lib/i18n/backend/simple.rb
  3. +8 −0 lib/i18n/exceptions.rb
  4. +1 −0 test/locale/en-US.rb
  5. +3 −0 test/locale/en-US.yml
  6. +57 −0 test/simple_backend_test.rb
View
@@ -56,6 +56,10 @@ def populate(&block)
backend.populate &block
end
+ def load_translations(filename)
+ backend.load_translations filename
+ end
+
# Stores translations for the given locale in the backend.
def store_translations(locale, data)
backend.store_translations locale, data
View
@@ -12,6 +12,10 @@ class << self
def populate(&block)
yield
end
+
+ def load_translations(*filenames)
+ filenames.each {|filename| load_file filename }
+ end
# Stores translations for the given locale in memory.
# This uses a deep merge for the translations hash, so existing
@@ -131,6 +135,23 @@ def interpolate(locale, string, values = {})
s.string
end
+ def load_file(filename)
+ type = File.extname(filename).tr('.', '').downcase
+ raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}"
+ data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
+ data.each do |locale, data|
+ merge_translations locale, data
+ end
+ end
+
+ def load_rb(filename)
+ eval IO.read(filename), binding, filename
+ end
+
+ def load_yml(filename)
+ YAML::load IO.read(filename)
+ end
+
# Deep merges the given translations hash with the existing translations
# for the given locale
def merge_translations(locale, data)
View
@@ -42,4 +42,12 @@ def initialize(key, string)
super "reserved key #{key.inspect} used in #{string.inspect}"
end
end
+
+ class UnknownFileType < ArgumentError
+ attr_reader :type, :filename
+ def initialize(type, filename)
+ @type, @filename = type, filename
+ super "can not load translations from #{filename}, the file type #{type} is not known"
+ end
+ end
end
View
@@ -0,0 +1 @@
+{:'en-US-Ruby' => {:foo => {:bar => "baz"}}}
View
@@ -0,0 +1,3 @@
+en-US-Yaml:
+ foo:
+ bar: baz
@@ -5,12 +5,14 @@
require 'mocha'
require 'i18n'
require 'Time'
+require 'Yaml'
module I18nSimpleBackendTestSetup
def setup_backend
@backend = I18n::Backend::Simple
@backend.send :class_variable_set, :@@translations, {}
@backend.store_translations 'en-US', :foo => {:bar => 'bar', :baz => 'baz'}
+ @locale_dir = File.dirname(__FILE__) + '/locale'
end
alias :setup :setup_backend
@@ -418,3 +420,58 @@ def test_deep_symbolize_keys_works
assert_equal expected, result
end
end
+
+class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_load_translations_with_unknown_file_type_raises_exception
+ assert_raises(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en-US.xml" }
+ end
+
+ def test_load_translations_with_ruby_file_type_does_not_raise_exception
+ assert_nothing_raised { @backend.load_translations "#{@locale_dir}/en-US.rb" }
+ end
+
+ def test_load_rb_loads_data_from_ruby_file
+ data = @backend.send :load_rb, "#{@locale_dir}/en-US.rb"
+ assert_equal({:'en-US-Ruby' => {:foo => {:bar => "baz"}}}, data)
+ end
+
+ def test_load_rb_loads_data_from_yaml_file
+ data = @backend.send :load_yml, "#{@locale_dir}/en-US.yml"
+ assert_equal({'en-US-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
+ end
+
+ def test_load_translations_loads_from_different_file_formats
+ @backend.send :class_variable_set, :@@translations, {} # reset translations
+ @backend.load_translations "#{@locale_dir}/en-US.rb", "#{@locale_dir}/en-US.yml"
+ expected = {
+ :'en-US-Ruby' => {:foo => {:bar => "baz"}},
+ :'en-US-Yaml' => {:foo => {:bar => "baz"}}
+ }
+ result = @backend.send :class_variable_get, :@@translations
+ assert_equal expected, result
+ end
+end
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

0 comments on commit c6428a0

Please sign in to comment.