socialcast-i18n-js 4.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.gitignore +13 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +2 -0
  4. data/README.md +324 -0
  5. data/README.rdoc +320 -0
  6. data/Rakefile +13 -0
  7. data/app/assets/javascripts/i18n/translations.js.erb +6 -0
  8. data/app/assets/javascripts/i18n.js +661 -0
  9. data/config/i18n-js.yml +22 -0
  10. data/i18n-js.gemspec +28 -0
  11. data/lib/i18n/js/engine.rb +6 -0
  12. data/lib/i18n/js/file_dependency_processor.rb +17 -0
  13. data/lib/i18n/js/railtie.rb +17 -0
  14. data/lib/i18n/js/translator.rb +56 -0
  15. data/lib/i18n/js/version.rb +10 -0
  16. data/lib/i18n-js/engine.rb +63 -0
  17. data/lib/i18n-js/middleware.rb +59 -0
  18. data/lib/i18n-js/railtie.rb +13 -0
  19. data/lib/i18n-js/rake.rb +16 -0
  20. data/lib/i18n-js/version.rb +10 -0
  21. data/lib/i18n-js.rb +22 -0
  22. data/lib/tasks/i18n-js.rake +17 -0
  23. data/spec/file_dependency_processor_spec.rb +32 -0
  24. data/spec/fixtures/custom_path.yml +2 -0
  25. data/spec/fixtures/default.yml +2 -0
  26. data/spec/fixtures/js_file_per_locale.yml +1 -0
  27. data/spec/fixtures/locales.yml +76 -0
  28. data/spec/fixtures/multiple_conditions.yml +3 -0
  29. data/spec/fixtures/no_config.yml +2 -0
  30. data/spec/fixtures/simple_scope.yml +2 -0
  31. data/spec/i18n_js_spec.rb +11 -0
  32. data/spec/i18n_spec.js +820 -0
  33. data/spec/i18n_spec.rb +205 -0
  34. data/spec/js/currency.spec.js +60 -0
  35. data/spec/js/current_locale.spec.js +19 -0
  36. data/spec/js/dates.spec.js +218 -0
  37. data/spec/js/defaults.spec.js +23 -0
  38. data/spec/js/interpolation.spec.js +28 -0
  39. data/spec/js/jasmine/MIT.LICENSE +20 -0
  40. data/spec/js/jasmine/jasmine-html.js +190 -0
  41. data/spec/js/jasmine/jasmine.css +166 -0
  42. data/spec/js/jasmine/jasmine.js +2476 -0
  43. data/spec/js/jasmine/jasmine_favicon.png +0 -0
  44. data/spec/js/localization.spec.js +41 -0
  45. data/spec/js/numbers.spec.js +124 -0
  46. data/spec/js/placeholder.spec.js +24 -0
  47. data/spec/js/pluralization.spec.js +105 -0
  48. data/spec/js/prepare_options.spec.js +41 -0
  49. data/spec/js/specs.html +46 -0
  50. data/spec/js/translate.spec.js +119 -0
  51. data/spec/js/translations.js +115 -0
  52. data/spec/resources/custom_path.yml +4 -0
  53. data/spec/resources/default.yml +4 -0
  54. data/spec/resources/js_file_per_locale.yml +3 -0
  55. data/spec/resources/locales.yml +76 -0
  56. data/spec/resources/multiple_conditions.yml +6 -0
  57. data/spec/resources/multiple_files.yml +6 -0
  58. data/spec/resources/no_config.yml +2 -0
  59. data/spec/resources/no_scope.yml +3 -0
  60. data/spec/resources/simple_scope.yml +4 -0
  61. data/spec/spec_helper.rb +40 -0
  62. data/spec/translator_spec.rb +45 -0
  63. data/vendor/assets/javascripts/i18n/translations.js.erb +9 -0
  64. data/vendor/assets/javascripts/i18n.js +531 -0
  65. data/vendor/assets/javascripts/underscore.js +1059 -0
  66. metadata +240 -0
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ ## PROJECT::GENERAL
2
+ coverage
3
+ rdoc
4
+ pkg
5
+ *.gem
6
+ .bundle
7
+ Gemfile.lock
8
+ pkg/*
9
+ doc
10
+ pkg
11
+ spec/tmp
12
+ .DS_Store
13
+
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,324 @@
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, install the `jasmine-node` library:
292
+
293
+ $ npm install jasmine-node -g
294
+
295
+ Then execute the following command from the lib's root directory:
296
+
297
+ $ jasmine-node spec/js
298
+
299
+ To run using your browser, just open the `spec/js/specs.html` file.
300
+
301
+ You can run both Ruby and JavaScript specs with `rake spec`.
302
+
303
+ ## License
304
+
305
+ (The MIT License)
306
+
307
+ Permission is hereby granted, free of charge, to any person obtaining
308
+ a copy of this software and associated documentation files (the
309
+ 'Software'), to deal in the Software without restriction, including
310
+ without limitation the rights to use, copy, modify, merge, publish,
311
+ distribute, sublicense, and/or sell copies of the Software, and to
312
+ permit persons to whom the Software is furnished to do so, subject to
313
+ the following conditions:
314
+
315
+ The above copyright notice and this permission notice shall be
316
+ included in all copies or substantial portions of the Software.
317
+
318
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
319
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
320
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
321
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
322
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
323
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
324
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:"spec:ruby")
6
+
7
+ desc "Run JavaScript specs"
8
+ task "spec:js" do
9
+ system "jasmine-node", "spec/js"
10
+ end
11
+
12
+ desc "Run all specs"
13
+ task :spec => ["spec:ruby", "spec:js"]
@@ -0,0 +1,6 @@
1
+ //= require i18n
2
+ //= require_self
3
+ <%# encoding: utf-8 %>
4
+ <% translations = I18n::Js::Translator.new(I18n::Js.config[:only]).translations %>
5
+ <% scoped_translation = translations.select { |locale, value| [nil, locale.to_s].include?(__FILE__[/translation\-(\w*)/,1]) } %>
6
+ ;I18n.translations = <%= scoped_translation.to_json %>;