Permalink
Browse files

update i18n gem and clean up dir structure within i18n-0.0.1

  • Loading branch information...
1 parent bdf783b commit 572f73fae62972cc5aebba24bed45a127f2a4cac Sven Fuchs committed Aug 10, 2008
@@ -1,20 +0,0 @@
-Copyright (c) 2008 The Ruby I18n team
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,18 +0,0 @@
-h1. Ruby I18n gem
-
-I18n and localization solution for Ruby.
-
-h2. Authors
-
-* "Matt Aimonetti":http://railsontherun.com
-* "Sven Fuchs":http://www.artweb-design.de
-* "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
-* "Saimon Moore":http://saimonmoore.net
-* "Stephan Soller":http://www.arkanis-development.de
-
-h2. License
-
-MIT License. See the included MIT-LICENCE file.
-
-
-
@@ -1,24 +0,0 @@
-Gem::Specification.new do |s|
- s.name = "i18n"
- s.version = "0.0.1"
- s.date = "2008-06-13"
- s.summary = "Internationalization for Ruby"
- s.email = "rails-patch-i18n@googlegroups.com"
- s.homepage = "http://groups.google.com/group/rails-patch-i18n"
- s.description = "Add Internationalization to your Ruby application."
- s.has_rdoc = false
- s.authors = ['Sven Fuchs', 'Matt Aimonetti', 'Stephan Soller', 'Saimon Moore']
- s.files = [
- "lib/i18n/backend/minimal.rb",
- "lib/i18n/core_ext.rb",
- "lib/i18n/localization.rb",
- "lib/i18n/translation.rb",
- "lib/i18n.rb",
- "LICENSE",
- "README",
- "spec/core_ext_spec.rb",
- "spec/i18n_spec.rb",
- "spec/spec.opts",
- "spec/spec_helper.rb"
- ]
-end
@@ -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
@@ -173,8 +177,8 @@ def default_exception_handler(exception, locale, key, options)
# keys are Symbols.
def normalize_translation_keys(locale, key, scope)
keys = [locale] + Array(scope) + [key]
- keys = keys.map{|k| k.to_s.split(/\./) }
- keys.flatten.map{|k| k.to_sym}
+ keys = keys.map{|key| key.to_s.split(/\./) }
+ keys.flatten.map{|key| key.to_sym}
end
end
end
@@ -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
@@ -23,16 +27,16 @@ def store_translations(locale, data)
def translate(locale, key, options = {})
raise InvalidLocale.new(locale) if locale.nil?
- return key.map{|k| translate locale, k, options } if key.is_a? Array
+ return key.map{|key| translate locale, key, options } if key.is_a? Array
reserved = :scope, :default
count, scope, default = options.values_at(:count, *reserved)
options.delete(:default)
values = options.reject{|name, value| reserved.include? name }
entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
- entry = pluralize entry, count
- entry = interpolate entry, values
+ entry = pluralize locale, entry, count
+ entry = interpolate locale, entry, values
entry
end
@@ -66,7 +70,7 @@ def localize(locale, object, format = :default)
def lookup(locale, key, scope = [])
return unless key
keys = I18n.send :normalize_translation_keys, locale, key, scope
- keys.inject(@@translations){|result, k| result[k.to_sym] or return nil }
+ keys.inject(@@translations){|result, key| result[key.to_sym] or return nil }
end
# Evaluates a default translation.
@@ -92,10 +96,13 @@ def default(locale, default, options = {})
# rules. It will pick the first translation if count is not equal to 1
# and the second translation if it is equal to 1. Other backends can
# implement more flexible or complex pluralization rules.
- def pluralize(entry, count)
- return entry unless entry.is_a?(Array) and count
- raise InvalidPluralizationData.new(entry, count) unless entry.size == 2
- entry[count == 1 ? 0 : 1]
+ def pluralize(locale, entry, count)
+ return entry unless entry.is_a?(Hash) and count
+ # raise InvalidPluralizationData.new(entry, count) unless entry.is_a?(Hash)
+ key = :zero if count == 0 && entry.has_key?(:zero)
+ key ||= count == 1 ? :one : :many
+ raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
+ entry[key]
end
# Interpolates values into a given string.
@@ -106,7 +113,7 @@ def pluralize(entry, count)
# Note that you have to double escape the <tt>\\</tt> when you want to escape
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
# interpolation).
- def interpolate(string, values = {})
+ def interpolate(locale, string, values = {})
return string if !string.is_a?(String)
map = {'%d' => '{{count}}', '%s' => '{{value}}'} # TODO deprecate this?
@@ -128,6 +135,23 @@ def interpolate(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)
@@ -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
@@ -1,4 +0,0 @@
-dir = File.dirname(__FILE__)
-require dir + '/i18n_test.rb'
-require dir + '/simple_backend_test.rb'
-require dir + '/i18n_exceptions_test.rb'
@@ -1,100 +0,0 @@
-$:.unshift "lib"
-
-require 'rubygems'
-require 'test/unit'
-require 'mocha'
-require 'i18n'
-require 'active_support'
-
-class I18nExceptionsTest < Test::Unit::TestCase
- def test_invalid_locale_stores_locale
- force_invalid_locale
- rescue I18n::ArgumentError => e
- assert_nil e.locale
- end
-
- def test_invalid_locale_message
- force_invalid_locale
- rescue I18n::ArgumentError => e
- assert_equal 'nil is not a valid locale', e.message
- end
-
- def test_missing_translation_data_stores_locale_key_and_options
- force_missing_translation_data
- rescue I18n::ArgumentError => e
- options = {:scope => :bar}
- assert_equal 'de-DE', e.locale
- assert_equal :foo, e.key
- assert_equal options, e.options
- end
-
- def test_missing_translation_data_message
- force_missing_translation_data
- rescue I18n::ArgumentError => e
- assert_equal 'translation missing: de-DE, bar, foo', e.message
- end
-
- def test_invalid_pluralization_data_stores_entry_and_count
- force_invalid_pluralization_data
- rescue I18n::ArgumentError => e
- assert_equal [:bar], e.entry
- assert_equal 1, e.count
- end
-
- def test_invalid_pluralization_data_message
- force_invalid_pluralization_data
- rescue I18n::ArgumentError => e
- assert_equal 'translation data [:bar] can not be used with :count => 1', e.message
- end
-
- def test_missing_interpolation_argument_stores_key_and_string
- force_missing_interpolation_argument
- rescue I18n::ArgumentError => e
- assert_equal 'bar', e.key
- assert_equal "{{bar}}", e.string
- end
-
- def test_missing_interpolation_argument_message
- force_missing_interpolation_argument
- rescue I18n::ArgumentError => e
- assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message
- end
-
- def test_reserved_interpolation_key_stores_key_and_string
- force_reserved_interpolation_key
- rescue I18n::ArgumentError => e
- assert_equal 'scope', e.key
- assert_equal "{{scope}}", e.string
- end
-
- def test_reserved_interpolation_key_message
- force_reserved_interpolation_key
- rescue I18n::ArgumentError => e
- assert_equal 'reserved key "scope" used in "{{scope}}"', e.message
- end
-
- private
- def force_invalid_locale
- I18n.backend.translate nil, :foo
- end
-
- def force_missing_translation_data
- I18n.store_translations 'de-DE', :bar => nil
- I18n.backend.translate 'de-DE', :foo, :scope => :bar
- end
-
- def force_invalid_pluralization_data
- I18n.store_translations 'de-DE', :foo => [:bar]
- I18n.backend.translate 'de-DE', :foo, :count => 1
- end
-
- def force_missing_interpolation_argument
- I18n.store_translations 'de-DE', :foo => "{{bar}}"
- I18n.backend.translate 'de-DE', :foo, :baz => 'baz'
- end
-
- def force_reserved_interpolation_key
- I18n.store_translations 'de-DE', :foo => "{{scope}}"
- I18n.backend.translate 'de-DE', :foo, :baz => 'baz'
- end
-end
Oops, something went wrong.

0 comments on commit 572f73f

Please sign in to comment.