i18n-js 2.1.2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +23 -31
- data/README.md +318 -0
- data/Rakefile +6 -6
- data/app/assets/javascripts/i18n/translations.js.erb +3 -0
- data/i18n-js.gemspec +5 -6
- data/lib/i18n-js.rb +1 -177
- data/lib/i18n.js +698 -0
- data/lib/i18n/js.rb +157 -0
- data/lib/i18n/js/engine.rb +22 -0
- data/lib/{i18n-js → i18n/js}/middleware.rb +7 -7
- data/lib/i18n/js/version.rb +10 -0
- data/spec/{resources → fixtures}/custom_path.yml +1 -1
- data/spec/{resources → fixtures}/default.yml +1 -1
- data/spec/fixtures/js_file_per_locale.yml +3 -0
- data/spec/{resources → fixtures}/locales.yml +1 -1
- data/spec/fixtures/multiple_conditions.yml +5 -0
- data/spec/{resources → fixtures}/multiple_files.yml +3 -3
- data/spec/{resources → fixtures}/no_config.yml +0 -0
- data/spec/{resources → fixtures}/no_scope.yml +1 -1
- data/spec/{resources → fixtures}/simple_scope.yml +1 -1
- data/spec/i18n_js_spec.rb +122 -0
- data/spec/js/currency.spec.js +60 -0
- data/spec/js/current_locale.spec.js +19 -0
- data/spec/js/dates.spec.js +218 -0
- data/spec/js/defaults.spec.js +23 -0
- data/spec/js/interpolation.spec.js +28 -0
- data/spec/js/jasmine/MIT.LICENSE +20 -0
- data/spec/js/jasmine/jasmine-html.js +190 -0
- data/spec/js/jasmine/jasmine.css +166 -0
- data/spec/js/jasmine/jasmine.js +2476 -0
- data/spec/js/jasmine/jasmine_favicon.png +0 -0
- data/spec/js/localization.spec.js +41 -0
- data/spec/js/numbers.spec.js +124 -0
- data/spec/js/placeholder.spec.js +24 -0
- data/spec/js/pluralization.spec.js +105 -0
- data/spec/js/prepare_options.spec.js +41 -0
- data/spec/js/specs.html +46 -0
- data/spec/js/translate.spec.js +115 -0
- data/spec/js/translations.js +115 -0
- data/spec/spec_helper.rb +36 -14
- metadata +115 -69
- data/.gitignore +0 -5
- data/.rspec +0 -1
- data/README.rdoc +0 -305
- data/config/i18n-js.yml +0 -22
- data/lib/i18n-js/engine.rb +0 -62
- data/lib/i18n-js/railtie.rb +0 -13
- data/lib/i18n-js/rake.rb +0 -16
- data/lib/i18n-js/version.rb +0 -10
- data/spec/i18n_spec.js +0 -768
- data/spec/i18n_spec.rb +0 -205
- data/spec/resources/js_file_per_locale.yml +0 -3
- data/spec/resources/multiple_conditions.yml +0 -6
- data/vendor/assets/javascripts/i18n.js +0 -450
- data/vendor/assets/javascripts/i18n/translations.js.erb +0 -7
data/Gemfile.lock
CHANGED
@@ -1,51 +1,43 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
i18n-js (
|
4
|
+
i18n-js (3.0.0.rc1)
|
5
5
|
i18n
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (3.
|
10
|
+
activesupport (3.2.3)
|
11
|
+
i18n (~> 0.6)
|
11
12
|
multi_json (~> 1.0)
|
12
|
-
|
13
|
+
awesome_print (1.0.2)
|
14
|
+
coderay (1.0.6)
|
13
15
|
diff-lcs (1.1.3)
|
14
|
-
fakeweb (1.3.0)
|
15
16
|
i18n (0.6.0)
|
16
|
-
method_source (0.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
method_source (~> 0.6.7)
|
23
|
-
ruby_parser (>= 2.3.1)
|
24
|
-
slop (~> 2.1.0)
|
17
|
+
method_source (0.7.1)
|
18
|
+
multi_json (1.3.2)
|
19
|
+
pry (0.9.9.3)
|
20
|
+
coderay (~> 1.0.5)
|
21
|
+
method_source (~> 0.7.1)
|
22
|
+
slop (>= 2.4.4, < 3)
|
25
23
|
rake (0.9.2.2)
|
26
|
-
rspec (2.
|
27
|
-
rspec-core (~> 2.
|
28
|
-
rspec-expectations (~> 2.
|
29
|
-
rspec-mocks (~> 2.
|
30
|
-
rspec-core (2.
|
31
|
-
rspec-expectations (2.
|
32
|
-
diff-lcs (~> 1.1.
|
33
|
-
rspec-mocks (2.
|
34
|
-
|
35
|
-
sexp_processor (~> 3.0)
|
36
|
-
sexp_processor (3.0.8)
|
37
|
-
slop (2.1.0)
|
38
|
-
spec-js (0.1.0.beta.3)
|
39
|
-
notifier
|
24
|
+
rspec (2.9.0)
|
25
|
+
rspec-core (~> 2.9.0)
|
26
|
+
rspec-expectations (~> 2.9.0)
|
27
|
+
rspec-mocks (~> 2.9.0)
|
28
|
+
rspec-core (2.9.0)
|
29
|
+
rspec-expectations (2.9.1)
|
30
|
+
diff-lcs (~> 1.1.3)
|
31
|
+
rspec-mocks (2.9.0)
|
32
|
+
slop (2.4.4)
|
40
33
|
|
41
34
|
PLATFORMS
|
42
35
|
ruby
|
43
36
|
|
44
37
|
DEPENDENCIES
|
45
|
-
activesupport
|
46
|
-
|
38
|
+
activesupport
|
39
|
+
awesome_print
|
47
40
|
i18n-js!
|
48
41
|
pry
|
49
42
|
rake
|
50
|
-
rspec
|
51
|
-
spec-js (~> 0.1.0.beta.0)
|
43
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,318 @@
|
|
1
|
+
# I18n.js
|
2
|
+
|
3
|
+
It's a small library to provide the Rails I18n translations on the Javascript.
|
4
|
+
|
5
|
+
Features:
|
6
|
+
|
7
|
+
- Pluralization
|
8
|
+
- Date/Time localization
|
9
|
+
- Number localization
|
10
|
+
- Locale fallback
|
11
|
+
- Asset pipeline support
|
12
|
+
- Lots more! :)
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
### Installation
|
17
|
+
|
18
|
+
#### Rails app
|
19
|
+
|
20
|
+
Add the gem to your Gemfile.
|
21
|
+
|
22
|
+
source :rubygems
|
23
|
+
gem "rails", "3.2.3"
|
24
|
+
gem "i18n-js"
|
25
|
+
|
26
|
+
If you're using the [asset pipeline](http://guides.rubyonrails.org/asset_pipeline.html),
|
27
|
+
then you must add the following line to your `app/assets/javascripts/application.js`.
|
28
|
+
|
29
|
+
//= require i18n/translations
|
30
|
+
|
31
|
+
If you're not using the asset pipeline, download the JavaScript file at
|
32
|
+
<https://github.com/fnando/i18n-js/tree/master/lib/i18n.js> and load it on your page.
|
33
|
+
Also load the `translations.js` file.
|
34
|
+
|
35
|
+
<%= javascript_include_tag "i18n", "translations" %>
|
36
|
+
|
37
|
+
This `translations.js` file can be automatically generated by the `I18n::JS::Middleware`.
|
38
|
+
Just add it to your `config/application.rb` file.
|
39
|
+
|
40
|
+
config.middleware.use I18n::JS::Middleware
|
41
|
+
|
42
|
+
If you can't generate this file in production (Heroku anyone?), you can "precompile"
|
43
|
+
it by running the following command. Move the middleware line to your
|
44
|
+
`config/environments/development.rb` file and run the following command before
|
45
|
+
deploying.
|
46
|
+
|
47
|
+
$ rails runner I18n::JS.export
|
48
|
+
|
49
|
+
This will export all translation files, including the custom scopes you may have
|
50
|
+
defined on `config/i18n-js.yml`.
|
51
|
+
|
52
|
+
#### Vanilla JavaScript
|
53
|
+
|
54
|
+
Just add the `i18n.js` file to your page. You'll have to build the translations object
|
55
|
+
by hand or using your favorite programming language. More info below.
|
56
|
+
|
57
|
+
### Setting up
|
58
|
+
|
59
|
+
You **don't** need to set up a thing. The default settings will work just okay. But if you want to split translations into several files or specify specific contexts, you can follow the rest of this setting up section.
|
60
|
+
|
61
|
+
Set your locale is easy as
|
62
|
+
|
63
|
+
I18n.defaultLocale = "pt-BR";
|
64
|
+
I18n.locale = "pt-BR";
|
65
|
+
I18n.currentLocale();
|
66
|
+
// pt-BR
|
67
|
+
|
68
|
+
In practice, you'll have something like the following in your `application.html.erb`:
|
69
|
+
|
70
|
+
<script type="text/javascript">
|
71
|
+
I18n.defaultLocale = "<%= I18n.default_locale %>";
|
72
|
+
I18n.locale = "<%= I18n.locale %>";
|
73
|
+
</script>
|
74
|
+
|
75
|
+
You can use translate your messages:
|
76
|
+
|
77
|
+
I18n.t("some.scoped.translation");
|
78
|
+
// or translate with explicite setting of locale
|
79
|
+
I18n.t("some.scoped.translation", {locale: "fr"});
|
80
|
+
|
81
|
+
You can also interpolate values:
|
82
|
+
|
83
|
+
I18n.t("hello", {name: "John Doe"});
|
84
|
+
|
85
|
+
You can set default values for missing scopes:
|
86
|
+
|
87
|
+
// simple translation
|
88
|
+
I18n.t("some.missing.scope", {defaultValue: "A default message"});
|
89
|
+
|
90
|
+
// with interpolation
|
91
|
+
I18n.t("noun", {defaultValue: "I'm a {{noun}}", noun: "Mac"});
|
92
|
+
|
93
|
+
Translation fallback can be enabled by enabling the `I18n.fallbacks` option:
|
94
|
+
|
95
|
+
<script type="text/javascript">
|
96
|
+
I18n.fallbacks = true;
|
97
|
+
</script>
|
98
|
+
|
99
|
+
By default missing translations will first be looked for in less
|
100
|
+
specific versions of the requested locale and if that fails by taking
|
101
|
+
them from your `I18n.defaultLocale`.
|
102
|
+
|
103
|
+
// if I18n.defaultLocale = "en" and translation doesn't exist
|
104
|
+
// for I18n.locale = "de-DE" this key will be taken from "de" locale scope
|
105
|
+
// or, if that also doesn't exist, from "en" locale scope
|
106
|
+
I18n.t("some.missing.scope");
|
107
|
+
|
108
|
+
Custom fallback rules can also be specified for a particular language. There
|
109
|
+
are three different ways of doing it so:
|
110
|
+
|
111
|
+
I18n.locales.no = ["nb", "en"];
|
112
|
+
I18n.locales.no = "nb";
|
113
|
+
I18n.locales.no = function(locale){ return ["nb"]; };
|
114
|
+
|
115
|
+
Pluralization is possible as well and by default provides english rules:
|
116
|
+
|
117
|
+
I18n.t("inbox.counting", {count: 10}); // You have 10 messages
|
118
|
+
|
119
|
+
The sample above expects the following translation:
|
120
|
+
|
121
|
+
en:
|
122
|
+
inbox:
|
123
|
+
counting:
|
124
|
+
one: You have 1 new message
|
125
|
+
other: You have {{count}} new messages
|
126
|
+
zero: You have no messages
|
127
|
+
|
128
|
+
**NOTE:** Rais I18n recognizes the `zero` option.
|
129
|
+
|
130
|
+
If you need special rules just define them for your language, for example Russian, just add a new pluralizer:
|
131
|
+
|
132
|
+
I18n.pluralization["ru"] = function (count) {
|
133
|
+
return count % 10 == 1 && count % 100 != 11 ? "one" : [2, 3, 4].indexOf(count % 10) >= 0 && [12, 13, 14].indexOf(count % 100) < 0 ? "few" : count % 10 == 0 || [5, 6, 7, 8, 9].indexOf(count % 10) >= 0 || [11, 12, 13, 14].indexOf(count % 100) >= 0 ? "many" : "other";
|
134
|
+
};
|
135
|
+
|
136
|
+
You can find all rules on <http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html>.
|
137
|
+
|
138
|
+
If you're using the same scope over and over again, you may use the `scope` option.
|
139
|
+
|
140
|
+
var options = {scope: "activerecord.attributes.user"};
|
141
|
+
|
142
|
+
I18n.t("name", options);
|
143
|
+
I18n.t("email", options);
|
144
|
+
I18n.t("username", options);
|
145
|
+
|
146
|
+
You also provide an array as scope.
|
147
|
+
|
148
|
+
// use the greetings.hello scope
|
149
|
+
I18n.t(["greetings", "hello"]);
|
150
|
+
|
151
|
+
#### Number formatting
|
152
|
+
|
153
|
+
Similar to Rails helpers, you have localized number and currency formatting.
|
154
|
+
|
155
|
+
I18n.l("currency", 1990.99);
|
156
|
+
// $1,990.99
|
157
|
+
|
158
|
+
I18n.l("number", 1990.99);
|
159
|
+
// 1,990.99
|
160
|
+
|
161
|
+
I18n.l("percentage", 123.45);
|
162
|
+
// 123.450%
|
163
|
+
|
164
|
+
To have more control over number formatting, you can use the
|
165
|
+
`I18n.toNumber`, `I18n.toPercentage`, `I18n.toCurrency` and `I18n.toHumanSize`
|
166
|
+
functions.
|
167
|
+
|
168
|
+
I18n.toNumber(1000); // 1,000.000
|
169
|
+
I18n.toCurrency(1000); // $1,000.00
|
170
|
+
I18n.toPercentage(100); // 100.000%
|
171
|
+
|
172
|
+
The `toNumber` and `toPercentage` functions accept the following options:
|
173
|
+
|
174
|
+
- `precision`: defaults to `3`
|
175
|
+
- `separator`: defaults to `.`
|
176
|
+
- `delimiter`: defaults to `,`
|
177
|
+
- `strip_insignificant_zeros`: defaults to `false`
|
178
|
+
|
179
|
+
See some number formatting examples:
|
180
|
+
|
181
|
+
I18n.toNumber(1000, {precision: 0}); // 1,000
|
182
|
+
I18n.toNumber(1000, {delimiter: ".", separator: ","}); // 1.000,000
|
183
|
+
I18n.toNumber(1000, {delimiter: ".", precision: 0}); // 1.000
|
184
|
+
|
185
|
+
The `toCurrency` function accepts the following options:
|
186
|
+
|
187
|
+
- `precision`: sets the level of precision
|
188
|
+
- `separator`: sets the separator between the units
|
189
|
+
- `delimiter`: sets the thousands delimiter
|
190
|
+
- `format`: sets the format of the output string
|
191
|
+
- `unit`: sets the denomination of the currency
|
192
|
+
- `strip_insignificant_zeros`: defaults to `false`
|
193
|
+
|
194
|
+
You can provide only the options you want to override:
|
195
|
+
|
196
|
+
I18n.toCurrency(1000, {precision: 0}); // $1,000
|
197
|
+
|
198
|
+
The `toHumanSize` function accepts the following options:
|
199
|
+
|
200
|
+
- `precision`: defaults to `1`
|
201
|
+
- `separator`: defaults to `.`
|
202
|
+
- `delimiter`: defaults to `""`
|
203
|
+
- `strip_insignificant_zeros`: defaults to `false`
|
204
|
+
- `format`: defaults to `%n%u`
|
205
|
+
|
206
|
+
<!---->
|
207
|
+
|
208
|
+
I18n.toHumanSize(1234); // 1KB
|
209
|
+
I18n.toHumanSize(1234 * 1024); // 1MB
|
210
|
+
|
211
|
+
#### Date formatting
|
212
|
+
|
213
|
+
// accepted formats
|
214
|
+
I18n.l("date.formats.short", "2009-09-18"); // yyyy-mm-dd
|
215
|
+
I18n.l("time.formats.short", "2009-09-18 23:12:43"); // yyyy-mm-dd hh:mm:ss
|
216
|
+
I18n.l("time.formats.short", "2009-11-09T18:10:34"); // JSON format with local Timezone (part of ISO-8601)
|
217
|
+
I18n.l("time.formats.short", "2009-11-09T18:10:34Z"); // JSON format in UTC (part of ISO-8601)
|
218
|
+
I18n.l("date.formats.short", 1251862029000); // Epoch time
|
219
|
+
I18n.l("date.formats.short", "09/18/2009"); // mm/dd/yyyy
|
220
|
+
I18n.l("date.formats.short", (new Date())); // Date object
|
221
|
+
|
222
|
+
If you prefer, you can use the `I18n.strftime` function to format dates.
|
223
|
+
|
224
|
+
var date = new Date();
|
225
|
+
I18n.strftime(date, "%d/%m/%Y");
|
226
|
+
|
227
|
+
The accepted formats are:
|
228
|
+
|
229
|
+
%a - The abbreviated weekday name (Sun)
|
230
|
+
%A - The full weekday name (Sunday)
|
231
|
+
%b - The abbreviated month name (Jan)
|
232
|
+
%B - The full month name (January)
|
233
|
+
%c - The preferred local date and time representation
|
234
|
+
%d - Day of the month (01..31)
|
235
|
+
%-d - Day of the month (1..31)
|
236
|
+
%H - Hour of the day, 24-hour clock (00..23)
|
237
|
+
%-H - Hour of the day, 24-hour clock (0..23)
|
238
|
+
%I - Hour of the day, 12-hour clock (01..12)
|
239
|
+
%-I - Hour of the day, 12-hour clock (1..12)
|
240
|
+
%m - Month of the year (01..12)
|
241
|
+
%-m - Month of the year (1..12)
|
242
|
+
%M - Minute of the hour (00..59)
|
243
|
+
%-M - Minute of the hour (0..59)
|
244
|
+
%p - Meridian indicator (AM or PM)
|
245
|
+
%S - Second of the minute (00..60)
|
246
|
+
%-S - Second of the minute (0..60)
|
247
|
+
%w - Day of the week (Sunday is 0, 0..6)
|
248
|
+
%y - Year without a century (00..99)
|
249
|
+
%-y - Year without a century (0..99)
|
250
|
+
%Y - Year with century
|
251
|
+
%z - Timezone offset (+0545)
|
252
|
+
|
253
|
+
Check out `spec/*.spec.js` files for more examples!
|
254
|
+
|
255
|
+
## Using I18n.js with other languages (Python, PHP, ...)
|
256
|
+
|
257
|
+
The JavaScript library is language agnostic; so you can use it with PHP, Python, [you favorite language here].
|
258
|
+
The only requirement is that you need to set the `translations` attribute like following:
|
259
|
+
|
260
|
+
I18n.translations = {};
|
261
|
+
|
262
|
+
I18n.translations["en"] = {
|
263
|
+
message: "Some special message for you"
|
264
|
+
}
|
265
|
+
|
266
|
+
I18n.translations["pt-BR"] = {
|
267
|
+
message: "Uma mensagem especial para você"
|
268
|
+
}
|
269
|
+
|
270
|
+
## Maintainer
|
271
|
+
|
272
|
+
- Nando Vieira - <http://nandovieira.com.br>
|
273
|
+
|
274
|
+
## Contributing
|
275
|
+
|
276
|
+
Once you've made your great commits:
|
277
|
+
|
278
|
+
1. [Fork](http://help.github.com/forking/) I18n.js
|
279
|
+
2. Create a topic branch - `git checkout -b my_branch`
|
280
|
+
3. Push to your branch - `git push origin my_branch`
|
281
|
+
4. [Create an Issue](http://github.com/fnando/i18n-js/issues) with a link to your branch
|
282
|
+
5. That's it!
|
283
|
+
|
284
|
+
Please respect the indentation rules and code style.
|
285
|
+
And use 2 spaces, not tabs. And don't touch the versioning thing.
|
286
|
+
|
287
|
+
## Running tests
|
288
|
+
|
289
|
+
You can run I18n tests using Node.js or your browser.
|
290
|
+
|
291
|
+
To use Node.js, execute the following command from the lib's root directory:
|
292
|
+
|
293
|
+
$ ./node_modules/.bin/jasmine-node spec/js
|
294
|
+
|
295
|
+
To run using your browser, just open the `spec/js/specs.html` file.
|
296
|
+
|
297
|
+
## License
|
298
|
+
|
299
|
+
(The MIT License)
|
300
|
+
|
301
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
302
|
+
a copy of this software and associated documentation files (the
|
303
|
+
'Software'), to deal in the Software without restriction, including
|
304
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
305
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
306
|
+
permit persons to whom the Software is furnished to do so, subject to
|
307
|
+
the following conditions:
|
308
|
+
|
309
|
+
The above copyright notice and this permission notice shall be
|
310
|
+
included in all copies or substantial portions of the Software.
|
311
|
+
|
312
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
313
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
314
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
315
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
316
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
317
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
318
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require "bundler"
|
2
2
|
Bundler::GemHelper.install_tasks
|
3
3
|
|
4
|
-
require "spec_js/rake_task"
|
5
|
-
SpecJs::RakeTask.new do |t|
|
6
|
-
t.env_js = false
|
7
|
-
end
|
8
|
-
|
9
4
|
require "rspec/core/rake_task"
|
10
5
|
RSpec::Core::RakeTask.new(:"spec:ruby")
|
11
6
|
|
7
|
+
desc "Run JavaScript specs"
|
8
|
+
task "spec:js" do
|
9
|
+
system "jasmine-node", "spec/js"
|
10
|
+
end
|
11
|
+
|
12
12
|
desc "Run all specs"
|
13
|
-
task :spec => [
|
13
|
+
task :spec => ["spec:ruby", "spec:js"]
|
data/i18n-js.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "i18n
|
3
|
+
require "i18n/js/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "i18n-js"
|
7
|
-
s.version =
|
7
|
+
s.version = I18n::JS::Version::STRING
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Nando Vieira"]
|
10
10
|
s.email = ["fnando.vieira@gmail.com"]
|
@@ -18,10 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
|
20
20
|
s.add_dependency "i18n"
|
21
|
-
s.add_development_dependency "
|
22
|
-
s.add_development_dependency "
|
23
|
-
s.add_development_dependency "rspec", "~> 2.6"
|
24
|
-
s.add_development_dependency "spec-js", "~> 0.1.0.beta.0"
|
21
|
+
s.add_development_dependency "activesupport"
|
22
|
+
s.add_development_dependency "rspec"
|
25
23
|
s.add_development_dependency "rake"
|
26
24
|
s.add_development_dependency "pry"
|
25
|
+
s.add_development_dependency "awesome_print"
|
27
26
|
end
|
data/lib/i18n-js.rb
CHANGED
@@ -1,177 +1 @@
|
|
1
|
-
require "
|
2
|
-
|
3
|
-
module SimplesIdeias
|
4
|
-
module I18n
|
5
|
-
extend self
|
6
|
-
|
7
|
-
require "i18n-js/railtie" if Rails.version >= "3.0"
|
8
|
-
require "i18n-js/engine" if Rails.version >= "3.1"
|
9
|
-
require "i18n-js/middleware"
|
10
|
-
|
11
|
-
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
|
12
|
-
MERGER = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : v2 }
|
13
|
-
|
14
|
-
# Under rails 3.1.1 and higher, perform a check to ensure that the
|
15
|
-
# full environment will be available during asset compilation.
|
16
|
-
# This is required to ensure I18n is loaded.
|
17
|
-
def assert_usable_configuration!
|
18
|
-
@usable_configuration ||= Rails.version >= "3.1.1" &&
|
19
|
-
Rails.configuration.assets.initialize_on_precompile ||
|
20
|
-
raise("Cannot precompile i18n-js translations unless environment is initialized. Please set config.assets.initialize_on_precompile to true.")
|
21
|
-
end
|
22
|
-
|
23
|
-
def has_asset_pipeline?
|
24
|
-
Rails.configuration.respond_to?(:assets) && Rails.configuration.assets.enabled
|
25
|
-
end
|
26
|
-
|
27
|
-
def config_file
|
28
|
-
Rails.root.join("config/i18n-js.yml")
|
29
|
-
end
|
30
|
-
|
31
|
-
def export_dir
|
32
|
-
if has_asset_pipeline?
|
33
|
-
"app/assets/javascripts/i18n"
|
34
|
-
else
|
35
|
-
"public/javascripts"
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def javascript_file
|
40
|
-
Rails.root.join(export_dir, "i18n.js")
|
41
|
-
end
|
42
|
-
|
43
|
-
# Export translations to JavaScript, considering settings
|
44
|
-
# from configuration file
|
45
|
-
def export!
|
46
|
-
translation_segments.each do |filename, translations|
|
47
|
-
save(translations, filename)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def segments_per_locale(pattern,scope)
|
52
|
-
::I18n.available_locales.each_with_object({}) do |locale,segments|
|
53
|
-
result = scoped_translations("#{locale}.#{scope}")
|
54
|
-
unless result.empty?
|
55
|
-
segment_name = ::I18n.interpolate(pattern,{:locale => locale})
|
56
|
-
segments[segment_name] = result
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def segment_for_scope(scope)
|
62
|
-
if scope == "*"
|
63
|
-
translations
|
64
|
-
else
|
65
|
-
scoped_translations(scope)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def configured_segments
|
70
|
-
config[:translations].each_with_object({}) do |options,segments|
|
71
|
-
options.reverse_merge!(:only => "*")
|
72
|
-
if options[:file] =~ ::I18n::INTERPOLATION_PATTERN
|
73
|
-
segments.merge!(segments_per_locale(options[:file],options[:only]))
|
74
|
-
else
|
75
|
-
result = segment_for_scope(options[:only])
|
76
|
-
segments[options[:file]] = result unless result.empty?
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def translation_segments
|
82
|
-
if config? && config[:translations]
|
83
|
-
configured_segments
|
84
|
-
else
|
85
|
-
{"#{export_dir}/translations.js" => translations}
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
# Load configuration file for partial exporting and
|
90
|
-
# custom output directory
|
91
|
-
def config
|
92
|
-
if config?
|
93
|
-
(YAML.load_file(config_file) || {}).with_indifferent_access
|
94
|
-
else
|
95
|
-
{}
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# Check if configuration file exist
|
100
|
-
def config?
|
101
|
-
File.file? config_file
|
102
|
-
end
|
103
|
-
|
104
|
-
# Copy configuration and JavaScript library files to
|
105
|
-
# <tt>config/i18n-js.yml</tt> and <tt>public/javascripts/i18n.js</tt>.
|
106
|
-
def setup!
|
107
|
-
FileUtils.cp(File.dirname(__FILE__) + "/../vendor/assets/javascripts/i18n.js", javascript_file) unless Rails.version >= "3.1"
|
108
|
-
FileUtils.cp(File.dirname(__FILE__) + "/../config/i18n-js.yml", config_file) unless config?
|
109
|
-
end
|
110
|
-
|
111
|
-
# Retrieve an updated JavaScript library from Github.
|
112
|
-
def update!
|
113
|
-
require "open-uri"
|
114
|
-
contents = open("https://raw.github.com/fnando/i18n-js/master/vendor/assets/javascripts/i18n.js").read
|
115
|
-
File.open(javascript_file, "w+") {|f| f << contents}
|
116
|
-
end
|
117
|
-
|
118
|
-
# Convert translations to JSON string and save file.
|
119
|
-
def save(translations, file)
|
120
|
-
file = Rails.root.join(file)
|
121
|
-
FileUtils.mkdir_p File.dirname(file)
|
122
|
-
|
123
|
-
File.open(file, "w+") do |f|
|
124
|
-
f << %(var I18n = I18n || {};\n)
|
125
|
-
f << %(I18n.translations = );
|
126
|
-
f << translations.to_json
|
127
|
-
f << %(;)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
def scoped_translations(scopes) # :nodoc:
|
132
|
-
result = {}
|
133
|
-
|
134
|
-
[scopes].flatten.each do |scope|
|
135
|
-
deep_merge! result, filter(translations, scope)
|
136
|
-
end
|
137
|
-
|
138
|
-
result
|
139
|
-
end
|
140
|
-
|
141
|
-
# Filter translations according to the specified scope.
|
142
|
-
def filter(translations, scopes)
|
143
|
-
scopes = scopes.split(".") if scopes.is_a?(String)
|
144
|
-
scopes = scopes.clone
|
145
|
-
scope = scopes.shift
|
146
|
-
|
147
|
-
if scope == "*"
|
148
|
-
results = {}
|
149
|
-
translations.each do |scope, translations|
|
150
|
-
tmp = scopes.empty? ? translations : filter(translations, scopes)
|
151
|
-
results[scope.to_sym] = tmp unless tmp.nil?
|
152
|
-
end
|
153
|
-
return results
|
154
|
-
elsif translations.has_key?(scope.to_sym)
|
155
|
-
return {scope.to_sym => scopes.empty? ? translations[scope.to_sym] : filter(translations[scope.to_sym], scopes)}
|
156
|
-
end
|
157
|
-
nil
|
158
|
-
end
|
159
|
-
|
160
|
-
# Initialize and return translations
|
161
|
-
def translations
|
162
|
-
::I18n.backend.instance_eval do
|
163
|
-
init_translations unless initialized?
|
164
|
-
translations
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
def deep_merge(target, hash) # :nodoc:
|
169
|
-
target.merge(hash, &MERGER)
|
170
|
-
end
|
171
|
-
|
172
|
-
def deep_merge!(target, hash) # :nodoc:
|
173
|
-
target.merge!(hash, &MERGER)
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
1
|
+
require "i18n/js"
|