i18n-js 3.0.0.rc13 → 3.0.0.rc14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d618c24aafbe01dfc9573cf7a3df0c214b9b48a
4
- data.tar.gz: 22bbc551ae4ba0d927c4d8b61fbf54996f51de4f
3
+ metadata.gz: 10a07ab318e610dfc7102d3d88015d8a55c42a76
4
+ data.tar.gz: 21fa8daa428b711f6d592c3b14bad7183e5af2f8
5
5
  SHA512:
6
- metadata.gz: f8ada8c660b2185867aa9c5ed68627aaee251c164032e39cf1b3fd0e9f0690f161defe732055f9eb97945084665208237252174ebf60131e66e6589fdf666506
7
- data.tar.gz: aabf496875397c179606adf78bba0bee468e5d923a7d3c6d21d36494b03aab55cc650fdf8a672eea0da410a9c57c18ea07ae7c377129aa275cea6254c260a073
6
+ metadata.gz: cef3f6f68e7ca327e96902093069c47df6897074d35db121700076e3e7137d1ae68a9222e76ded1bea5e8d3145597cc0f37c9651d12a3b62ccbf766ce8374186
7
+ data.tar.gz: 6940d5acc297f6482cb74caa56f64c85b2be20065a069fd26267956ee157ce139b2f92b425c9e2e55ce983c285bd66dcdae861454bc4371aa4e478687613da4d
data/CHANGELOG.md CHANGED
@@ -16,6 +16,18 @@ This project adheres to [Semantic Versioning](http://semver.org/).
16
16
 
17
17
  - Nothing
18
18
 
19
+
20
+ ## [3.0.0.rc14] - 2016-08-29
21
+
22
+ ### Changed
23
+
24
+ - [JS] Method `I18n.extend()` behave as deep merging instead of shallow merging. (https://github.com/fnando/i18n-js/pull/416)
25
+ - [Ruby] Use object/class instead of block when registering Sprockets preprocessor (https://github.com/fnando/i18n-js/pull/418)
26
+ To ensure that your cache will expire properly based on locale file content after upgrading,
27
+ you should run `rake assets:clobber` and/or other rake tasks that clear the asset cache once gem updated
28
+ - [Ruby] Detect & support rails 5 (https://github.com/fnando/i18n-js/pull/413)
29
+
30
+
19
31
  ## [3.0.0.rc13] - 2016-06-29
20
32
 
21
33
  ### Added
@@ -31,7 +43,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
31
43
  - [JS] Initialize option `missingBehaviour` & `missingTranslationPrefix` with default values ([#398](https://github.com/fnando/i18n-js/pull/398))
32
44
  - [JS] Throw an error when `I18n.strftime()` takes an invalid date ([#383](https://github.com/fnando/i18n-js/pull/383))
33
45
  - [JS] Fix default error message when translation missing to consider locale passed in options
34
- - [Ruby] Reset middleware cache on rails startup
46
+ - [Ruby] Reset middleware cache on rails startup
35
47
  ([#402](https://github.com/fnando/i18n-js/pull/402))
36
48
 
37
49
 
@@ -94,7 +106,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
94
106
  - [Ruby] Add `:except` option to exclude certain phrases or groups of phrases from the
95
107
  outputted translations ([#312](https://github.com/fnando/i18n-js/pull/312))
96
108
  - [JS] You can now set `I18n.missingBehavior='guess'` to have the scope string output as text instead of of the
97
- "[missing `scope`]" message when no translation is available.
109
+ "[missing `scope`]" message when no translation is available.
98
110
  Combined that with `I18n.missingTranslationPrefix='SOMETHING'` and you can
99
111
  still identify those missing strings.
100
112
  ([#304](https://github.com/fnando/i18n-js/pull/304))
@@ -110,8 +122,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
110
122
  ### enhancements
111
123
 
112
124
  - Add support for loading via AMD and CommonJS module loaders ([#266](https://github.com/fnando/i18n-js/pull/266))
113
- - Add `I18n.nullPlaceholder`
114
- Defaults to I18n.missingPlaceholder (`[missing {{name}} value]`)
125
+ - Add `I18n.nullPlaceholder`
126
+ Defaults to I18n.missingPlaceholder (`[missing {{name}} value]`)
115
127
  Set to `function() {return "";}` to match Ruby `I18n.t("name: %{name}", name: nil)`
116
128
  - For date formatting, you can now also add placeholders to the date format, see README for detail
117
129
  - Add fallbacks option to `i18n-js.yml`, defaults to `true`
@@ -121,8 +133,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
121
133
  - Fix factory initialization so that the Node/CommonJS branch only gets executed if the environment is Node/CommonJS
122
134
  (it currently will execute if module is defined in the global scope, which occurs with QUnit, for example)
123
135
  - Fix pluralization rules selection for negative `count` (e.g. `-1` was lead to use `one` for pluralization) ([#268](https://github.com/fnando/i18n-js/pull/268))
124
- - Remove check for `Rails.configuration.assets.compile` before telling Sprockets the dependency of translations JS file
125
- This might be the reason of many "cache not expired" issues
136
+ - Remove check for `Rails.configuration.assets.compile` before telling Sprockets the dependency of translations JS file
137
+ This might be the reason of many "cache not expired" issues
126
138
  Discovered/reported in #277
127
139
 
128
140
  ## 3.0.0.rc7
@@ -130,11 +142,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
130
142
  ### enhancements
131
143
 
132
144
  - The Rails Engine initializer is now named as `i18n-js.register_preprocessor` (https://github.com/fnando/i18n-js/pull/261)
133
- - Rename `I18n::JS.config_file` to `I18n::JS.config_file_path` and make it configurable
145
+ - Rename `I18n::JS.config_file` to `I18n::JS.config_file_path` and make it configurable
134
146
  Expected a `String`, default is still `config/i18n-js.yml`
135
147
  - When running `rake i18n:js:export`, the `i18n.js` will also be exported to `I18n::JS.export_i18n_js_dir_path` by default
136
- - Add `I18n::JS.export_i18n_js_dir_path`
137
- Expected a `String`, default is `public/javascripts`
148
+ - Add `I18n::JS.export_i18n_js_dir_path`
149
+ Expected a `String`, default is `public/javascripts`
138
150
  Set to `nil` will disable exporting `i18n.js`
139
151
 
140
152
  ### bug fixes
@@ -176,6 +188,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
176
188
 
177
189
 
178
190
 
179
- [Unreleased]: https://github.com/fnando/i18n-js/compare/v3.0.0.rc13...HEAD
191
+ [Unreleased]: https://github.com/fnando/i18n-js/compare/v3.0.0.rc14...HEAD
192
+ [3.0.0.rc14]: https://github.com/fnando/i18n-js/compare/v3.0.0.rc13...v3.0.0.rc14
180
193
  [3.0.0.rc13]: https://github.com/fnando/i18n-js/compare/v3.0.0.rc12...v3.0.0.rc13
181
194
  [3.0.0.rc12]: https://github.com/fnando/i18n-js/compare/v3.0.0.rc11...v3.0.0.rc12
data/README.md CHANGED
@@ -65,23 +65,23 @@ Then get the JS files following the instructions below.
65
65
 
66
66
  **There are two ways to get `translations.js`.**
67
67
 
68
- 1. This `translations.js` file can be automatically generated by the `I18n::JS::Middleware`.
69
- Just add `config.middleware.use I18n::JS::Middleware` to your `config/application.rb` file.
68
+ 1. This `translations.js` file can be automatically generated by the `I18n::JS::Middleware`.
69
+ Just add `config.middleware.use I18n::JS::Middleware` to your `config/application.rb` file.
70
70
  2. If you can't or prefer not to generate this file,
71
71
  you can move the middleware line to your `config/environments/development.rb` file
72
72
  and run `rake i18n:js:export` before deploying.
73
73
  This will export all translation files, including the custom scopes
74
- you may have defined on `config/i18n-js.yml`.
74
+ you may have defined on `config/i18n-js.yml`.
75
75
  If `I18n.available_locales` is set (e.g. in your Rails `config/application.rb` file)
76
- then only the specified locales will be exported.
76
+ then only the specified locales will be exported.
77
77
  Current version of `i18n.js` will also be exported to avoid version mismatching by downloading.
78
78
 
79
79
  #### Export Configuration (For translations)
80
80
 
81
- Exported translation files generated by `I18n::JS::Middleware` or `rake i18n:js:export` can be customized with config file `config/i18n-js.yml`
82
- (use `rails generate i18n:js:config` to create it).
83
- You can even get more files generated to different folders and with different translations to best suit your needs.
84
- The config file also affects developers using Asset Pipeline to require translations.
81
+ Exported translation files generated by `I18n::JS::Middleware` or `rake i18n:js:export` can be customized with config file `config/i18n-js.yml`
82
+ (use `rails generate i18n:js:config` to create it).
83
+ You can even get more files generated to different folders and with different translations to best suit your needs.
84
+ The config file also affects developers using Asset Pipeline to require translations.
85
85
  Except the option `file`, since all translations are required by adding `//= require i18n/translations`.
86
86
 
87
87
  Examples:
@@ -134,14 +134,14 @@ translations:
134
134
  #### Export Configuration (For other things)
135
135
 
136
136
  - `I18n::JS.config_file_path`
137
- Expected Type: `String`
138
- Default: `config/i18n-js.yml`
139
- Behaviour: Try to read the config file from that location
137
+ Expected Type: `String`
138
+ Default: `config/i18n-js.yml`
139
+ Behaviour: Try to read the config file from that location
140
140
 
141
141
  - `I18n::JS.export_i18n_js_dir_path`
142
- Expected Type: `String`
143
- Default: `public/javascripts`
144
- Behaviour:
142
+ Expected Type: `String`
143
+ Default: `public/javascripts`
144
+ Behaviour:
145
145
  - Any `String`: considered as a relative path for a folder to `Rails.root` and export `i18n.js` to that folder for `rake i18n:js:export`
146
146
  - Any non-`String` (`nil`, `false`, `:none`, etc): Disable `i18n.js` exporting
147
147
 
@@ -169,7 +169,7 @@ To find more examples on how to use the configuration file please refer to the t
169
169
 
170
170
  #### Fallbacks
171
171
 
172
- If you specify the `fallbacks` option, you will be able to fill missing translations with those inside fallback locale(s).
172
+ If you specify the `fallbacks` option, you will be able to fill missing translations with those inside fallback locale(s).
173
173
  Default value is `true`.
174
174
 
175
175
  Examples:
@@ -256,10 +256,10 @@ translations:
256
256
  ```
257
257
 
258
258
 
259
- #### Javscript Merge (:js_extend option)
259
+ #### Javscript Deep Merge (:js_extend option)
260
260
 
261
261
  By default, the output file Javascript will call the `I18n.extend` method to ensure that newly loaded locale
262
- files are shallow-merged with any locale data already in memory. To disable this either globally or per-file,
262
+ files are deep-merged with any locale data already in memory. To disable this either globally or per-file,
263
263
  set the `js_extend` option to false
264
264
 
265
265
  ```yaml
@@ -617,7 +617,7 @@ This method is useful for very large apps where a single contained translations.
617
617
  + file: "app/assets/javascript/nls/welcome.js"
618
618
  only:
619
619
  + '*.welcome.*'
620
-
620
+
621
621
  + file: "app/assets/javascript/nls/albums.js"
622
622
  only:
623
623
  + '*.albums.*'
@@ -687,13 +687,13 @@ To use this with require.js we are only going to change a few things from above.
687
687
  deps:
688
688
  + i18n
689
689
 
690
- # Finally in your modules
690
+ # Finally in your modules
691
691
  modules:
692
692
  + name: 'application'
693
693
  include:
694
694
  + i18n
695
695
  + 'nls/global'
696
-
696
+
697
697
  + name: 'welcome'
698
698
  exclude:
699
699
  + application
@@ -767,7 +767,7 @@ This means that new locale files will not be detected, and so they will not trig
767
767
  $ rake assets:precompile
768
768
  ```
769
769
  or similar commands. If you are precompiling assets on the target machine(s), cached pages may be broken by this, so they will need to be refreshed.
770
-
770
+
771
771
  2. You can change something in a different locale file.
772
772
 
773
773
  3. Finally, you can change `config.assets.version`.
@@ -79,6 +79,20 @@
79
79
  return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
80
80
  }
81
81
 
82
+ var merge = function (dest, obj) {
83
+ var key, value;
84
+ for (key in obj) if (obj.hasOwnProperty(key)) {
85
+ value = obj[key];
86
+ if (Object.prototype.toString.call(value) === '[object String]') {
87
+ dest[key] = value;
88
+ } else {
89
+ if (dest[key] == null) dest[key] = {};
90
+ merge(dest[key], value);
91
+ }
92
+ }
93
+ return dest;
94
+ };
95
+
82
96
  // Set default days/months translations.
83
97
  var DATE = {
84
98
  day_names: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
@@ -921,19 +935,10 @@
921
935
  * https://stackoverflow.com/questions/8157700/object-has-no-hasownproperty-method-i-e-its-undefined-ie8
922
936
  */
923
937
  I18n.extend = function ( obj1, obj2 ) {
924
- var extended = {};
925
- var prop;
926
- for (prop in obj1) {
927
- if (Object.prototype.hasOwnProperty.call(obj1, prop)) {
928
- extended[prop] = obj1[prop];
929
- }
930
- }
931
- for (prop in obj2) {
932
- if (Object.prototype.hasOwnProperty.call(obj2, prop)) {
933
- extended[prop] = obj2[prop];
934
- }
938
+ if (typeof(obj1) === "undefined" && typeof(obj2) === "undefined") {
939
+ return {};
935
940
  }
936
- return extended;
941
+ return merge(obj1, obj2);
937
942
  };
938
943
 
939
944
  // Set aliases, so we can save some typing.
@@ -12,6 +12,10 @@ module I18n
12
12
  safe_gem_check("rails", "~> 4.0", ">= 4.0.0.beta1") && running_rails4?
13
13
  end
14
14
 
15
+ def rails5?
16
+ safe_gem_check("rails", "~> 5.0", ">= 5.0.0.beta1") && running_rails5?
17
+ end
18
+
15
19
  def sprockets_supports_register_preprocessor?
16
20
  defined?(Sprockets) && Sprockets.respond_to?(:register_preprocessor)
17
21
  end
@@ -24,9 +28,13 @@ module I18n
24
28
  safe_gem_check("rails", '>= 3.0.0.beta')
25
29
  end
26
30
 
31
+ # This cannot be called at class definition time
32
+ # Since not all libraries are loaded
33
+ #
34
+ # Call this in an initializer
27
35
  def using_asset_pipeline?
28
36
  assets_pipeline_available =
29
- (rails3? || rails4?) &&
37
+ (rails3? || rails4? || rails5?) &&
30
38
  Rails.respond_to?(:application) &&
31
39
  Rails.application.respond_to?(:assets)
32
40
  rails3_assets_enabled =
@@ -34,7 +42,7 @@ module I18n
34
42
  assets_pipeline_available &&
35
43
  Rails.application.config.assets.enabled != false
36
44
 
37
- assets_pipeline_available && (rails4? || rails3_assets_enabled)
45
+ assets_pipeline_available && (rails4? || rails5? || rails3_assets_enabled)
38
46
  end
39
47
 
40
48
  private
@@ -47,6 +55,10 @@ module I18n
47
55
  running_rails? && Rails.version.to_i == 4
48
56
  end
49
57
 
58
+ def running_rails5?
59
+ running_rails? && Rails.version.to_i == 5
60
+ end
61
+
50
62
  def running_rails?
51
63
  defined?(Rails) && Rails.respond_to?(:version)
52
64
  end
@@ -2,39 +2,91 @@ require "i18n/js"
2
2
 
3
3
  module I18n
4
4
  module JS
5
+ # @api private
6
+ # The class cannot be private
7
+ class SprocketsExtension
8
+ # Actual definition is placed below
9
+ end
10
+
5
11
  class Engine < ::Rails::Engine
6
- # `sprockets.environment` was used for 1.x of `sprockets-rails`
7
- # https://github.com/rails/sprockets-rails/issues/227
8
- #
9
- # References for current values:
10
- #
11
- # Here is where sprockets are attached with Rails. There is no 'sprockets.environment' mentioned.
12
- # https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb
13
- #
14
- # Finisher hook is the place which should be used as border.
15
- # http://guides.rubyonrails.org/configuring.html#initializers
16
- #
17
- # For detail see Pull Request:
18
- # https://github.com/fnando/i18n-js/pull/371
19
- initializer "i18n-js.register_preprocessor", after: :engines_blank_point, before: :finisher_hook do
20
- next unless JS::Dependencies.using_asset_pipeline?
21
- next unless JS::Dependencies.sprockets_supports_register_preprocessor?
22
-
23
- # From README of 2.x & 3.x of `sprockets-rails`
24
- # It seems the `configure` block is preferred way to call `register_preprocessor`
25
- # Not sure if this will break older versions of rails
12
+ if JS::Dependencies.sprockets_supports_register_preprocessor?
13
+ # constant `Sprockets` should be available here after
14
+ # `.sprockets_supports_register_preprocessor?` called
15
+ sprockets_version = Gem::Version.new(Sprockets::VERSION).release
16
+ v2_only = Gem::Dependency.new("", " ~> 2")
17
+ v3_plus = Gem::Dependency.new("", " >= 3")
18
+
19
+ # See https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors
20
+ # for reference of supporting multiple versions
21
+
22
+ # `sprockets.environment` was used for 1.x of `sprockets-rails`
23
+ # https://github.com/rails/sprockets-rails/issues/227
24
+ #
25
+ # References for current values:
26
+ #
27
+ # Here is where sprockets are attached with Rails. There is no 'sprockets.environment' mentioned.
28
+ # https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb
26
29
  #
27
- # https://github.com/rails/sprockets-rails/blob/v2.3.3/README.md
28
- # https://github.com/rails/sprockets-rails/blob/v3.0.0/README.md
29
- Rails.application.config.assets.configure do |config|
30
- config.register_preprocessor "application/javascript", :"i18n-js_dependencies" do |context, source|
31
- if context.logical_path == "i18n/filtered"
32
- ::I18n.load_path.each {|path| context.depend_on(File.expand_path(path))}
33
- end
34
- source
30
+ # Finisher hook is the place which should be used as border.
31
+ # http://guides.rubyonrails.org/configuring.html#initializers
32
+ #
33
+ # For detail see Pull Request:
34
+ # https://github.com/fnando/i18n-js/pull/371
35
+ initializer_args = case sprockets_version
36
+ when -> (v) { v2_only.match?("", v) || v3_plus.match?("", v) }
37
+ { after: :engines_blank_point, before: :finisher_hook }
38
+ else
39
+ raise StandardError, "Sprockets version #{sprockets_version} is not supported"
40
+ end
41
+
42
+ initializer "i18n-js.register_preprocessor", initializer_args do
43
+ # This must be called inside initializer block
44
+ # For details see comments for `using_asset_pipeline?`
45
+ next unless JS::Dependencies.using_asset_pipeline?
46
+
47
+ # From README of 2.x & 3.x of `sprockets-rails`
48
+ # It seems the `configure` block is preferred way to call `register_preprocessor`
49
+ # Not sure if this will break older versions of rails
50
+ #
51
+ # https://github.com/rails/sprockets-rails/blob/v2.3.3/README.md
52
+ # https://github.com/rails/sprockets-rails/blob/v3.0.0/README.md
53
+ Rails.application.config.assets.configure do |config|
54
+ config.register_preprocessor(
55
+ "application/javascript",
56
+ ::I18n::JS::SprocketsExtension,
57
+ )
35
58
  end
36
59
  end
37
60
  end
38
61
  end
62
+
63
+ # @api private
64
+ class SprocketsExtension
65
+ def initialize(filename, &block)
66
+ @filename = filename
67
+ @source = block.call
68
+ end
69
+
70
+ def render(context, empty_hash_wtf)
71
+ self.class.run(@filename, @source, context)
72
+ end
73
+
74
+ def self.run(filename, source, context)
75
+ if context.logical_path == "i18n/filtered"
76
+ ::I18n.load_path.each { |path| context.depend_on(File.expand_path(path)) }
77
+ end
78
+
79
+ source
80
+ end
81
+
82
+ def self.call(input)
83
+ filename = input[:filename]
84
+ source = input[:data]
85
+ context = input[:environment].context_class.new(input)
86
+
87
+ result = run(filename, source, context)
88
+ context.metadata.merge(data: result)
89
+ end
90
+ end
39
91
  end
40
92
  end
@@ -4,7 +4,7 @@ module I18n
4
4
  MAJOR = 3
5
5
  MINOR = 0
6
6
  PATCH = 0
7
- STRING = "#{MAJOR}.#{MINOR}.#{PATCH}.rc13"
7
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}.rc14"
8
8
  end
9
9
  end
10
10
  end
@@ -38,4 +38,25 @@ describe("Extend", function () {
38
38
 
39
39
  expect(I18n.extend(obj1,obj2)).toEqual(expected);
40
40
  });
41
+
42
+ it("should merge deeply from obj1 with the same key of obj2", function() {
43
+ var obj1 = {
44
+ test1: {
45
+ test2: "abc"
46
+ }
47
+ }
48
+ , obj2 = {
49
+ test1: {
50
+ test3: "xyz"
51
+ }
52
+ }
53
+ , expected = {
54
+ test1: {
55
+ test2: "abc"
56
+ , test3: "xyz"
57
+ }
58
+ }
59
+
60
+ expect(I18n.extend(obj1, obj2)).toEqual(expected);
61
+ })
41
62
  });
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-js
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.rc13
4
+ version: 3.0.0.rc14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-29 00:00:00.000000000 Z
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -208,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
208
  version: 1.3.1
209
209
  requirements: []
210
210
  rubyforge_project:
211
- rubygems_version: 2.6.4
211
+ rubygems_version: 2.6.6
212
212
  signing_key:
213
213
  specification_version: 4
214
214
  summary: It's a small library to provide the Rails I18n translations on the Javascript.