r18n-rails-api 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +166 -0
- data/README.rdoc +74 -0
- data/lib/r18n-rails-api.rb +26 -0
- data/lib/r18n-rails-api/backend.rb +99 -0
- data/lib/r18n-rails-api/filters.rb +44 -0
- data/lib/r18n-rails-api/loader.rb +95 -0
- data/spec/backend_spec.rb +69 -0
- data/spec/data/general/en.yml +21 -0
- data/spec/data/other/en.yml +2 -0
- data/spec/data/other/ru.yml +2 -0
- data/spec/data/simple/en.yml +7 -0
- data/spec/data/simple/ru.rb +1 -0
- data/spec/data/simple/russian.yml +2 -0
- data/spec/filters_spec.rb +28 -0
- data/spec/loader_spec.rb +36 -0
- data/spec/spec_helper.rb +12 -0
- metadata +89 -0
data/LICENSE
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
166
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
= R18n Rails API
|
2
|
+
|
3
|
+
Rails I18n compatibility for R18n:
|
4
|
+
* filters to work with named variables in Rails translation format;
|
5
|
+
* R18n loader for Rails I18n translation format;
|
6
|
+
* R18n backend.
|
7
|
+
|
8
|
+
== How To
|
9
|
+
|
10
|
+
=== Variables
|
11
|
+
|
12
|
+
After require <tt>r18n-rails-api</tt> you can use named variables in any
|
13
|
+
translations:
|
14
|
+
|
15
|
+
greeting: "Hi, {{name}}"
|
16
|
+
users: !!pl
|
17
|
+
1: One user
|
18
|
+
n: {{count}} users
|
19
|
+
|
20
|
+
require 'r18n-rails-api'
|
21
|
+
|
22
|
+
i18n.greeting(:name => 'John') #=> "Hi, John"
|
23
|
+
i18n.users(:count => 5) #=> "5 users"
|
24
|
+
|
25
|
+
=== Rails Translations
|
26
|
+
|
27
|
+
You can use <tt>R18n::Loader::Rails</tt> to load translations from
|
28
|
+
<tt>I18n.load_path</tt>:
|
29
|
+
|
30
|
+
i18n/en.yml:
|
31
|
+
en:
|
32
|
+
posts:
|
33
|
+
one: One post
|
34
|
+
many: {{count}} posts
|
35
|
+
|
36
|
+
example.rb:
|
37
|
+
require 'r18n-rails-api'
|
38
|
+
|
39
|
+
I18n.load_path = ['i18n/en.yml']
|
40
|
+
i18n = R18n::I18n.new('en', R18n::Loader::Rails)
|
41
|
+
|
42
|
+
i18n.posts(:count => 5) #=> "5 posts"
|
43
|
+
|
44
|
+
=== Backend
|
45
|
+
|
46
|
+
You can use R18n as backend for Rails I18n:
|
47
|
+
|
48
|
+
require 'r18n-rails-api'
|
49
|
+
|
50
|
+
R18n.set R18n::I18n.new('en', 'path/to/translation')
|
51
|
+
I18n.backend = R18n::Backend
|
52
|
+
|
53
|
+
I18n.l Time.now, :format => :full #=> "6th of December, 2009 22:44"
|
54
|
+
I18n.t :greeting, :name => 'John' #=> "Hi, John"
|
55
|
+
I18n.t :users, :count => 5 #=> "5 users"
|
56
|
+
|
57
|
+
== R18n Features
|
58
|
+
|
59
|
+
* Nice Ruby-style syntax.
|
60
|
+
* Filters.
|
61
|
+
* Flexible locales.
|
62
|
+
* Custom translations loaders.
|
63
|
+
* Translation support for any classes.
|
64
|
+
* Time and number localization.
|
65
|
+
* Several user language support.
|
66
|
+
|
67
|
+
== License
|
68
|
+
|
69
|
+
R18n is licensed under the GNU Lesser General Public License version 3.
|
70
|
+
You can read it in LICENSE file or in http://www.gnu.org/licenses/lgpl.html.
|
71
|
+
|
72
|
+
== Author
|
73
|
+
|
74
|
+
Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
=begin
|
2
|
+
Rails I18n compatibility for R18n.
|
3
|
+
|
4
|
+
Copyright (C) 2009 Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
5
|
+
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
9
|
+
(at your option) any later version.
|
10
|
+
|
11
|
+
This program is distributed in the hope that it will be useful,
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
GNU Lesser General Public License for more details.
|
15
|
+
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
17
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
=end
|
19
|
+
|
20
|
+
require 'pathname'
|
21
|
+
require 'r18n-core'
|
22
|
+
|
23
|
+
dir = Pathname(__FILE__).dirname.expand_path + 'r18n-rails-api'
|
24
|
+
require dir + 'filters'
|
25
|
+
require dir + 'loader'
|
26
|
+
require dir + 'backend'
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
=begin
|
3
|
+
R18n backend for Rails I18n.
|
4
|
+
|
5
|
+
Copyright (C) 2009 Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
6
|
+
|
7
|
+
This program is free software: you can redistribute it and/or modify
|
8
|
+
it under the terms of the GNU Lesser General Public License as published by
|
9
|
+
the Free Software Foundation, either version 3 of the License, or
|
10
|
+
(at your option) any later version.
|
11
|
+
|
12
|
+
This program is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
GNU Lesser General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU Lesser General Public License
|
18
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
=end
|
20
|
+
|
21
|
+
module R18n
|
22
|
+
# R18n backend for Rails I18n. You must set R18n I18n object before use this
|
23
|
+
# backend:
|
24
|
+
#
|
25
|
+
# R18n.set R18n::I18n.new(locales, R18n::Loader::Rails)
|
26
|
+
#
|
27
|
+
# I18n.l Time.now, :format => :full #=> "6th of December, 2009 22:44"
|
28
|
+
class Backend
|
29
|
+
RESERVED_KEYS = [:scope, :default, :separator]
|
30
|
+
|
31
|
+
# Find translation in R18n. It didn’t use +locale+ argument, only current
|
32
|
+
# R18n I18n object. Also it doesn’t support Proc and variables in +default+
|
33
|
+
# String option.
|
34
|
+
def translate(locale, key, options = {})
|
35
|
+
return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)
|
36
|
+
|
37
|
+
scope, default, separator = options.values_at(*RESERVED_KEYS)
|
38
|
+
params = options.reject { |name, value| RESERVED_KEYS.include?(name) }
|
39
|
+
|
40
|
+
result = lookup(scope, key, separator, params)
|
41
|
+
|
42
|
+
if result.translated?
|
43
|
+
result
|
44
|
+
else
|
45
|
+
options = options.reject { |key, value| key == :default }
|
46
|
+
|
47
|
+
Array(default).each do |entry|
|
48
|
+
if entry.is_a? Symbol
|
49
|
+
value = lookup(scope, entry, separator, params)
|
50
|
+
return value if value.translated?
|
51
|
+
else
|
52
|
+
return entry
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
raise ::I18n::MissingTranslationData.new(locale, key, options)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Convert +object+ to String, according to the rules of the current
|
61
|
+
# R18n locale. It didn’t use +locale+ argument, only current R18n I18n
|
62
|
+
# object. It support Fixnum, Bignum, Float, Time, Date and DateTime.
|
63
|
+
#
|
64
|
+
# Support Rails I18n (+:default+, +:short+, +:long+, +:long_ordinal+,
|
65
|
+
# +:only_day+ and +:only_second+) and R18n (+:full+, +:human+, +:standard+
|
66
|
+
# and +:month+) time formatters.
|
67
|
+
def localize(locale, object, format = :default, options = {})
|
68
|
+
if format.is_a? Symbol
|
69
|
+
key = format
|
70
|
+
type = object.respond_to?(:sec) ? 'time' : 'date'
|
71
|
+
format = R18n.get[type].formats[key] | format
|
72
|
+
end
|
73
|
+
R18n.get.localize(object, format)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Return array of available locales codes.
|
77
|
+
def available_locales
|
78
|
+
R18n.get.available_locales.map { |i| i.code.to_sym }
|
79
|
+
end
|
80
|
+
|
81
|
+
# Reload R18n I18n object.
|
82
|
+
def reload!
|
83
|
+
R18n.get.reload!
|
84
|
+
end
|
85
|
+
|
86
|
+
protected
|
87
|
+
|
88
|
+
# Find translation by <tt>scope.key(params)</tt> in current R18n I18n
|
89
|
+
# object.
|
90
|
+
def lookup(scope, key, separator, params)
|
91
|
+
keys = (Array(scope) + Array(key)).map { |k|
|
92
|
+
k.to_s.split(separator || ::I18n.default_separator) }.flatten
|
93
|
+
last = keys.pop
|
94
|
+
|
95
|
+
result = keys.inject(R18n.get) { |result, key| result[key] }
|
96
|
+
result[last, params]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
=begin
|
2
|
+
Filters for Rails I18n compatibility for R18n.
|
3
|
+
|
4
|
+
Copyright (C) 2009 Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
5
|
+
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
9
|
+
(at your option) any later version.
|
10
|
+
|
11
|
+
This program is distributed in the hope that it will be useful,
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
GNU Lesser General Public License for more details.
|
15
|
+
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
17
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
=end
|
19
|
+
|
20
|
+
# Filter to use Rails named variables:
|
21
|
+
#
|
22
|
+
# name: "My name is {{name}}"
|
23
|
+
#
|
24
|
+
# i18n.name(name: 'Ivan') #=> "My name is Ivan"
|
25
|
+
R18n::Filters.add(String, :named_variables) do |content, config, params|
|
26
|
+
if params.is_a? Hash
|
27
|
+
content = content.clone
|
28
|
+
params.each_pair do |name, value|
|
29
|
+
content.gsub! "{{#{name}}}", config[:locale].localize(value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
content
|
33
|
+
end
|
34
|
+
|
35
|
+
# Pluralization by named variable <tt>{{count}}</tt>.
|
36
|
+
R18n::Filters.add('pl', :named_pluralization) do |content, config, param|
|
37
|
+
if param.is_a? Hash and param.has_key? :count
|
38
|
+
type = config[:locale].pluralize(param[:count])
|
39
|
+
type = 'n' if not content.has_key? type
|
40
|
+
content[type]
|
41
|
+
else
|
42
|
+
content
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
=begin
|
3
|
+
Loader for Rails translations.
|
4
|
+
|
5
|
+
Copyright (C) 2009 Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
6
|
+
|
7
|
+
This program is free software: you can redistribute it and/or modify
|
8
|
+
it under the terms of the GNU Lesser General Public License as published by
|
9
|
+
the Free Software Foundation, either version 3 of the License, or
|
10
|
+
(at your option) any later version.
|
11
|
+
|
12
|
+
This program is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
GNU Lesser General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU Lesser General Public License
|
18
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
=end
|
20
|
+
|
21
|
+
require 'i18n'
|
22
|
+
|
23
|
+
module R18n
|
24
|
+
module Loader
|
25
|
+
# Loader for translations in Rails I18n format:
|
26
|
+
#
|
27
|
+
# R18n::I18n.new('en', R18n::Loader::Rails.new)
|
28
|
+
#
|
29
|
+
# It use Rails I18n backend to load translations. By default, simple
|
30
|
+
# backend will be used, by you can change it, if use extended backend
|
31
|
+
# (for example, with ActiveRecord storage):
|
32
|
+
#
|
33
|
+
# R18n::I18n.new('en',
|
34
|
+
# R18n::Loader::Rails.new(I18n::Backend::ActiveRecord.new))
|
35
|
+
class Rails
|
36
|
+
PLURALIZATIONS = { :zero => 0, :one => 1, :few => 2, :many => 'n',
|
37
|
+
:other => 'other' }
|
38
|
+
|
39
|
+
# Create new loader for some +backend+ from Rails I18n. Backend must have
|
40
|
+
# +reload!+, +init_translations+ and +translations+ methods.
|
41
|
+
def initialize(backend = ::I18n::Backend::Simple.new)
|
42
|
+
@backend = backend
|
43
|
+
end
|
44
|
+
|
45
|
+
# Array of locales, which has translations in +I18n.load_path+.
|
46
|
+
def available
|
47
|
+
reload!
|
48
|
+
@translations.keys.map { |code| R18n::Locale.load(code) }
|
49
|
+
end
|
50
|
+
|
51
|
+
# Return Hash with translations for +locale+.
|
52
|
+
def load(locale)
|
53
|
+
reload!
|
54
|
+
@translations[locale.code.downcase]
|
55
|
+
end
|
56
|
+
|
57
|
+
# Reload backend if <tt>I18n.load_path</tt> is changed.
|
58
|
+
def reload!
|
59
|
+
return if @last_path == ::I18n.load_path
|
60
|
+
@last_path = ::I18n.load_path.clone
|
61
|
+
@backend.reload!
|
62
|
+
@backend.send(:init_translations)
|
63
|
+
@translations = transform @backend.send(:translations)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Return hash for object and <tt>I18n.load_path</tt>.
|
67
|
+
def hash
|
68
|
+
super + ::I18n.load_path.hash
|
69
|
+
end
|
70
|
+
|
71
|
+
# Is another +loader+ is also load Rails translations.
|
72
|
+
def ==(loader)
|
73
|
+
self.class == loader.class
|
74
|
+
end
|
75
|
+
|
76
|
+
protected
|
77
|
+
|
78
|
+
# Change pluralization and keys to R18n format.
|
79
|
+
def transform(hash)
|
80
|
+
if hash.empty?
|
81
|
+
hash
|
82
|
+
elsif hash.keys.inject(true) { |a, i| a and PLURALIZATIONS.include? i }
|
83
|
+
R18n::Typed.new('pl', Hash[hash.map { |key, value|
|
84
|
+
[PLURALIZATIONS[key],
|
85
|
+
(value.is_a?(Hash) ? transform(value) : value)]
|
86
|
+
}])
|
87
|
+
else
|
88
|
+
Hash[hash.map { |key, value|
|
89
|
+
[key.to_s, (value.is_a?(Hash) ? transform(value) : value)]
|
90
|
+
}]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
+
|
4
|
+
describe R18n::Backend do
|
5
|
+
before do
|
6
|
+
I18n.load_path = [GENERAL]
|
7
|
+
I18n.backend = R18n::Backend.new
|
8
|
+
R18n.set R18n::I18n.new('en', R18n::Loader::Rails.new)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return available locales" do
|
12
|
+
I18n.available_locales.should =~ [:en]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should localize objects" do
|
16
|
+
time = Time.at(0).utc
|
17
|
+
date = Date.parse('1970-01-01')
|
18
|
+
|
19
|
+
I18n.l(time).should == 'Thu, 01 Jan 1970 00:00:00 +0000'
|
20
|
+
I18n.l(date).should == '1970-01-01'
|
21
|
+
|
22
|
+
I18n.l(time, :format => :short).should == '01 Jan 00:00'
|
23
|
+
I18n.l(time, :format => :full).should == '1st of January, 1970 00:00'
|
24
|
+
|
25
|
+
I18n.l(-5000.5).should == '−5,000.5'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should translate by key and scope" do
|
29
|
+
I18n.t('in.another.level').should == 'Hierarchical'
|
30
|
+
I18n.t(:level, :scope => 'in.another').should == 'Hierarchical'
|
31
|
+
I18n.t(:'another.level', :scope => 'in').should == 'Hierarchical'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should use pluralization and variables" do
|
35
|
+
I18n.t(:users, :count => 0).should == '0 users'
|
36
|
+
I18n.t(:users, :count => 1).should == '1 user'
|
37
|
+
I18n.t(:users, :count => 5).should == '5 users'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should use another separator" do
|
41
|
+
I18n.t('in/another/level', :separator => '/').should == 'Hierarchical'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should translate array" do
|
45
|
+
I18n.t(['in.another.level', 'in.default']).should == ['Hierarchical',
|
46
|
+
'Default']
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should use default value" do
|
50
|
+
I18n.t(:no, :default => 'Default').should == 'Default'
|
51
|
+
I18n.t(:no, :default => :default, :scope => :in).should == 'Default'
|
52
|
+
I18n.t(:no, :default => [:also_no, :'in.default']).should == 'Default'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should raise error on no translation" do
|
56
|
+
lambda {
|
57
|
+
I18n.backend.translate(:en, :no)
|
58
|
+
}.should raise_error(::I18n::MissingTranslationData)
|
59
|
+
I18n.t(:no).should == 'translation missing: en, no'
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should reload translations" do
|
63
|
+
I18n.t(:other).should == 'translation missing: en, other'
|
64
|
+
I18n.load_path << OTHER
|
65
|
+
I18n.reload!
|
66
|
+
I18n.t(:other).should == 'Other'
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
en:
|
2
|
+
date:
|
3
|
+
formats:
|
4
|
+
default: "%Y-%m-%d"
|
5
|
+
short: "%b %d"
|
6
|
+
long: "%B %d, %Y"
|
7
|
+
|
8
|
+
time:
|
9
|
+
formats:
|
10
|
+
default: "%a, %d %b %Y %H:%M:%S %z"
|
11
|
+
short: "%d %b %H:%M"
|
12
|
+
long: "%B %d, %Y %H:%M"
|
13
|
+
|
14
|
+
in:
|
15
|
+
another:
|
16
|
+
level: Hierarchical
|
17
|
+
default: Default
|
18
|
+
|
19
|
+
users:
|
20
|
+
one: 1 user
|
21
|
+
many: "{{count}} users"
|
@@ -0,0 +1 @@
|
|
1
|
+
{ :ru => { :two => "Два" } }
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
+
|
4
|
+
describe 'Rails filters' do
|
5
|
+
before :all do
|
6
|
+
@en = R18n::Locale.load('en')
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should use named variables" do
|
10
|
+
i18n = R18n::Translation.new(@en, '', @en, {'echo' => 'Value is {{value}}'})
|
11
|
+
|
12
|
+
i18n.echo(:value => 'R18n').should == 'Value is R18n'
|
13
|
+
i18n.echo(:value => -5.5).should == 'Value is −5.5'
|
14
|
+
i18n.echo(:value => 5000).should == 'Value is 5,000'
|
15
|
+
i18n.echo.should == 'Value is {{value}}'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should pluralize by variable {{count}}" do
|
19
|
+
i18n = R18n::Translation.new(@en, '', @en, {'users' => R18n::Typed.new('pl',
|
20
|
+
{ 0 => 'no users', 1 => '1 user', 'n' => '{{count}} users' }
|
21
|
+
) })
|
22
|
+
|
23
|
+
i18n.users(:count => 0).should == 'no users'
|
24
|
+
i18n.users(:count => 1).should == '1 user'
|
25
|
+
i18n.users(:count => 5).should == '5 users'
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/spec/loader_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
+
|
4
|
+
describe R18n::Loader::Rails do
|
5
|
+
before :all do
|
6
|
+
I18n.load_path = [SIMPLE]
|
7
|
+
@loader = R18n::Loader::Rails.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return available locales" do
|
11
|
+
@loader.available.should =~ [EN, RU]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should load translation" do
|
15
|
+
@loader.load(RU).should == { 'one' => 'Один', 'two' => 'Два' }
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should change pluralization" do
|
19
|
+
@loader.load(EN).should == {'users' => R18n::Typed.new('pl', {
|
20
|
+
0 => 'Zero', 1 => 'One', 2 => 'Few', 'n' => 'Many', 'other' => 'Other'
|
21
|
+
})}
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should reload translations on load_path changes" do
|
25
|
+
I18n.load_path << OTHER
|
26
|
+
@loader.load(RU).should == { 'one' => 'Один', 'two' => 'Два',
|
27
|
+
'three' => 'Три' }
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should change hash on load_path changes" do
|
31
|
+
before = @loader.hash
|
32
|
+
I18n.load_path << OTHER
|
33
|
+
@loader.hash.should_not == before
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pp'
|
3
|
+
gem 'i18n'
|
4
|
+
|
5
|
+
require File.join(File.dirname(__FILE__), '../lib/r18n-rails-api')
|
6
|
+
|
7
|
+
EN = R18n::Locale.load(:en)
|
8
|
+
RU = R18n::Locale.load(:ru)
|
9
|
+
|
10
|
+
GENERAL = Dir.glob(File.join(File.dirname(__FILE__), 'data/general/*'))
|
11
|
+
SIMPLE = Dir.glob(File.join(File.dirname(__FILE__), 'data/simple/*'))
|
12
|
+
OTHER = Dir.glob(File.join(File.dirname(__FILE__), 'data/other/*'))
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: r18n-rails-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.4"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrey "A.I." Sitnik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-03 00:00:00 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: r18n-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0.4"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: i18n
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: " R18n backend for Rails I18n and R18n filters and loader to support Rails\n translation format.\n R18n has nice Ruby-style syntax, filters, flexible locales, custom loaders,\n translation support for any classes, time and number localization, several\n user language support, agnostic core package with out-of-box support for\n Rails, Sinatra, Merb and desktop applications.\n"
|
36
|
+
email: andrey@sitnik.ru
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.rdoc
|
43
|
+
- LICENSE
|
44
|
+
files:
|
45
|
+
- lib/r18n-rails-api.rb
|
46
|
+
- lib/r18n-rails-api/filters.rb
|
47
|
+
- lib/r18n-rails-api/loader.rb
|
48
|
+
- lib/r18n-rails-api/backend.rb
|
49
|
+
- LICENSE
|
50
|
+
- README.rdoc
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://r18n.rubyforge.org/
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project: r18n-rails-api
|
75
|
+
rubygems_version: 1.3.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Rails I18n compatibility for R18n
|
79
|
+
test_files:
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/backend_spec.rb
|
82
|
+
- spec/filters_spec.rb
|
83
|
+
- spec/loader_spec.rb
|
84
|
+
- spec/data/other/ru.yml
|
85
|
+
- spec/data/other/en.yml
|
86
|
+
- spec/data/general/en.yml
|
87
|
+
- spec/data/simple/russian.yml
|
88
|
+
- spec/data/simple/ru.rb
|
89
|
+
- spec/data/simple/en.yml
|