everythingbehind-i18n 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.textile +20 -0
- data/i18n.gemspec +29 -0
- data/lib/i18n.rb +205 -0
- data/lib/i18n/backend/simple.rb +237 -0
- data/lib/i18n/exceptions.rb +53 -0
- data/lib/i18n/test_helper.rb +20 -0
- data/test/all.rb +6 -0
- data/test/i18n_exceptions_test.rb +100 -0
- data/test/i18n_test.rb +130 -0
- data/test/i18n_test_helper_test.rb +56 -0
- data/test/locale/en.rb +1 -0
- data/test/locale/en.yml +3 -0
- data/test/simple_backend_test.rb +510 -0
- metadata +69 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 The Ruby I18n team
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
h1. Ruby I18n gem
|
2
|
+
|
3
|
+
I18n and localization solution for Ruby.
|
4
|
+
|
5
|
+
For information please refer to http://rails-i18n.org
|
6
|
+
|
7
|
+
h2. Authors
|
8
|
+
|
9
|
+
* "Matt Aimonetti":http://railsontherun.com
|
10
|
+
* "Sven Fuchs":http://www.artweb-design.de
|
11
|
+
* "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
|
12
|
+
* "Saimon Moore":http://saimonmoore.net
|
13
|
+
* "Stephan Soller":http://www.arkanis-development.de
|
14
|
+
|
15
|
+
h2. License
|
16
|
+
|
17
|
+
MIT License. See the included MIT-LICENCE file.
|
18
|
+
|
19
|
+
|
20
|
+
|
data/i18n.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "i18n"
|
3
|
+
s.version = "0.1.1"
|
4
|
+
s.date = "2008-10-26"
|
5
|
+
s.summary = "Internationalization support for Ruby"
|
6
|
+
s.email = "rails-i18n@googlegroups.com"
|
7
|
+
s.homepage = "http://rails-i18n.org"
|
8
|
+
s.description = "Add Internationalization support to your Ruby application."
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.authors = ['Sven Fuchs', 'Joshua Harvey', 'Matt Aimonetti', 'Stephan Soller', 'Saimon Moore']
|
11
|
+
s.files = [
|
12
|
+
'i18n.gemspec',
|
13
|
+
'lib/i18n/backend/simple.rb',
|
14
|
+
'lib/i18n/exceptions.rb',
|
15
|
+
'lib/i18n.rb',
|
16
|
+
'lib/i18n/test_helper.rb',
|
17
|
+
'MIT-LICENSE',
|
18
|
+
'README.textile'
|
19
|
+
]
|
20
|
+
s.test_files = [
|
21
|
+
'test/all.rb',
|
22
|
+
'test/i18n_exceptions_test.rb',
|
23
|
+
'test/i18n_test.rb',
|
24
|
+
'test/locale/en.rb',
|
25
|
+
'test/locale/en.yml',
|
26
|
+
'test/simple_backend_test.rb',
|
27
|
+
'test/i18n_test_helper_test.rb'
|
28
|
+
]
|
29
|
+
end
|
data/lib/i18n.rb
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
# Authors:: Matt Aimonetti (http://railsontherun.com/),
|
2
|
+
# Sven Fuchs (http://www.artweb-design.de),
|
3
|
+
# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
|
4
|
+
# Saimon Moore (http://saimonmoore.net),
|
5
|
+
# Stephan Soller (http://www.arkanis-development.de/)
|
6
|
+
# Copyright:: Copyright (c) 2008 The Ruby i18n Team
|
7
|
+
# License:: MIT
|
8
|
+
require 'i18n/backend/simple'
|
9
|
+
require 'i18n/exceptions'
|
10
|
+
require 'i18n/test_helper'
|
11
|
+
|
12
|
+
module I18n
|
13
|
+
@@backend = nil
|
14
|
+
@@load_path = nil
|
15
|
+
@@default_locale = :'en'
|
16
|
+
@@exception_handler = :default_exception_handler
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# Returns the current backend. Defaults to +Backend::Simple+.
|
20
|
+
def backend
|
21
|
+
@@backend ||= Backend::Simple.new
|
22
|
+
end
|
23
|
+
|
24
|
+
# Sets the current backend. Used to set a custom backend.
|
25
|
+
def backend=(backend)
|
26
|
+
@@backend = backend
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns the current default locale. Defaults to :'en'
|
30
|
+
def default_locale
|
31
|
+
@@default_locale
|
32
|
+
end
|
33
|
+
|
34
|
+
# Sets the current default locale. Used to set a custom default locale.
|
35
|
+
def default_locale=(locale)
|
36
|
+
@@default_locale = locale
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns the current locale. Defaults to I18n.default_locale.
|
40
|
+
def locale
|
41
|
+
Thread.current[:locale] ||= default_locale
|
42
|
+
end
|
43
|
+
|
44
|
+
# Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
|
45
|
+
def locale=(locale)
|
46
|
+
Thread.current[:locale] = locale
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns an array of availiable locales
|
50
|
+
def locales
|
51
|
+
backend.locales
|
52
|
+
end
|
53
|
+
|
54
|
+
# Lists all available translation keys (including scopes) for a given locale
|
55
|
+
def available_translations(locale)
|
56
|
+
backend.available_translations(locale)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Sets the exception handler.
|
60
|
+
def exception_handler=(exception_handler)
|
61
|
+
@@exception_handler = exception_handler
|
62
|
+
end
|
63
|
+
|
64
|
+
# Allow clients to register paths providing translation data sources. The
|
65
|
+
# backend defines acceptable sources.
|
66
|
+
#
|
67
|
+
# E.g. the provided SimpleBackend accepts a list of paths to translation
|
68
|
+
# files which are either named *.rb and contain plain Ruby Hashes or are
|
69
|
+
# named *.yml and contain YAML data. So for the SimpleBackend clients may
|
70
|
+
# register translation files like this:
|
71
|
+
# I18n.load_path << 'path/to/locale/en.yml'
|
72
|
+
def load_path
|
73
|
+
@@load_path ||= []
|
74
|
+
end
|
75
|
+
|
76
|
+
# Sets the load path instance. Custom implementations are expected to
|
77
|
+
# behave like a Ruby Array.
|
78
|
+
def load_path=(load_path)
|
79
|
+
@@load_path = load_path
|
80
|
+
end
|
81
|
+
|
82
|
+
# Tells the backend to reload translations. Used in situations like the
|
83
|
+
# Rails development environment. Backends can implement whatever strategy
|
84
|
+
# is useful.
|
85
|
+
def reload!
|
86
|
+
backend.reload!
|
87
|
+
end
|
88
|
+
|
89
|
+
# Translates, pluralizes and interpolates a given key using a given locale,
|
90
|
+
# scope, and default, as well as interpolation values.
|
91
|
+
#
|
92
|
+
# *LOOKUP*
|
93
|
+
#
|
94
|
+
# Translation data is organized as a nested hash using the upper-level keys
|
95
|
+
# as namespaces. <em>E.g.</em>, ActionView ships with the translation:
|
96
|
+
# <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
|
97
|
+
#
|
98
|
+
# Translations can be looked up at any level of this hash using the key argument
|
99
|
+
# and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
|
100
|
+
# returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
|
101
|
+
#
|
102
|
+
# Key can be either a single key or a dot-separated key (both Strings and Symbols
|
103
|
+
# work). <em>E.g.</em>, the short format can be looked up using both:
|
104
|
+
# I18n.t 'date.formats.short'
|
105
|
+
# I18n.t :'date.formats.short'
|
106
|
+
#
|
107
|
+
# Scope can be either a single key, a dot-separated key or an array of keys
|
108
|
+
# or dot-separated keys. Keys and scopes can be combined freely. So these
|
109
|
+
# examples will all look up the same short date format:
|
110
|
+
# I18n.t 'date.formats.short'
|
111
|
+
# I18n.t 'formats.short', :scope => 'date'
|
112
|
+
# I18n.t 'short', :scope => 'date.formats'
|
113
|
+
# I18n.t 'short', :scope => %w(date formats)
|
114
|
+
#
|
115
|
+
# *INTERPOLATION*
|
116
|
+
#
|
117
|
+
# Translations can contain interpolation variables which will be replaced by
|
118
|
+
# values passed to #translate as part of the options hash, with the keys matching
|
119
|
+
# the interpolation variable names.
|
120
|
+
#
|
121
|
+
# <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
|
122
|
+
# value for the key +bar+ will be interpolated into the translation:
|
123
|
+
# I18n.t :foo, :bar => 'baz' # => 'foo baz'
|
124
|
+
#
|
125
|
+
# *PLURALIZATION*
|
126
|
+
#
|
127
|
+
# Translation data can contain pluralized translations. Pluralized translations
|
128
|
+
# are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
|
129
|
+
#
|
130
|
+
# Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
|
131
|
+
# pluralization rules. Other algorithms can be supported by custom backends.
|
132
|
+
#
|
133
|
+
# This returns the singular version of a pluralized translation:
|
134
|
+
# I18n.t :foo, :count => 1 # => 'Foo'
|
135
|
+
#
|
136
|
+
# These both return the plural version of a pluralized translation:
|
137
|
+
# I18n.t :foo, :count => 0 # => 'Foos'
|
138
|
+
# I18n.t :foo, :count => 2 # => 'Foos'
|
139
|
+
#
|
140
|
+
# The <tt>:count</tt> option can be used both for pluralization and interpolation.
|
141
|
+
# <em>E.g.</em>, with the translation
|
142
|
+
# <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
|
143
|
+
# be interpolated to the pluralized translation:
|
144
|
+
# I18n.t :foo, :count => 1 # => '1 foo'
|
145
|
+
#
|
146
|
+
# *DEFAULTS*
|
147
|
+
#
|
148
|
+
# This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
|
149
|
+
# I18n.t :foo, :default => 'default'
|
150
|
+
#
|
151
|
+
# This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
|
152
|
+
# translation for <tt>:foo</tt> was found:
|
153
|
+
# I18n.t :foo, :default => :bar
|
154
|
+
#
|
155
|
+
# Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
|
156
|
+
# or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
|
157
|
+
# I18n.t :foo, :default => [:bar, 'default']
|
158
|
+
#
|
159
|
+
# <b>BULK LOOKUP</b>
|
160
|
+
#
|
161
|
+
# This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
|
162
|
+
# I18n.t [:foo, :bar]
|
163
|
+
#
|
164
|
+
# Can be used with dot-separated nested keys:
|
165
|
+
# I18n.t [:'baz.foo', :'baz.bar']
|
166
|
+
#
|
167
|
+
# Which is the same as using a scope option:
|
168
|
+
# I18n.t [:foo, :bar], :scope => :baz
|
169
|
+
def translate(key, options = {})
|
170
|
+
locale = options.delete(:locale) || I18n.locale
|
171
|
+
backend.translate(locale, key, options)
|
172
|
+
rescue I18n::ArgumentError => e
|
173
|
+
raise e if options[:raise]
|
174
|
+
send(@@exception_handler, e, locale, key, options)
|
175
|
+
end
|
176
|
+
alias :t :translate
|
177
|
+
|
178
|
+
# Localizes certain objects, such as dates and numbers to local formatting.
|
179
|
+
def localize(object, options = {})
|
180
|
+
locale = options[:locale] || I18n.locale
|
181
|
+
format = options[:format] || :default
|
182
|
+
backend.localize(locale, object, format)
|
183
|
+
end
|
184
|
+
alias :l :localize
|
185
|
+
|
186
|
+
protected
|
187
|
+
# Handles exceptions raised in the backend. All exceptions except for
|
188
|
+
# MissingTranslationData exceptions are re-raised. When a MissingTranslationData
|
189
|
+
# was caught and the option :raise is not set the handler returns an error
|
190
|
+
# message string containing the key/scope.
|
191
|
+
def default_exception_handler(exception, locale, key, options)
|
192
|
+
return exception.message if MissingTranslationData === exception
|
193
|
+
raise exception
|
194
|
+
end
|
195
|
+
|
196
|
+
# Merges the given locale, key and scope into a single array of keys.
|
197
|
+
# Splits keys that contain dots into multiple keys. Makes sure all
|
198
|
+
# keys are Symbols.
|
199
|
+
def normalize_translation_keys(locale, key, scope)
|
200
|
+
keys = [locale] + Array(scope) + [key]
|
201
|
+
keys = keys.map { |k| k.to_s.split(/\./) }
|
202
|
+
keys.flatten.map { |k| k.to_sym }
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
@@ -0,0 +1,237 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module I18n
|
4
|
+
module Backend
|
5
|
+
class Simple
|
6
|
+
INTERPOLATION_RESERVED_KEYS = %w(scope default)
|
7
|
+
MATCH = /(\\\\)?\{\{([^\}]+)\}\}/
|
8
|
+
|
9
|
+
# Accepts a list of paths to translation files. Loads translations from
|
10
|
+
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
|
11
|
+
# for details.
|
12
|
+
def load_translations(*filenames)
|
13
|
+
filenames.each { |filename| load_file(filename) }
|
14
|
+
end
|
15
|
+
|
16
|
+
# Stores translations for the given locale in memory.
|
17
|
+
# This uses a deep merge for the translations hash, so existing
|
18
|
+
# translations will be overwritten by new ones only at the deepest
|
19
|
+
# level of the hash.
|
20
|
+
def store_translations(locale, data)
|
21
|
+
merge_translations(locale, data)
|
22
|
+
end
|
23
|
+
|
24
|
+
def translate(locale, key, options = {})
|
25
|
+
raise InvalidLocale.new(locale) if locale.nil?
|
26
|
+
return key.map { |k| translate(locale, k, options) } if key.is_a? Array
|
27
|
+
|
28
|
+
reserved = :scope, :default
|
29
|
+
count, scope, default = options.values_at(:count, *reserved)
|
30
|
+
options.delete(:default)
|
31
|
+
values = options.reject { |name, value| reserved.include?(name) }
|
32
|
+
|
33
|
+
entry = lookup(locale, key, scope)
|
34
|
+
if entry.nil?
|
35
|
+
entry = default(locale, default, options)
|
36
|
+
if entry.nil?
|
37
|
+
raise(I18n::MissingTranslationData.new(locale, key, options))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
entry = pluralize(locale, entry, count)
|
41
|
+
entry = interpolate(locale, entry, values)
|
42
|
+
entry
|
43
|
+
end
|
44
|
+
|
45
|
+
# Acts the same as +strftime+, but returns a localized version of the
|
46
|
+
# formatted date string. Takes a key from the date/time formats
|
47
|
+
# translations as a format argument (<em>e.g.</em>, <tt>:short</tt> in <tt>:'date.formats'</tt>).
|
48
|
+
def localize(locale, object, format = :default)
|
49
|
+
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
|
50
|
+
|
51
|
+
type = object.respond_to?(:sec) ? 'time' : 'date'
|
52
|
+
# TODO only translate these if format is a String?
|
53
|
+
formats = translate(locale, :"#{type}.formats")
|
54
|
+
format = formats[format.to_sym] if formats && formats[format.to_sym]
|
55
|
+
# TODO raise exception unless format found?
|
56
|
+
format = format.to_s.dup
|
57
|
+
|
58
|
+
# TODO only translate these if the format string is actually present
|
59
|
+
# TODO check which format strings are present, then bulk translate then, then replace them
|
60
|
+
format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday])
|
61
|
+
format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday])
|
62
|
+
format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])
|
63
|
+
format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon])
|
64
|
+
format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
|
65
|
+
object.strftime(format)
|
66
|
+
end
|
67
|
+
|
68
|
+
def initialized?
|
69
|
+
@initialized ||= false
|
70
|
+
end
|
71
|
+
|
72
|
+
def reload!
|
73
|
+
@initialized = false
|
74
|
+
@translations = nil
|
75
|
+
end
|
76
|
+
|
77
|
+
def locales
|
78
|
+
translations.keys
|
79
|
+
end
|
80
|
+
|
81
|
+
def available_translations(locale)
|
82
|
+
flatten_hash_tree_keys(translations[locale])
|
83
|
+
end
|
84
|
+
|
85
|
+
protected
|
86
|
+
def init_translations
|
87
|
+
load_translations(*I18n.load_path)
|
88
|
+
@initialized = true
|
89
|
+
end
|
90
|
+
|
91
|
+
def translations
|
92
|
+
@translations ||= {}
|
93
|
+
end
|
94
|
+
|
95
|
+
# Looks up a translation from the translations hash. Returns nil if
|
96
|
+
# eiher key is nil, or locale, scope or key do not exist as a key in the
|
97
|
+
# nested translations hash. Splits keys or scopes containing dots
|
98
|
+
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
|
99
|
+
# <tt>%w(currency format)</tt>.
|
100
|
+
def lookup(locale, key, scope = [])
|
101
|
+
return unless key
|
102
|
+
init_translations unless initialized?
|
103
|
+
keys = I18n.send(:normalize_translation_keys, locale, key, scope)
|
104
|
+
keys.inject(translations) do |result, k|
|
105
|
+
if (x = result[k.to_sym]).nil?
|
106
|
+
return nil
|
107
|
+
else
|
108
|
+
x
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Evaluates a default translation.
|
114
|
+
# If the given default is a String it is used literally. If it is a Symbol
|
115
|
+
# it will be translated with the given options. If it is an Array the first
|
116
|
+
# translation yielded will be returned.
|
117
|
+
#
|
118
|
+
# <em>I.e.</em>, <tt>default(locale, [:foo, 'default'])</tt> will return +default+ if
|
119
|
+
# <tt>translate(locale, :foo)</tt> does not yield a result.
|
120
|
+
def default(locale, default, options = {})
|
121
|
+
case default
|
122
|
+
when String then default
|
123
|
+
when Symbol then translate locale, default, options
|
124
|
+
when Array then default.each do |obj|
|
125
|
+
result = default(locale, obj, options.dup) and return result
|
126
|
+
end and nil
|
127
|
+
end
|
128
|
+
rescue MissingTranslationData
|
129
|
+
nil
|
130
|
+
end
|
131
|
+
|
132
|
+
# Picks a translation from an array according to English pluralization
|
133
|
+
# rules. It will pick the first translation if count is not equal to 1
|
134
|
+
# and the second translation if it is equal to 1. Other backends can
|
135
|
+
# implement more flexible or complex pluralization rules.
|
136
|
+
def pluralize(locale, entry, count)
|
137
|
+
return entry unless entry.is_a?(Hash) and count
|
138
|
+
# raise InvalidPluralizationData.new(entry, count) unless entry.is_a?(Hash)
|
139
|
+
key = :zero if count == 0 && entry.has_key?(:zero)
|
140
|
+
key ||= count == 1 ? :one : :other
|
141
|
+
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
|
142
|
+
entry[key]
|
143
|
+
end
|
144
|
+
|
145
|
+
# Interpolates values into a given string.
|
146
|
+
#
|
147
|
+
# interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X'
|
148
|
+
# # => "file test.txt opened by {{user}}"
|
149
|
+
#
|
150
|
+
# Note that you have to double escape the <tt>\\</tt> when you want to escape
|
151
|
+
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
|
152
|
+
# interpolation).
|
153
|
+
def interpolate(locale, string, values = {})
|
154
|
+
return string unless string.is_a?(String)
|
155
|
+
|
156
|
+
if string.respond_to?(:force_encoding)
|
157
|
+
original_encoding = string.encoding
|
158
|
+
string.force_encoding(Encoding::BINARY)
|
159
|
+
end
|
160
|
+
|
161
|
+
result = string.gsub(MATCH) do
|
162
|
+
escaped, pattern, key = $1, $2, $2.to_sym
|
163
|
+
|
164
|
+
if escaped
|
165
|
+
pattern
|
166
|
+
elsif INTERPOLATION_RESERVED_KEYS.include?(pattern)
|
167
|
+
raise ReservedInterpolationKey.new(pattern, string)
|
168
|
+
elsif !values.include?(key)
|
169
|
+
raise MissingInterpolationArgument.new(pattern, string)
|
170
|
+
else
|
171
|
+
values[key].to_s
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
result.force_encoding(original_encoding) if original_encoding
|
176
|
+
result
|
177
|
+
end
|
178
|
+
|
179
|
+
# Loads a single translations file by delegating to #load_rb or
|
180
|
+
# #load_yml depending on the file extension and directly merges the
|
181
|
+
# data to the existing translations. Raises I18n::UnknownFileType
|
182
|
+
# for all other file extensions.
|
183
|
+
def load_file(filename)
|
184
|
+
type = File.extname(filename).tr('.', '').downcase
|
185
|
+
raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}")
|
186
|
+
data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
|
187
|
+
data.each { |locale, d| merge_translations(locale, d) }
|
188
|
+
end
|
189
|
+
|
190
|
+
# Loads a plain Ruby translations file. eval'ing the file must yield
|
191
|
+
# a Hash containing translation data with locales as toplevel keys.
|
192
|
+
def load_rb(filename)
|
193
|
+
eval(IO.read(filename), binding, filename)
|
194
|
+
end
|
195
|
+
|
196
|
+
# Loads a YAML translations file. The data must have locales as
|
197
|
+
# toplevel keys.
|
198
|
+
def load_yml(filename)
|
199
|
+
YAML::load(IO.read(filename))
|
200
|
+
end
|
201
|
+
|
202
|
+
# Deep merges the given translations hash with the existing translations
|
203
|
+
# for the given locale
|
204
|
+
def merge_translations(locale, data)
|
205
|
+
locale = locale.to_sym
|
206
|
+
translations[locale] ||= {}
|
207
|
+
data = deep_symbolize_keys(data)
|
208
|
+
|
209
|
+
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
|
210
|
+
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
211
|
+
translations[locale].merge!(data, &merger)
|
212
|
+
end
|
213
|
+
|
214
|
+
# Return a new hash with all keys and nested keys converted to symbols.
|
215
|
+
def deep_symbolize_keys(hash)
|
216
|
+
hash.inject({}) { |result, (key, value)|
|
217
|
+
value = deep_symbolize_keys(value) if value.is_a? Hash
|
218
|
+
result[(key.to_sym rescue key) || key] = value
|
219
|
+
result
|
220
|
+
}
|
221
|
+
end
|
222
|
+
|
223
|
+
# Walks a given tree of hashes, returning an array of possible walks through the tree.
|
224
|
+
# flatten_hash_tree_keys({:a => { :b => 3, :c => 4 } }) # => [ [:a, :b], [:a, :c] ]
|
225
|
+
def flatten_hash_tree_keys(tree)
|
226
|
+
tree.map{|key, value|
|
227
|
+
case value
|
228
|
+
when Hash
|
229
|
+
flatten_hash_tree_keys(value).map{|normalised_subtree| [key] + [normalised_subtree] }
|
230
|
+
else
|
231
|
+
[key]
|
232
|
+
end
|
233
|
+
}
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|