mlanett-i18n-js 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +51 -0
- data/README.rdoc +320 -0
- data/Rakefile +13 -0
- data/config/i18n-js.yml +22 -0
- data/i18n-js.gemspec +27 -0
- data/lib/i18n-js.rb +192 -0
- data/lib/i18n-js/engine.rb +63 -0
- data/lib/i18n-js/middleware.rb +59 -0
- data/lib/i18n-js/railtie.rb +13 -0
- data/lib/i18n-js/rake.rb +16 -0
- data/lib/i18n-js/version.rb +10 -0
- data/spec/i18n_spec.js +820 -0
- data/spec/i18n_spec.rb +205 -0
- data/spec/resources/custom_path.yml +4 -0
- data/spec/resources/default.yml +4 -0
- data/spec/resources/js_file_per_locale.yml +3 -0
- data/spec/resources/locales.yml +76 -0
- data/spec/resources/multiple_conditions.yml +6 -0
- data/spec/resources/multiple_files.yml +6 -0
- data/spec/resources/no_config.yml +2 -0
- data/spec/resources/no_scope.yml +3 -0
- data/spec/resources/simple_scope.yml +4 -0
- data/spec/spec_helper.rb +19 -0
- data/vendor/assets/javascripts/i18n.js +706 -0
- data/vendor/assets/javascripts/i18n/translations.js.erb +9 -0
- metadata +197 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format documentation
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
i18n-js (2.1.2)
|
5
|
+
i18n
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (3.1.1)
|
11
|
+
multi_json (~> 1.0)
|
12
|
+
coderay (0.9.8)
|
13
|
+
diff-lcs (1.1.3)
|
14
|
+
fakeweb (1.3.0)
|
15
|
+
i18n (0.6.0)
|
16
|
+
method_source (0.6.7)
|
17
|
+
ruby_parser (>= 2.3.1)
|
18
|
+
multi_json (1.0.3)
|
19
|
+
notifier (0.1.4)
|
20
|
+
pry (0.9.7.4)
|
21
|
+
coderay (~> 0.9.8)
|
22
|
+
method_source (~> 0.6.7)
|
23
|
+
ruby_parser (>= 2.3.1)
|
24
|
+
slop (~> 2.1.0)
|
25
|
+
rake (0.9.2.2)
|
26
|
+
rspec (2.7.0)
|
27
|
+
rspec-core (~> 2.7.0)
|
28
|
+
rspec-expectations (~> 2.7.0)
|
29
|
+
rspec-mocks (~> 2.7.0)
|
30
|
+
rspec-core (2.7.1)
|
31
|
+
rspec-expectations (2.7.0)
|
32
|
+
diff-lcs (~> 1.1.2)
|
33
|
+
rspec-mocks (2.7.0)
|
34
|
+
ruby_parser (2.3.1)
|
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
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
activesupport (>= 3.0.0)
|
46
|
+
fakeweb
|
47
|
+
i18n-js!
|
48
|
+
pry
|
49
|
+
rake
|
50
|
+
rspec (~> 2.6)
|
51
|
+
spec-js (~> 0.1.0.beta.0)
|
data/README.rdoc
ADDED
@@ -0,0 +1,320 @@
|
|
1
|
+
= I18n for JavaScript
|
2
|
+
|
3
|
+
It's a small library to provide the Rails I18n translations on the Javascript.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
=== Installation
|
8
|
+
|
9
|
+
gem install i18n-js
|
10
|
+
|
11
|
+
=== Setting up
|
12
|
+
|
13
|
+
You <b>don't</b> 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.
|
14
|
+
|
15
|
+
==== Rails <= 3.0
|
16
|
+
|
17
|
+
Run <tt>rake i18n:js:setup</tt> to copy <tt>i18n.js</tt> to your javascript directory and <tt>i18n-js.yml</tt> to your config folder (if not already present). Then you're ready to go!
|
18
|
+
|
19
|
+
==== Rails >= 3.1
|
20
|
+
|
21
|
+
Add the following lines to your application.js to make the javascripts and translations available to your app:
|
22
|
+
|
23
|
+
//= require i18n
|
24
|
+
//= require i18n/translations
|
25
|
+
|
26
|
+
If asset pipeline has been disabled for your Rails application, then you will need to run <tt>rake i18n:js:setup</tt> to copy <tt>i18n-js.yml</tt> to your config folder (if not already present).
|
27
|
+
|
28
|
+
==== Exporting translations
|
29
|
+
|
30
|
+
You can export the translations file by running <tt>rake i18n:js:export</tt>.
|
31
|
+
Translations will be automatically exported in development mode.
|
32
|
+
|
33
|
+
==== Configuration
|
34
|
+
|
35
|
+
Translation files can be customized. You can even get more files generated to different folders and with different translations to best suit your needs.
|
36
|
+
|
37
|
+
Examples:
|
38
|
+
|
39
|
+
translations:
|
40
|
+
- file: 'public/javascripts/path-to-your-messages-file.js'
|
41
|
+
only: '*.date.formats'
|
42
|
+
- file: 'public/javascripts/path-to-your-second-file.js'
|
43
|
+
only: ['*.activerecord', '*.admin.*.title']
|
44
|
+
|
45
|
+
If <tt>only</tt> is omitted all the translations will be saved. Also, make sure you add that initial <tt>*</tt>; it specifies that all languages will be exported. If you want to export only one language, you can do something like this:
|
46
|
+
|
47
|
+
translations:
|
48
|
+
- file: 'public/javascripts/en.js'
|
49
|
+
only: 'en.*'
|
50
|
+
- file: 'public/javascripts/pt-BR.js'
|
51
|
+
only: 'pt-BR.*'
|
52
|
+
|
53
|
+
Optionally, you can auto generate a translation file per available locale if you specify the <tt>%{locale}</tt> placeholder.
|
54
|
+
|
55
|
+
translations:
|
56
|
+
- file: "public/javascripts/i18n/%{locale}.js"
|
57
|
+
only: '*'
|
58
|
+
- file: "public/javascripts/frontend/i18n/%{locale}.js"
|
59
|
+
only: ['frontend', 'users']
|
60
|
+
|
61
|
+
To find more examples on how to use the configuration file please refer to the tests.
|
62
|
+
|
63
|
+
=== On the Javascript
|
64
|
+
|
65
|
+
Set your locale is easy as
|
66
|
+
|
67
|
+
I18n.defaultLocale = "pt-BR";
|
68
|
+
I18n.locale = "pt-BR";
|
69
|
+
I18n.currentLocale();
|
70
|
+
// pt-BR
|
71
|
+
|
72
|
+
In practice, you'll have something like the following in your <tt>application.html.erb</tt>:
|
73
|
+
|
74
|
+
<script type="text/javascript">
|
75
|
+
I18n.defaultLocale = "<%= I18n.default_locale %>";
|
76
|
+
I18n.locale = "<%= I18n.locale %>";
|
77
|
+
</script>
|
78
|
+
|
79
|
+
You can use translate your messages:
|
80
|
+
|
81
|
+
I18n.t("some.scoped.translation");
|
82
|
+
// or translate with explicite setting of locale
|
83
|
+
I18n.t("some.scoped.translation", {locale: "fr"});
|
84
|
+
|
85
|
+
You can also interpolate values:
|
86
|
+
|
87
|
+
I18n.t("hello", {name: "John Doe"});
|
88
|
+
|
89
|
+
The sample above will assume that you have the following translations in your
|
90
|
+
<tt>config/locales/*.yml</tt>:
|
91
|
+
|
92
|
+
en:
|
93
|
+
hello: "Hello {{name}}!"
|
94
|
+
|
95
|
+
You can set default values for missing scopes:
|
96
|
+
|
97
|
+
// simple translation
|
98
|
+
I18n.t("some.missing.scope", {defaultValue: "A default message"});
|
99
|
+
|
100
|
+
// with interpolation
|
101
|
+
I18n.t("noun", {defaultValue: "I'm a {{noun}}", noun: "Mac"});
|
102
|
+
|
103
|
+
Translation fallback can be enabled by adding to your <tt>application.html.erb</tt>:
|
104
|
+
|
105
|
+
<script type="text/javascript">
|
106
|
+
I18n.fallbacks = true;
|
107
|
+
</script>
|
108
|
+
|
109
|
+
By default missing translations will first be looked for in less
|
110
|
+
specific versions of the requested locale and if that fails by taking
|
111
|
+
them from your I18n.defaultLocale.
|
112
|
+
Example:
|
113
|
+
// if I18n.defaultLocale = "en" and translation doesn't exist for I18n.locale = "de-DE"
|
114
|
+
I18n.t("some.missing.scope");
|
115
|
+
// this key will be taken from "de" locale scope
|
116
|
+
// or, if that also doesn't exist, from "en" locale scope
|
117
|
+
|
118
|
+
Custom fallback rules can also be specified for a particular language,
|
119
|
+
for example:
|
120
|
+
|
121
|
+
I18n.fallbackRules.no = [ "nb", "en" ];
|
122
|
+
|
123
|
+
Pluralization is possible as well and by default provides english rules:
|
124
|
+
|
125
|
+
I18n.t("inbox.counting", {count: 10}); // You have 10 messages
|
126
|
+
|
127
|
+
The sample above expects the following translation:
|
128
|
+
|
129
|
+
en:
|
130
|
+
inbox:
|
131
|
+
counting:
|
132
|
+
one: You have 1 new message
|
133
|
+
other: You have {{count}} new messages
|
134
|
+
zero: You have no messages
|
135
|
+
|
136
|
+
<b>NOTE:</b> Rais I18n recognizes the +zero+ option.
|
137
|
+
|
138
|
+
If you need special rules just define them for your language, for example for ru locale in application.js:
|
139
|
+
|
140
|
+
I18n.pluralizationRules.ru = function (n) {
|
141
|
+
return n % 10 == 1 && n % 100 != 11 ? "one" : [2, 3, 4].indexOf(n % 10) >= 0 && [12, 13, 14].indexOf(n % 100) < 0 ? "few" : n % 10 == 0 || [5, 6, 7, 8, 9].indexOf(n % 10) >= 0 || [11, 12, 13, 14].indexOf(n % 100) >= 0 ? "many" : "other";
|
142
|
+
}
|
143
|
+
|
144
|
+
You can find all rules on http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
|
145
|
+
|
146
|
+
If you're using the same scope over and over again, you may use the +scope+ option.
|
147
|
+
|
148
|
+
var options = {scope: "activerecord.attributes.user"};
|
149
|
+
|
150
|
+
I18n.t("name", options);
|
151
|
+
I18n.t("email", options);
|
152
|
+
I18n.t("username", options);
|
153
|
+
|
154
|
+
You also provide an array as scope.
|
155
|
+
|
156
|
+
// use the greetings.hello scope
|
157
|
+
I18n.t(["greetings", "hello"]);
|
158
|
+
|
159
|
+
==== Number formatting
|
160
|
+
|
161
|
+
Similar to Rails helpers, you have localized number and currency formatting.
|
162
|
+
|
163
|
+
I18n.l("currency", 1990.99);
|
164
|
+
// $1,990.99
|
165
|
+
|
166
|
+
I18n.l("number", 1990.99);
|
167
|
+
// 1,990.99
|
168
|
+
|
169
|
+
I18n.l("percentage", 123.45);
|
170
|
+
// 123.450%
|
171
|
+
|
172
|
+
To have more control over number formatting, you can use the <tt>I18n.toNumber</tt>, <tt>I18n.toPercentage</tt>, <tt>I18n.toCurrency</tt> and <tt>I18n.toHumanSize</tt> functions.
|
173
|
+
|
174
|
+
I18n.toNumber(1000); // 1,000.000
|
175
|
+
I18n.toCurrency(1000); // $1,000.00
|
176
|
+
I18n.toPercentage(100); // 100.000%
|
177
|
+
|
178
|
+
The +toNumber+ and +toPercentage+ functions accept the following options:
|
179
|
+
|
180
|
+
* +precision+: defaults to 3
|
181
|
+
* +separator+: defaults to <tt>.</tt>
|
182
|
+
* +delimiter+: defaults to <tt>,</tt>
|
183
|
+
* +strip_insignificant_zeros+: defaults to <tt>false</tt>
|
184
|
+
|
185
|
+
See some number formatting examples:
|
186
|
+
|
187
|
+
I18n.toNumber(1000, {precision: 0}); // 1,000
|
188
|
+
I18n.toNumber(1000, {delimiter: ".", separator: ","}); // 1.000,000
|
189
|
+
I18n.toNumber(1000, {delimiter: ".", precision: 0}); // 1.000
|
190
|
+
|
191
|
+
The +toCurrency+ function accepts the following options:
|
192
|
+
|
193
|
+
* +precision+: sets the level of precision
|
194
|
+
* +separator+: sets the separator between the units
|
195
|
+
* +delimiter+: sets the thousands delimiter
|
196
|
+
* +format+: sets the format of the output string
|
197
|
+
* +unit+: sets the denomination of the currency
|
198
|
+
* +strip_insignificant_zeros+: defaults to <tt>false</tt>
|
199
|
+
|
200
|
+
You can provide only the options you want to override:
|
201
|
+
|
202
|
+
I18n.toCurrency(1000, {precision: 0}); // $1,000
|
203
|
+
|
204
|
+
The +toHumanSize+ function accepts the following options:
|
205
|
+
|
206
|
+
* +precision+: defaults to 1
|
207
|
+
* +separator+: defaults to <tt>.</tt>
|
208
|
+
* +delimiter+: defaults to <tt>""</tt>
|
209
|
+
* +strip_insignificant_zeros+: defaults to <tt>false</tt>
|
210
|
+
* +format+: defaults to <tt>%n%u</tt>
|
211
|
+
|
212
|
+
I18n.toHumanSize(1234); // 1KB
|
213
|
+
I18n.toHumanSize(1234 * 1024); // 1MB
|
214
|
+
|
215
|
+
==== Date formatting
|
216
|
+
|
217
|
+
// accepted formats
|
218
|
+
I18n.l("date.formats.short", "2009-09-18"); // yyyy-mm-dd
|
219
|
+
I18n.l("time.formats.short", "2009-09-18 23:12:43"); // yyyy-mm-dd hh:mm:ss
|
220
|
+
I18n.l("time.formats.short", "2009-11-09T18:10:34"); // JSON format with local Timezone (part of ISO-8601)
|
221
|
+
I18n.l("time.formats.short", "2009-11-09T18:10:34Z"); // JSON format in UTC (part of ISO-8601)
|
222
|
+
I18n.l("date.formats.short", 1251862029000); // Epoch time
|
223
|
+
I18n.l("date.formats.short", "09/18/2009"); // mm/dd/yyyy
|
224
|
+
I18n.l("date.formats.short", (new Date())); // Date object
|
225
|
+
|
226
|
+
If you prefer, you can use the <tt>I18n.strftime</tt> function to format dates.
|
227
|
+
|
228
|
+
var date = new Date();
|
229
|
+
I18n.strftime(date, "%d/%m/%Y");
|
230
|
+
|
231
|
+
The accepted formats are:
|
232
|
+
|
233
|
+
%a - The abbreviated weekday name (Sun)
|
234
|
+
%A - The full weekday name (Sunday)
|
235
|
+
%b - The abbreviated month name (Jan)
|
236
|
+
%B - The full month name (January)
|
237
|
+
%c - The preferred local date and time representation
|
238
|
+
%d - Day of the month (01..31)
|
239
|
+
%-d - Day of the month (1..31)
|
240
|
+
%H - Hour of the day, 24-hour clock (00..23)
|
241
|
+
%-H - Hour of the day, 24-hour clock (0..23)
|
242
|
+
%I - Hour of the day, 12-hour clock (01..12)
|
243
|
+
%-I - Hour of the day, 12-hour clock (1..12)
|
244
|
+
%m - Month of the year (01..12)
|
245
|
+
%-m - Month of the year (1..12)
|
246
|
+
%M - Minute of the hour (00..59)
|
247
|
+
%-M - Minute of the hour (0..59)
|
248
|
+
%p - Meridian indicator (AM or PM)
|
249
|
+
%S - Second of the minute (00..60)
|
250
|
+
%-S - Second of the minute (0..60)
|
251
|
+
%w - Day of the week (Sunday is 0, 0..6)
|
252
|
+
%y - Year without a century (00..99)
|
253
|
+
%-y - Year without a century (0..99)
|
254
|
+
%Y - Year with century
|
255
|
+
%z - Timezone offset (+0545)
|
256
|
+
|
257
|
+
Check out <tt>vendor/plugins/i18n-js/spec/i18n_spec.js</tt> for more examples!
|
258
|
+
|
259
|
+
== Using I18nJS with other languages (Python, PHP, ...)
|
260
|
+
|
261
|
+
The JavaScript library is language agnostic; so you can use it with PHP, Python, [you favorite language here].
|
262
|
+
The only requirement is that you need to set the +translations+ attribute like following:
|
263
|
+
|
264
|
+
I18n.translations = {};
|
265
|
+
|
266
|
+
I18n.translations["en"] = {
|
267
|
+
message: "Some special message for you"
|
268
|
+
}
|
269
|
+
|
270
|
+
I18n.translations["pt-BR"] = {
|
271
|
+
message: "Uma mensagem especial para você"
|
272
|
+
}
|
273
|
+
|
274
|
+
== Maintainer
|
275
|
+
|
276
|
+
* Nando Vieira - http://simplesideias.com.br
|
277
|
+
* Sébastien Grosjean - http://github.com/ZenCocoon
|
278
|
+
|
279
|
+
== Contributing
|
280
|
+
|
281
|
+
Once you've made your great commits:
|
282
|
+
|
283
|
+
1. Fork[http://help.github.com/forking/] I18n-JS
|
284
|
+
2. Create a topic branch - <tt>git checkout -b my_branch</tt>
|
285
|
+
3. Push to your branch - <tt>git push origin my_branch</tt>
|
286
|
+
4. Create an Issue[http://github.com/fnando/i18n-js/issues] with a link to your branch
|
287
|
+
5. That's it!
|
288
|
+
|
289
|
+
Please respect the indentation rules. And use 2 spaces, not tabs.
|
290
|
+
|
291
|
+
=== Running tests
|
292
|
+
|
293
|
+
First, install all dependencies.
|
294
|
+
|
295
|
+
bundle install
|
296
|
+
|
297
|
+
Then just run <tt>rake spec</tt>.
|
298
|
+
|
299
|
+
== License
|
300
|
+
|
301
|
+
(The MIT License)
|
302
|
+
|
303
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
304
|
+
a copy of this software and associated documentation files (the
|
305
|
+
'Software'), to deal in the Software without restriction, including
|
306
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
307
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
308
|
+
permit persons to whom the Software is furnished to do so, subject to
|
309
|
+
the following conditions:
|
310
|
+
|
311
|
+
The above copyright notice and this permission notice shall be
|
312
|
+
included in all copies or substantial portions of the Software.
|
313
|
+
|
314
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
315
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
316
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
317
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
318
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
319
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
320
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require "spec_js/rake_task"
|
5
|
+
SpecJs::RakeTask.new do |t|
|
6
|
+
t.env_js = false
|
7
|
+
end
|
8
|
+
|
9
|
+
require "rspec/core/rake_task"
|
10
|
+
RSpec::Core::RakeTask.new(:"spec:ruby")
|
11
|
+
|
12
|
+
desc "Run all specs"
|
13
|
+
task :spec => [:"spec:ruby", :"spec:js"]
|
data/config/i18n-js.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Split context in several files.
|
2
|
+
# By default only one file with all translations is exported and
|
3
|
+
# no configuration is required. Your settings for asset pipeline
|
4
|
+
# are automatically recognized.
|
5
|
+
#
|
6
|
+
# If you want to split translations into several files or specify
|
7
|
+
# locale contexts that will be exported, just use this file to do
|
8
|
+
# so.
|
9
|
+
#
|
10
|
+
# If you're going to use the Rails 3.1 asset pipeline, change
|
11
|
+
# the following configuration to something like this:
|
12
|
+
#
|
13
|
+
# translations:
|
14
|
+
# - file: "app/assets/javascripts/i18n/translations.js"
|
15
|
+
#
|
16
|
+
# If you're running an old version, you can use something
|
17
|
+
# like this:
|
18
|
+
#
|
19
|
+
# translations:
|
20
|
+
# - file: "public/javascripts/translations.js"
|
21
|
+
# only: "*"
|
22
|
+
#
|
data/i18n-js.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "i18n-js/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mlanett-i18n-js"
|
7
|
+
s.version = SimplesIdeias::I18n::Version::STRING
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Nando Vieira"]
|
10
|
+
s.email = ["fnando.vieira@gmail.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/i18n-js"
|
12
|
+
s.summary = "It's a small library to provide the Rails I18n translations on the Javascript."
|
13
|
+
s.description = s.summary
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "i18n"
|
21
|
+
s.add_development_dependency "fakeweb"
|
22
|
+
s.add_development_dependency "activesupport", ">= 3.0.0"
|
23
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
24
|
+
s.add_development_dependency "spec-js", "~> 0.1.0.beta.0"
|
25
|
+
s.add_development_dependency "rake"
|
26
|
+
s.add_development_dependency "pry"
|
27
|
+
end
|