i18n-js 3.6.0 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -402,7 +402,7 @@
402
402
 
403
403
  while (locales.length) {
404
404
  locale = locales.shift();
405
- scopes = fullScope.split(this.defaultSeparator);
405
+ scopes = fullScope.split(options.separator || this.defaultSeparator);
406
406
  translations = this.translations[locale];
407
407
 
408
408
  if (!translations) {
@@ -459,7 +459,7 @@
459
459
 
460
460
  while (locales.length) {
461
461
  locale = locales.shift();
462
- scopes = scope.split(this.defaultSeparator);
462
+ scopes = scope.split(options.separator || this.defaultSeparator);
463
463
  translations = this.translations[locale];
464
464
 
465
465
  if (!translations) {
@@ -679,13 +679,13 @@
679
679
  var s = scope.split('.').slice(-1)[0];
680
680
  //replace underscore with space && camelcase with space and lowercase letter
681
681
  return (this.missingTranslationPrefix.length > 0 ? this.missingTranslationPrefix : '') +
682
- s.replace('_',' ').replace(/([a-z])([A-Z])/g,
682
+ s.replace(/_/g,' ').replace(/([a-z])([A-Z])/g,
683
683
  function(match, p1, p2) {return p1 + ' ' + p2.toLowerCase()} );
684
684
  }
685
685
 
686
686
  var localeForTranslation = (options != null && options.locale != null) ? options.locale : this.currentLocale();
687
687
  var fullScope = this.getFullScope(scope, options);
688
- var fullScopeWithLocale = [localeForTranslation, fullScope].join(this.defaultSeparator);
688
+ var fullScopeWithLocale = [localeForTranslation, fullScope].join(options.separator || this.defaultSeparator);
689
689
 
690
690
  return '[missing "' + fullScopeWithLocale + '" translation]';
691
691
  };
@@ -779,8 +779,8 @@
779
779
  I18n.toCurrency = function(number, options) {
780
780
  options = this.prepareOptions(
781
781
  options
782
- , this.lookup("number.currency.format")
783
- , this.lookup("number.format")
782
+ , this.lookup("number.currency.format", options)
783
+ , this.lookup("number.format", options)
784
784
  , CURRENCY_FORMAT
785
785
  );
786
786
 
@@ -799,17 +799,17 @@
799
799
 
800
800
  switch (scope) {
801
801
  case "currency":
802
- return this.toCurrency(value);
802
+ return this.toCurrency(value, options);
803
803
  case "number":
804
- scope = this.lookup("number.format");
804
+ scope = this.lookup("number.format", options);
805
805
  return this.toNumber(value, scope);
806
806
  case "percentage":
807
- return this.toPercentage(value);
807
+ return this.toPercentage(value, options);
808
808
  default:
809
809
  var localizedValue;
810
810
 
811
811
  if (scope.match(/^(date|time)/)) {
812
- localizedValue = this.toTime(scope, value);
812
+ localizedValue = this.toTime(scope, value, options);
813
813
  } else {
814
814
  localizedValue = value.toString();
815
815
  }
@@ -914,8 +914,8 @@
914
914
  // %Y - Year with century
915
915
  // %z/%Z - Timezone offset (+0545)
916
916
  //
917
- I18n.strftime = function(date, format) {
918
- var options = this.lookup("date")
917
+ I18n.strftime = function(date, format, options) {
918
+ var options = this.lookup("date", options)
919
919
  , meridianOptions = I18n.meridian()
920
920
  ;
921
921
 
@@ -984,9 +984,9 @@
984
984
  };
985
985
 
986
986
  // Convert the given dateString into a formatted date.
987
- I18n.toTime = function(scope, dateString) {
987
+ I18n.toTime = function(scope, dateString, options) {
988
988
  var date = this.parseDate(dateString)
989
- , format = this.lookup(scope)
989
+ , format = this.lookup(scope, options)
990
990
  ;
991
991
 
992
992
  // A date input of `null` or `undefined` will be returned as-is
@@ -1003,15 +1003,15 @@
1003
1003
  return date_string;
1004
1004
  }
1005
1005
 
1006
- return this.strftime(date, format);
1006
+ return this.strftime(date, format, options);
1007
1007
  };
1008
1008
 
1009
1009
  // Convert a number into a formatted percentage value.
1010
1010
  I18n.toPercentage = function(number, options) {
1011
1011
  options = this.prepareOptions(
1012
1012
  options
1013
- , this.lookup("number.percentage.format")
1014
- , this.lookup("number.format")
1013
+ , this.lookup("number.percentage.format", options)
1014
+ , this.lookup("number.format", options)
1015
1015
  , PERCENTAGE_FORMAT
1016
1016
  );
1017
1017
 
@@ -1025,6 +1025,7 @@
1025
1025
  , iterations = 0
1026
1026
  , unit
1027
1027
  , precision
1028
+ , fullScope
1028
1029
  ;
1029
1030
 
1030
1031
  while (size >= kb && iterations < 4) {
@@ -1033,10 +1034,12 @@
1033
1034
  }
1034
1035
 
1035
1036
  if (iterations === 0) {
1036
- unit = this.t("number.human.storage_units.units.byte", {count: size});
1037
+ fullScope = this.getFullScope("number.human.storage_units.units.byte", options);
1038
+ unit = this.t(fullScope, {count: size});
1037
1039
  precision = 0;
1038
1040
  } else {
1039
- unit = this.t("number.human.storage_units.units." + SIZE_UNITS[iterations]);
1041
+ fullScope = this.getFullScope("number.human.storage_units.units." + SIZE_UNITS[iterations], options);
1042
+ unit = this.t(fullScope);
1040
1043
  precision = (size - Math.floor(size) === 0) ? 0 : 1;
1041
1044
  }
1042
1045
 
@@ -1053,7 +1056,7 @@
1053
1056
 
1054
1057
  // Deal with the scope as an array.
1055
1058
  if (isArray(scope)) {
1056
- scope = scope.join(this.defaultSeparator);
1059
+ scope = scope.join(options.separator || this.defaultSeparator);
1057
1060
  }
1058
1061
 
1059
1062
  // Deal with the scope option provided through the second argument.
@@ -1061,7 +1064,7 @@
1061
1064
  // I18n.t('hello', {scope: 'greetings'});
1062
1065
  //
1063
1066
  if (options.scope) {
1064
- scope = [options.scope, scope].join(this.defaultSeparator);
1067
+ scope = [options.scope, scope].join(options.separator || this.defaultSeparator);
1065
1068
  }
1066
1069
 
1067
1070
  return scope;
data/i18n-js.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_dependency "i18n", ">= 0.6.6"
22
22
 
23
- s.add_development_dependency "appraisal", "~> 2.0"
23
+ s.add_development_dependency "appraisal", "~> 2.3"
24
24
  s.add_development_dependency "rspec", "~> 3.0"
25
25
  s.add_development_dependency "rake", "~> 12.0"
26
26
  s.add_development_dependency "gem-release", ">= 0.7"
data/i18njs.png ADDED
Binary file
@@ -4,6 +4,12 @@ module I18n
4
4
  module JS
5
5
  module Formatters
6
6
  class JS < Base
7
+ JSON_ESCAPE_MAP = {
8
+ "'" => "\\'",
9
+ "\\" => "\\\\"
10
+ }.freeze
11
+ private_constant :JSON_ESCAPE_MAP
12
+
7
13
  def format(translations)
8
14
  contents = header
9
15
  translations.each do |locale, translations_for_locale|
@@ -20,10 +26,11 @@ module I18n
20
26
  end
21
27
 
22
28
  def line(locale, translations)
29
+ json_literal = @pretty_print ? translations : %(JSON.parse('#{translations.gsub(/#{Regexp.union(JSON_ESCAPE_MAP.keys)}/){|match| JSON_ESCAPE_MAP.fetch(match) }}'))
23
30
  if @js_extend
24
- %(#{@namespace}.translations["#{locale}"] = I18n.extend((#{@namespace}.translations["#{locale}"] || {}), #{translations});\n)
31
+ %(#{@namespace}.translations["#{locale}"] = I18n.extend((#{@namespace}.translations["#{locale}"] || {}), #{json_literal});\n)
25
32
  else
26
- %(#{@namespace}.translations["#{locale}"] = #{translations};\n)
33
+ %(#{@namespace}.translations["#{locale}"] = #{json_literal};\n)
27
34
  end
28
35
  end
29
36
  end
@@ -45,7 +45,7 @@ module I18n
45
45
 
46
46
  def save_cache(new_cache)
47
47
  # path could be a symbolic link
48
- FileUtils.mkdir_p(cache_dir) unless File.exists?(cache_dir)
48
+ FileUtils.mkdir_p(cache_dir) unless File.exist?(cache_dir)
49
49
  File.open(cache_path, "w+") do |file|
50
50
  file << new_cache.to_yaml
51
51
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module I18n
6
+ module JS
7
+ # @api private
8
+ module Private
9
+ # Caching implementation for I18n::JS.config
10
+ #
11
+ # @api private
12
+ class ConfigStore
13
+ include Singleton
14
+
15
+ def fetch
16
+ return @config if @config
17
+
18
+ yield.tap do |obj|
19
+ raise ArgumentError, "unexpected falsy object from block" unless obj
20
+
21
+ @config = obj
22
+ end
23
+ end
24
+
25
+ def flush_cache
26
+ @config = nil
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -33,7 +33,7 @@ module I18n
33
33
  # Saves JSON file containing translations
34
34
  def save!
35
35
  if @file =~ LOCALE_INTERPOLATOR
36
- I18n.available_locales.each do |locale|
36
+ I18n::JS.js_available_locales.each do |locale|
37
37
  write_file(file_for_locale(locale), @translations.slice(locale))
38
38
  end
39
39
  else
@@ -50,6 +50,7 @@ module I18n
50
50
  def write_file(_file = @file, _translations = @translations)
51
51
  FileUtils.mkdir_p File.dirname(_file)
52
52
  _translations = Utils.deep_key_sort(_translations) if @sort_translation_keys
53
+ _translations = Utils.deep_remove_procs(_translations)
53
54
  contents = formatter.format(_translations)
54
55
 
55
56
  return if File.exist?(_file) && File.read(_file) == contents
data/lib/i18n/js/utils.rb CHANGED
@@ -73,6 +73,19 @@ module I18n
73
73
  seed[key] = value.is_a?(Hash) ? deep_key_sort(value) : value
74
74
  end
75
75
  end
76
+
77
+ def self.deep_remove_procs(hash)
78
+ # procs exist in `i18n.plural.rule` as pluralizer
79
+ # But having it in translation causes the exported JS/JSON changes every time
80
+ # https://github.com/ruby-i18n/i18n/blob/v1.8.7/lib/i18n/backend/pluralization.rb#L51
81
+ hash.keys.
82
+ each_with_object({}) do |key, seed|
83
+ value = hash[key]
84
+ next if value.is_a?(Proc)
85
+
86
+ seed[key] = value.is_a?(Hash) ? deep_remove_procs(value) : value
87
+ end
88
+ end
76
89
  end
77
90
  end
78
91
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module I18n
4
4
  module JS
5
- VERSION = "3.6.0"
5
+ VERSION = "3.9.0"
6
6
  end
7
7
  end
data/lib/i18n/js.rb CHANGED
@@ -4,6 +4,7 @@ require "i18n"
4
4
 
5
5
  require "i18n/js/utils"
6
6
  require "i18n/js/private/hash_with_symbol_keys"
7
+ require "i18n/js/private/config_store"
7
8
 
8
9
  module I18n
9
10
  module JS
@@ -26,6 +27,8 @@ module I18n
26
27
 
27
28
  def self.config_file_path=(new_path)
28
29
  @config_file_path = new_path
30
+ # new config file path = need to re-read config from new file
31
+ Private::ConfigStore.instance.flush_cache
29
32
  end
30
33
 
31
34
  # Allow using a different backend than the one globally configured
@@ -78,7 +81,7 @@ module I18n
78
81
 
79
82
  # deep_merge! given result with result for fallback locale
80
83
  def self.merge_with_fallbacks!(result)
81
- I18n.available_locales.each do |locale|
84
+ js_available_locales.each do |locale|
82
85
  fallback_locales = FallbackLocales.new(fallbacks, locale)
83
86
  fallback_locales.each do |fallback_locale|
84
87
  # `result[fallback_locale]` could be missing
@@ -108,14 +111,16 @@ module I18n
108
111
  # Load configuration file for partial exporting and
109
112
  # custom output directory
110
113
  def self.config
111
- if config_file_exists?
112
- erb_result_from_yaml_file = ERB.new(File.read(config_file_path)).result
113
- Private::HashWithSymbolKeys.new(
114
- (::YAML.load(erb_result_from_yaml_file) || {})
115
- )
116
- else
117
- Private::HashWithSymbolKeys.new({})
118
- end.freeze
114
+ Private::ConfigStore.instance.fetch do
115
+ if config_file_exists?
116
+ erb_result_from_yaml_file = ERB.new(File.read(config_file_path)).result
117
+ Private::HashWithSymbolKeys.new(
118
+ (::YAML.load(erb_result_from_yaml_file) || {})
119
+ )
120
+ else
121
+ Private::HashWithSymbolKeys.new({})
122
+ end.freeze
123
+ end
119
124
  end
120
125
 
121
126
  # @api private
@@ -178,7 +183,7 @@ module I18n
178
183
  #
179
184
  # So the input is wrapped by our class for better `#slice`
180
185
  Private::HashWithSymbolKeys.new(translations).
181
- slice(*::I18n.available_locales).
186
+ slice(*::I18n::JS.js_available_locales).
182
187
  to_h
183
188
  end
184
189
  end
@@ -208,6 +213,16 @@ module I18n
208
213
  end
209
214
  end
210
215
 
216
+ # Get all available locales.
217
+ #
218
+ # @return [Array<Symbol>] the locales.
219
+ def self.js_available_locales
220
+ config.fetch(:js_available_locales) do
221
+ # default value
222
+ I18n.available_locales
223
+ end.map(&:to_sym)
224
+ end
225
+
211
226
  def self.sort_translation_keys?
212
227
  @sort_translation_keys ||= (config[:sort_translation_keys]) if config.key?(:sort_translation_keys)
213
228
  @sort_translation_keys = true if @sort_translation_keys.nil?
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18n-js",
3
- "version": "3.5.1",
3
+ "version": "3.8.0",
4
4
  "description": "A javascript library similar to Ruby on Rails i18n gem",
5
5
  "author": "Nando Vieira",
6
6
  "license": "MIT",
@@ -8,7 +8,7 @@
8
8
  "i18n"
9
9
  ],
10
10
  "devDependencies": {
11
- "jasmine-node": "^1.14.5"
11
+ "jasmine-node": "^3.0.0"
12
12
  },
13
13
  "main": "app/assets/javascripts/i18n.js",
14
14
  "scripts": {
@@ -0,0 +1 @@
1
+ js_available_locales: ["en", "foo"]
@@ -0,0 +1,14 @@
1
+ describe("JSON.parse", function () {
2
+ it('should parse', function () {
3
+ expect(JSON.parse('{"a":"Test\'s"}')).toEqual({
4
+ a: "Test's"
5
+ })
6
+
7
+ expect(JSON.parse('{"a":"say \\"hello\\""}')).toEqual({
8
+ a: 'say "hello"'
9
+ });
10
+ expect(JSON.parse('{"double-backslash-in-double-quote":"\\"\\\\\\\\\\""}')).toEqual({
11
+ 'double-backslash-in-double-quote': '"\\\\"'
12
+ });
13
+ })
14
+ })
@@ -36,6 +36,20 @@ describe("Localization", function(){
36
36
  expect(I18n.localize("date.formats.long", "2009-01-07")).toEqual("07 de Janeiro de 2009");
37
37
  });
38
38
 
39
+ it("localizes strings with locale from options", function(){
40
+ I18n.locale = "en";
41
+
42
+ expect(I18n.localize("date.formats.default", "2009-11-29", { locale: "pt-BR" })).toEqual("29/11/2009");
43
+ expect(I18n.localize("date.formats.short", "2009-01-07", { locale: "pt-BR" })).toEqual("07 de Janeiro");
44
+ expect(I18n.localize("date.formats.long", "2009-01-07", { locale: "pt-BR" })).toEqual("07 de Janeiro de 2009");
45
+ expect(I18n.localize("time.formats.default", "2009-11-29 15:07:59", { locale: "pt-BR" })).toEqual("Domingo, 29 de Novembro de 2009, 15:07 h");
46
+ expect(I18n.localize("time.formats.short", "2009-01-07 09:12:35", { locale: "pt-BR" })).toEqual("07/01, 09:12 h");
47
+ expect(I18n.localize("time.formats.long", "2009-11-29 15:07:59", { locale: "pt-BR" })).toEqual("Domingo, 29 de Novembro de 2009, 15:07 h");
48
+ expect(I18n.localize("number", 1234567, { locale: "pt-BR" })).toEqual("1,234,567.000");
49
+ expect(I18n.localize("currency", 1234567, { locale: "pt-BR" })).toEqual("R$ 1.234.567,00");
50
+ expect(I18n.localize("percentage", 123.45, { locale: "pt-BR" })).toEqual("123,45%");
51
+ });
52
+
39
53
  it("localizes time strings", function(){
40
54
  I18n.locale = "pt-BR";
41
55
 
@@ -150,6 +150,10 @@ describe("Numbers", function(){
150
150
  expect(I18n.toHumanSize(1024 * 1.6, {precision: 0})).toEqual("2KB");
151
151
  });
152
152
 
153
+ it("returns number as human size using custom scope", function(){
154
+ expect(I18n.toHumanSize(1024 * 1024, {scope: "extended"})).toEqual("1Megabyte");
155
+ });
156
+
153
157
  it("formats numbers with strip insignificant zero", function() {
154
158
  options = {separator: ".", delimiter: ",", strip_insignificant_zeros: true};
155
159
 
@@ -48,9 +48,14 @@ describe("Translate", function(){
48
48
 
49
49
  it("returns guessed translation if missingBehaviour is set to guess", function(){
50
50
  I18n.missingBehaviour = 'guess'
51
- actual = I18n.translate("invalid.thisIsAutomaticallyGeneratedTranslation");
52
- expected = 'this is automatically generated translation';
53
- expect(actual).toEqual(expected);
51
+
52
+ var actual_1 = I18n.translate("invalid.thisIsAutomaticallyGeneratedTranslation");
53
+ var expected_1 = 'this is automatically generated translation';
54
+ expect(actual_1).toEqual(expected_1);
55
+
56
+ var actual_2 = I18n.translate("invalid.this_is_automatically_generated_translation");
57
+ var expected_2 = 'this is automatically generated translation';
58
+ expect(actual_2).toEqual(expected_2);
54
59
  });
55
60
 
56
61
  it("returns guessed translation with prefix if missingBehaviour is set to guess and prefix is also provided", function(){
@@ -291,4 +296,9 @@ describe("Translate", function(){
291
296
  {foo: "bar"}
292
297
  ]);
293
298
  });
299
+
300
+
301
+ it("returns value with key containing dot but different separator specified", function() {
302
+ expect(I18n.t(["A implies B means something."], {scope: "sentences_with_dots", separator: "|"})).toEqual("A implies B means that when A is true, B must be true.");
303
+ });
294
304
  });
@@ -58,6 +58,18 @@
58
58
  }
59
59
  }
60
60
 
61
+ , extended: {
62
+ number: {
63
+ human: {
64
+ storage_units: {
65
+ units: {
66
+ "mb": "Megabyte"
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
72
+
61
73
  , arrayWithParams: [
62
74
  null,
63
75
  "An item with a param of {{value}}",
@@ -67,7 +79,11 @@
67
79
  {foo: "bar"}
68
80
  ]
69
81
 
70
- , null_key: null
82
+ , null_key: null,
83
+
84
+ sentences_with_dots: {
85
+ "A implies B means something.": "A implies B means that when A is true, B must be true."
86
+ }
71
87
  };
72
88
 
73
89
  Translations["en-US"] = {
@@ -90,7 +106,16 @@
90
106
  hello: "Olá Mundo!"
91
107
 
92
108
  , number: {
93
- percentage: {
109
+ currency: {
110
+ format: {
111
+ delimiter: ".",
112
+ format: "%u %n",
113
+ precision: 2,
114
+ separator: ",",
115
+ unit: "R$"
116
+ }
117
+ }
118
+ , percentage: {
94
119
  format: {
95
120
  delimiter: ""
96
121
  , separator: ","
@@ -131,8 +131,8 @@ EOS
131
131
 
132
132
  expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF
133
133
  MyNamespace.translations || (MyNamespace.translations = {});
134
- MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"test":"Test"});
135
- MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), {"test":"Test2"});
134
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"test":"Test"}'));
135
+ MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), JSON.parse('{"test":"Test2"}'));
136
136
  EOF
137
137
  end
138
138
  end
@@ -146,12 +146,54 @@ MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] ||
146
146
 
147
147
  expect(File.open(File.join(temp_path, "en.js")){|f| f.read}).to eql <<-EOF
148
148
  MyNamespace.translations || (MyNamespace.translations = {});
149
- MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"test":"Test"});
149
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"test":"Test"}'));
150
150
  EOF
151
151
 
152
152
  expect(File.open(File.join(temp_path, "fr.js")){|f| f.read}).to eql <<-EOF
153
153
  MyNamespace.translations || (MyNamespace.translations = {});
154
- MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), {"test":"Test2"});
154
+ MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), JSON.parse('{"test":"Test2"}'));
155
+ EOF
156
+ end
157
+ end
158
+
159
+ context "when file includes single quote" do
160
+ let(:file){ "tmp/i18n-js/%{locale}.js" }
161
+ let(:translations){ { en: { "a" => "Test's" } } }
162
+
163
+ it "should write files" do
164
+ file_should_exist "en.js"
165
+
166
+ expect(File.open(File.join(temp_path, "en.js")){|f| f.read}).to eql <<-EOF
167
+ MyNamespace.translations || (MyNamespace.translations = {});
168
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"a":"Test\\'s"}'));
169
+ EOF
170
+ end
171
+ end
172
+
173
+ context "when file includes escaped double quote" do
174
+ let(:file){ "tmp/i18n-js/%{locale}.js" }
175
+ let(:translations){ { en: { "a" => 'say "hello"' } } }
176
+
177
+ it "should escape double quote" do
178
+ file_should_exist "en.js"
179
+
180
+ expect(File.open(File.join(temp_path, "en.js")){|f| f.read}).to eql <<-EOF
181
+ MyNamespace.translations || (MyNamespace.translations = {});
182
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"a":"say \\\\"hello\\\\""}'));
183
+ EOF
184
+ end
185
+ end
186
+
187
+ context "when file includes backslash in double quote" do
188
+ let(:file){ "tmp/i18n-js/%{locale}.js" }
189
+ let(:translations){ { en: { "double-backslash-in-double-quote" => '"\\\\"' } } }
190
+
191
+ it "should escape backslash" do
192
+ file_should_exist "en.js"
193
+
194
+ expect(File.open(File.join(temp_path, "en.js")){|f| f.read}).to eql <<-EOF
195
+ MyNamespace.translations || (MyNamespace.translations = {});
196
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"double-backslash-in-double-quote":"\\\\"\\\\\\\\\\\\\\\\\\\\""}'));
155
197
  EOF
156
198
  end
157
199
  end
@@ -166,7 +208,7 @@ MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] ||
166
208
 
167
209
  expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF
168
210
  MyNamespace.translations || (MyNamespace.translations = {});
169
- MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"a":"Test","b":"Test"});
211
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"a":"Test","b":"Test"}'));
170
212
  EOF
171
213
  end
172
214
  end
@@ -181,7 +223,7 @@ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] ||
181
223
 
182
224
  expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF
183
225
  MyNamespace.translations || (MyNamespace.translations = {});
184
- MyNamespace.translations["en"] = {"a":"Test","b":"Test"};
226
+ MyNamespace.translations["en"] = JSON.parse('{"a":"Test","b":"Test"}');
185
227
  EOF
186
228
  end
187
229
  end
@@ -196,7 +238,7 @@ MyNamespace.translations["en"] = {"a":"Test","b":"Test"};
196
238
 
197
239
  expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF
198
240
  MyNamespace.translations || (MyNamespace.translations = {});
199
- MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"a":"Test","b":"Test"});
241
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"a":"Test","b":"Test"}'));
200
242
  EOF
201
243
  end
202
244
  end
@@ -211,7 +253,32 @@ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] ||
211
253
 
212
254
  expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF
213
255
  MyNamespace.translations || (MyNamespace.translations = {});
214
- MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), {"b":"Test","a":"Test"});
256
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"b":"Test","a":"Test"}'));
257
+ EOF
258
+ end
259
+ end
260
+
261
+ context "when translation entries contain procs" do
262
+ let(:translations) do
263
+ {
264
+ en: {
265
+ "test" => "Test",
266
+ "i18n" => {"plural" => {"rule" => proc {} }},
267
+ },
268
+ fr: {
269
+ "test" => "Test2",
270
+ "i18n" => {"plural" => {"rule" => proc {} }},
271
+ },
272
+ }
273
+ end
274
+
275
+ it "should write files without procs or their string representations" do
276
+ file_should_exist "segment.js"
277
+
278
+ expect(File.open(File.join(temp_path, "segment.js")){|f| f.read}).to eql <<-EOF
279
+ MyNamespace.translations || (MyNamespace.translations = {});
280
+ MyNamespace.translations["en"] = I18n.extend((MyNamespace.translations["en"] || {}), JSON.parse('{"i18n":{"plural":{}},"test":"Test"}'));
281
+ MyNamespace.translations["fr"] = I18n.extend((MyNamespace.translations["fr"] || {}), JSON.parse('{"i18n":{"plural":{}},"test":"Test2"}'));
215
282
  EOF
216
283
  end
217
284
  end
@@ -103,4 +103,36 @@ describe I18n::JS::Utils do
103
103
  expect(described_class.scopes_match?([:a, :b, :c], [:a, '*', '*'])).to eql true
104
104
  end
105
105
  end
106
+
107
+ describe ".deep_remove_procs" do
108
+ let(:proc_obj) { proc {} }
109
+ let(:hash_with_proc) do
110
+ {
111
+ :a => :b,
112
+ :c => proc_obj,
113
+ :d => {
114
+ :e => proc_obj,
115
+ :f => :g,
116
+ }
117
+ }
118
+ end
119
+ subject { described_class.deep_remove_procs(hash_with_proc) }
120
+
121
+ it "performs a deep keys sort without changing the original hash" do
122
+ should eql({
123
+ :a => :b,
124
+ :d => {
125
+ :f => :g,
126
+ }
127
+ })
128
+ expect(hash_with_proc).to eql({
129
+ :a => :b,
130
+ :c => proc_obj,
131
+ :d => {
132
+ :e => proc_obj,
133
+ :f => :g,
134
+ }
135
+ })
136
+ end
137
+ end
106
138
  end