i18n-js-pika 3.0.0.rc6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/CHANGELOG.md +13 -0
  4. data/Gemfile +2 -0
  5. data/Gemfile.lock +52 -0
  6. data/README.md +326 -0
  7. data/Rakefile +13 -0
  8. data/app/assets/javascripts/i18n/filtered.js.erb +2 -0
  9. data/app/assets/javascripts/i18n/shims.js +93 -0
  10. data/app/assets/javascripts/i18n/translations.js +3 -0
  11. data/app/assets/javascripts/i18n.js +690 -0
  12. data/i18n-js.gemspec +27 -0
  13. data/lib/i18n/js/engine.rb +22 -0
  14. data/lib/i18n/js/middleware.rb +59 -0
  15. data/lib/i18n/js/version.rb +12 -0
  16. data/lib/i18n/js.rb +162 -0
  17. data/lib/i18n-js.rb +1 -0
  18. data/lib/tasks/export.rake +8 -0
  19. data/package.json +11 -0
  20. data/spec/fixtures/custom_path.yml +4 -0
  21. data/spec/fixtures/default.yml +4 -0
  22. data/spec/fixtures/js_file_per_locale.yml +3 -0
  23. data/spec/fixtures/locales.yml +76 -0
  24. data/spec/fixtures/multiple_conditions.yml +5 -0
  25. data/spec/fixtures/multiple_files.yml +6 -0
  26. data/spec/fixtures/no_config.yml +2 -0
  27. data/spec/fixtures/no_scope.yml +3 -0
  28. data/spec/fixtures/simple_scope.yml +4 -0
  29. data/spec/i18n_js_spec.rb +139 -0
  30. data/spec/js/currency.spec.js +60 -0
  31. data/spec/js/current_locale.spec.js +19 -0
  32. data/spec/js/dates.spec.js +222 -0
  33. data/spec/js/defaults.spec.js +23 -0
  34. data/spec/js/interpolation.spec.js +28 -0
  35. data/spec/js/jasmine/MIT.LICENSE +20 -0
  36. data/spec/js/jasmine/jasmine-html.js +190 -0
  37. data/spec/js/jasmine/jasmine.css +166 -0
  38. data/spec/js/jasmine/jasmine.js +2476 -0
  39. data/spec/js/jasmine/jasmine_favicon.png +0 -0
  40. data/spec/js/localization.spec.js +41 -0
  41. data/spec/js/numbers.spec.js +142 -0
  42. data/spec/js/placeholder.spec.js +24 -0
  43. data/spec/js/pluralization.spec.js +105 -0
  44. data/spec/js/prepare_options.spec.js +41 -0
  45. data/spec/js/specs.html +46 -0
  46. data/spec/js/translate.spec.js +120 -0
  47. data/spec/js/translations.js +120 -0
  48. data/spec/spec_helper.rb +41 -0
  49. metadata +196 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f2f195e7dd4f8ea76f020bb8046f68e557808c8
4
+ data.tar.gz: c3c36611fd1fc1d428bab5f849e68588cbe6de6d
5
+ SHA512:
6
+ metadata.gz: cc97a5c29ca24a1e3acfa8b681f0f52f6407cb8ceb3e90e434d25b89877726ff49815f7fb7f3a7ca2f0793e5e1bbd180d1e7d62f0cfd6182b706c48e2f64404d
7
+ data.tar.gz: 25086f2c7ca230f0c7a77a37efd39c384a782595ca87c9af276f9031c83a6c181ecdd87ed58e6fae82223f63b156b877f1d43641837a2ed93d22bcb740e874a0
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+ node_modules
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ Changelog
2
+ =========
3
+
4
+ Major changes to I18n.js for each release. Please see the Git log for complete list of changes.
5
+
6
+ 3.0.0.rc6 (2013-12-13)
7
+ -------------------
8
+
9
+ * Add support for +00:00 style time zone designator (https://github.com/fnando/i18n-js/pull/167)
10
+ * Add back rake task for export (`rake i18n:js:export`)
11
+ * Always limit output file to only the I18n.available_locales (https://github.com/fnando/i18n-js/pull/162)
12
+ * Not overriding translation when manually run `I18n::JS.export` (https://github.com/fnando/i18n-js/pull/171)
13
+ * Move missing placeholder text generation into its own function (for easier debugging) (https://github.com/fnando/i18n-js/pull/169)
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i18n-js (3.0.0.rc5)
5
+ i18n
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (3.2.12)
11
+ i18n (~> 0.6)
12
+ multi_json (~> 1.0)
13
+ awesome_print (1.1.0)
14
+ coderay (1.0.9)
15
+ diff-lcs (1.2.1)
16
+ i18n (0.6.4)
17
+ method_source (0.8.1)
18
+ multi_json (1.6.1)
19
+ pry (0.9.12)
20
+ coderay (~> 1.0.5)
21
+ method_source (~> 0.8)
22
+ slop (~> 3.4)
23
+ pry-meta (0.0.5)
24
+ awesome_print
25
+ pry
26
+ pry-nav
27
+ pry-remote
28
+ pry-nav (0.2.3)
29
+ pry (~> 0.9.10)
30
+ pry-remote (0.1.7)
31
+ pry (~> 0.9)
32
+ slop (~> 3.0)
33
+ rake (10.0.3)
34
+ rspec (2.13.0)
35
+ rspec-core (~> 2.13.0)
36
+ rspec-expectations (~> 2.13.0)
37
+ rspec-mocks (~> 2.13.0)
38
+ rspec-core (2.13.0)
39
+ rspec-expectations (2.13.0)
40
+ diff-lcs (>= 1.1.3, < 2.0)
41
+ rspec-mocks (2.13.0)
42
+ slop (3.4.3)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ activesupport
49
+ i18n-js!
50
+ pry-meta
51
+ rake
52
+ rspec
data/README.md ADDED
@@ -0,0 +1,326 @@
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
+ $ rake 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
+ **NOTE:** Just make sure you apply your configuration **after i18n.js** is loaded. Otherwise, your settings will be ignored.
69
+
70
+ In practice, you'll have something like the following in your `application.html.erb`:
71
+
72
+ <script type="text/javascript">
73
+ I18n.defaultLocale = "<%= I18n.default_locale %>";
74
+ I18n.locale = "<%= I18n.locale %>";
75
+ </script>
76
+
77
+ You can use translate your messages:
78
+
79
+ I18n.t("some.scoped.translation");
80
+ // or translate with explicite setting of locale
81
+ I18n.t("some.scoped.translation", {locale: "fr"});
82
+
83
+ You can also interpolate values:
84
+
85
+ I18n.t("hello", {name: "John Doe"});
86
+
87
+ You can set default values for missing scopes:
88
+
89
+ // simple translation
90
+ I18n.t("some.missing.scope", {defaultValue: "A default message"});
91
+
92
+ // with interpolation
93
+ I18n.t("noun", {defaultValue: "I'm a {{noun}}", noun: "Mac"});
94
+
95
+ Translation fallback can be enabled by enabling the `I18n.fallbacks` option:
96
+
97
+ <script type="text/javascript">
98
+ I18n.fallbacks = true;
99
+ </script>
100
+
101
+ By default missing translations will first be looked for in less
102
+ specific versions of the requested locale and if that fails by taking
103
+ them from your `I18n.defaultLocale`.
104
+
105
+ // if I18n.defaultLocale = "en" and translation doesn't exist
106
+ // for I18n.locale = "de-DE" this key will be taken from "de" locale scope
107
+ // or, if that also doesn't exist, from "en" locale scope
108
+ I18n.t("some.missing.scope");
109
+
110
+ Custom fallback rules can also be specified for a particular language. There
111
+ are three different ways of doing it so:
112
+
113
+ I18n.locales.no = ["nb", "en"];
114
+ I18n.locales.no = "nb";
115
+ I18n.locales.no = function(locale){ return ["nb"]; };
116
+
117
+ Pluralization is possible as well and by default provides english rules:
118
+
119
+ I18n.t("inbox.counting", {count: 10}); // You have 10 messages
120
+
121
+ The sample above expects the following translation:
122
+
123
+ en:
124
+ inbox:
125
+ counting:
126
+ one: You have 1 new message
127
+ other: You have {{count}} new messages
128
+ zero: You have no messages
129
+
130
+ **NOTE:** Rais I18n recognizes the `zero` option.
131
+
132
+ If you need special rules just define them for your language, for example Russian, just add a new pluralizer:
133
+
134
+ I18n.pluralization["ru"] = function (count) {
135
+ 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";
136
+ };
137
+
138
+ You can find all rules on <http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html>.
139
+
140
+ If you're using the same scope over and over again, you may use the `scope` option.
141
+
142
+ var options = {scope: "activerecord.attributes.user"};
143
+
144
+ I18n.t("name", options);
145
+ I18n.t("email", options);
146
+ I18n.t("username", options);
147
+
148
+ You can also provide an array as scope.
149
+
150
+ // use the greetings.hello scope
151
+ I18n.t(["greetings", "hello"]);
152
+
153
+ #### Number formatting
154
+
155
+ Similar to Rails helpers, you have localized number and currency formatting.
156
+
157
+ I18n.l("currency", 1990.99);
158
+ // $1,990.99
159
+
160
+ I18n.l("number", 1990.99);
161
+ // 1,990.99
162
+
163
+ I18n.l("percentage", 123.45);
164
+ // 123.450%
165
+
166
+ To have more control over number formatting, you can use the
167
+ `I18n.toNumber`, `I18n.toPercentage`, `I18n.toCurrency` and `I18n.toHumanSize`
168
+ functions.
169
+
170
+ I18n.toNumber(1000); // 1,000.000
171
+ I18n.toCurrency(1000); // $1,000.00
172
+ I18n.toPercentage(100); // 100.000%
173
+
174
+ The `toNumber` and `toPercentage` functions accept the following options:
175
+
176
+ - `precision`: defaults to `3`
177
+ - `separator`: defaults to `.`
178
+ - `delimiter`: defaults to `,`
179
+ - `strip_insignificant_zeros`: defaults to `false`
180
+
181
+ See some number formatting examples:
182
+
183
+ I18n.toNumber(1000, {precision: 0}); // 1,000
184
+ I18n.toNumber(1000, {delimiter: ".", separator: ","}); // 1.000,000
185
+ I18n.toNumber(1000, {delimiter: ".", precision: 0}); // 1.000
186
+
187
+ The `toCurrency` function accepts the following options:
188
+
189
+ - `precision`: sets the level of precision
190
+ - `separator`: sets the separator between the units
191
+ - `delimiter`: sets the thousands delimiter
192
+ - `format`: sets the format of the output string
193
+ - `unit`: sets the denomination of the currency
194
+ - `strip_insignificant_zeros`: defaults to `false`
195
+
196
+ You can provide only the options you want to override:
197
+
198
+ I18n.toCurrency(1000, {precision: 0}); // $1,000
199
+
200
+ The `toHumanSize` function accepts the following options:
201
+
202
+ - `precision`: defaults to `1`
203
+ - `separator`: defaults to `.`
204
+ - `delimiter`: defaults to `""`
205
+ - `strip_insignificant_zeros`: defaults to `false`
206
+ - `format`: defaults to `%n%u`
207
+
208
+ <!---->
209
+
210
+ I18n.toHumanSize(1234); // 1KB
211
+ I18n.toHumanSize(1234 * 1024); // 1MB
212
+
213
+ #### Date formatting
214
+
215
+ // accepted formats
216
+ I18n.l("date.formats.short", "2009-09-18"); // yyyy-mm-dd
217
+ I18n.l("time.formats.short", "2009-09-18 23:12:43"); // yyyy-mm-dd hh:mm:ss
218
+ I18n.l("time.formats.short", "2009-11-09T18:10:34"); // JSON format with local Timezone (part of ISO-8601)
219
+ I18n.l("time.formats.short", "2009-11-09T18:10:34Z"); // JSON format in UTC (part of ISO-8601)
220
+ I18n.l("date.formats.short", 1251862029000); // Epoch time
221
+ I18n.l("date.formats.short", "09/18/2009"); // mm/dd/yyyy
222
+ I18n.l("date.formats.short", (new Date())); // Date object
223
+
224
+ If you prefer, you can use the `I18n.strftime` function to format dates.
225
+
226
+ var date = new Date();
227
+ I18n.strftime(date, "%d/%m/%Y");
228
+
229
+ The accepted formats are:
230
+
231
+ %a - The abbreviated weekday name (Sun)
232
+ %A - The full weekday name (Sunday)
233
+ %b - The abbreviated month name (Jan)
234
+ %B - The full month name (January)
235
+ %c - The preferred local date and time representation
236
+ %d - Day of the month (01..31)
237
+ %-d - Day of the month (1..31)
238
+ %H - Hour of the day, 24-hour clock (00..23)
239
+ %-H - Hour of the day, 24-hour clock (0..23)
240
+ %I - Hour of the day, 12-hour clock (01..12)
241
+ %-I - Hour of the day, 12-hour clock (1..12)
242
+ %m - Month of the year (01..12)
243
+ %-m - Month of the year (1..12)
244
+ %M - Minute of the hour (00..59)
245
+ %-M - Minute of the hour (0..59)
246
+ %p - Meridian indicator (AM or PM)
247
+ %S - Second of the minute (00..60)
248
+ %-S - Second of the minute (0..60)
249
+ %w - Day of the week (Sunday is 0, 0..6)
250
+ %y - Year without a century (00..99)
251
+ %-y - Year without a century (0..99)
252
+ %Y - Year with century
253
+ %z - Timezone offset (+0545)
254
+
255
+ Check out `spec/*.spec.js` files for more examples!
256
+
257
+ ## Using I18n.js with other languages (Python, PHP, ...)
258
+
259
+ The JavaScript library is language agnostic; so you can use it with PHP, Python, [you favorite language here].
260
+ The only requirement is that you need to set the `translations` attribute like following:
261
+
262
+ I18n.translations = {};
263
+
264
+ I18n.translations["en"] = {
265
+ message: "Some special message for you"
266
+ }
267
+
268
+ I18n.translations["pt-BR"] = {
269
+ message: "Uma mensagem especial para você"
270
+ }
271
+
272
+ ## Maintainer
273
+
274
+ - Nando Vieira - <http://nandovieira.com.br>
275
+
276
+ ## Contributing
277
+
278
+ Once you've made your great commits:
279
+
280
+ 1. [Fork](http://help.github.com/forking/) I18n.js
281
+ 2. Create a topic branch - `git checkout -b my_branch`
282
+ 3. Push to your branch - `git push origin my_branch`
283
+ 4. [Create an Issue](http://github.com/fnando/i18n-js/issues) with a link to your branch
284
+ 5. That's it!
285
+
286
+ Please respect the indentation rules and code style.
287
+ And use 2 spaces, not tabs. And don't touch the versioning thing.
288
+
289
+ ## Running tests
290
+
291
+ You can run I18n tests using Node.js or your browser.
292
+
293
+ To use Node.js, install the `jasmine-node` library:
294
+
295
+ $ npm install jasmine-node
296
+
297
+ Then execute the following command from the lib's root directory:
298
+
299
+ $ npm test
300
+
301
+ To run using your browser, just open the `spec/js/specs.html` file.
302
+
303
+ You can run both Ruby and JavaScript specs with `rake spec`.
304
+
305
+ ## License
306
+
307
+ (The MIT License)
308
+
309
+ Permission is hereby granted, free of charge, to any person obtaining
310
+ a copy of this software and associated documentation files (the
311
+ 'Software'), to deal in the Software without restriction, including
312
+ without limitation the rights to use, copy, modify, merge, publish,
313
+ distribute, sublicense, and/or sell copies of the Software, and to
314
+ permit persons to whom the Software is furnished to do so, subject to
315
+ the following conditions:
316
+
317
+ The above copyright notice and this permission notice shall be
318
+ included in all copies or substantial portions of the Software.
319
+
320
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
321
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
322
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
323
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
324
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
325
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
326
+ 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 "npm", "test"
10
+ end
11
+
12
+ desc "Run all specs"
13
+ task :spec => ["spec:ruby", "spec:js"]
@@ -0,0 +1,2 @@
1
+ <%# encoding: utf-8 %>
2
+ ;I18n.translations = <%= I18n::JS.filtered_translations.to_json %>;
@@ -0,0 +1,93 @@
1
+ // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
2
+ if (!Array.prototype.indexOf) {
3
+ Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
4
+ "use strict";
5
+ if (this == null) {
6
+ throw new TypeError();
7
+ }
8
+ var t = Object(this);
9
+ var len = t.length >>> 0;
10
+ if (len === 0) {
11
+ return -1;
12
+ }
13
+ var n = 0;
14
+ if (arguments.length > 1) {
15
+ n = Number(arguments[1]);
16
+ if (n != n) { // shortcut for verifying if it's NaN
17
+ n = 0;
18
+ } else if (n != 0 && n != Infinity && n != -Infinity) {
19
+ n = (n > 0 || -1) * Math.floor(Math.abs(n));
20
+ }
21
+ }
22
+ if (n >= len) {
23
+ return -1;
24
+ }
25
+ var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
26
+ for (; k < len; k++) {
27
+ if (k in t && t[k] === searchElement) {
28
+ return k;
29
+ }
30
+ }
31
+ return -1;
32
+ }
33
+ }
34
+
35
+ // Production steps of ECMA-262, Edition 5, 15.4.4.18
36
+ // Reference: http://es5.github.com/#x15.4.4.18
37
+ // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach
38
+ if ( !Array.prototype.forEach ) {
39
+
40
+ Array.prototype.forEach = function forEach( callback, thisArg ) {
41
+
42
+ var T, k;
43
+
44
+ if ( this == null ) {
45
+ throw new TypeError( "this is null or not defined" );
46
+ }
47
+
48
+ // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
49
+ var O = Object(this);
50
+
51
+ // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
52
+ // 3. Let len be ToUint32(lenValue).
53
+ var len = O.length >>> 0; // Hack to convert O.length to a UInt32
54
+
55
+ // 4. If IsCallable(callback) is false, throw a TypeError exception.
56
+ // See: http://es5.github.com/#x9.11
57
+ if ( {}.toString.call(callback) !== "[object Function]" ) {
58
+ throw new TypeError( callback + " is not a function" );
59
+ }
60
+
61
+ // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
62
+ if ( thisArg ) {
63
+ T = thisArg;
64
+ }
65
+
66
+ // 6. Let k be 0
67
+ k = 0;
68
+
69
+ // 7. Repeat, while k < len
70
+ while( k < len ) {
71
+
72
+ var kValue;
73
+
74
+ // a. Let Pk be ToString(k).
75
+ // This is implicit for LHS operands of the in operator
76
+ // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
77
+ // This step can be combined with c
78
+ // c. If kPresent is true, then
79
+ if ( Object.prototype.hasOwnProperty.call(O, k) ) {
80
+
81
+ // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
82
+ kValue = O[ k ];
83
+
84
+ // ii. Call the Call internal method of callback with T as the this value and
85
+ // argument list containing kValue, k, and O.
86
+ callback.call( T, kValue, k, O );
87
+ }
88
+ // d. Increase k by 1.
89
+ k++;
90
+ }
91
+ // 8. return undefined
92
+ };
93
+ }
@@ -0,0 +1,3 @@
1
+ //= require i18n/shims
2
+ //= require i18n
3
+ //= require i18n/filtered