i18n-js 3.6.0 → 3.8.2

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
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
@@ -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
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module I18n
4
4
  module JS
5
- VERSION = "3.6.0"
5
+ VERSION = "3.8.2"
6
6
  end
7
7
  end
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",
@@ -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: ","
data/spec/spec_helper.rb CHANGED
@@ -64,6 +64,7 @@ RSpec.configure do |config|
64
64
 
65
65
  config.after do
66
66
  FileUtils.rm_rf(temp_path)
67
+ I18n::JS::Private::ConfigStore.instance.flush_cache
67
68
  end
68
69
 
69
70
  config.include Helpers
data/yarn.lock CHANGED
@@ -91,8 +91,8 @@ jasmine-reporters@~1.0.0:
91
91
  mkdirp "~0.3.5"
92
92
 
93
93
  lodash@~4.17.10:
94
- version "4.17.10"
95
- resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
94
+ version "4.17.20"
95
+ resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
96
96
 
97
97
  minimatch@^3.0.4, minimatch@~3.0.2:
98
98
  version "3.0.4"
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.6.0
4
+ version: 3.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-14 00:00:00.000000000 Z
11
+ date: 2021-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.0'
33
+ version: '2.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.0'
40
+ version: '2.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -102,9 +102,10 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - ".editorconfig"
105
+ - ".github/FUNDING.yml"
106
+ - ".github/workflows/tests.yaml"
105
107
  - ".gitignore"
106
108
  - ".npmignore"
107
- - ".travis.yml"
108
109
  - Appraisals
109
110
  - CHANGELOG.md
110
111
  - Gemfile
@@ -128,6 +129,7 @@ files:
128
129
  - gemfiles/i18n_1_7.gemfile
129
130
  - gemfiles/i18n_1_8.gemfile
130
131
  - i18n-js.gemspec
132
+ - i18njs.png
131
133
  - lib/i18n-js.rb
132
134
  - lib/i18n/js.rb
133
135
  - lib/i18n/js/dependencies.rb
@@ -137,6 +139,7 @@ files:
137
139
  - lib/i18n/js/formatters/js.rb
138
140
  - lib/i18n/js/formatters/json.rb
139
141
  - lib/i18n/js/middleware.rb
142
+ - lib/i18n/js/private/config_store.rb
140
143
  - lib/i18n/js/private/hash_with_symbol_keys.rb
141
144
  - lib/i18n/js/segment.rb
142
145
  - lib/i18n/js/utils.rb
@@ -208,7 +211,7 @@ homepage: http://rubygems.org/gems/i18n-js
208
211
  licenses:
209
212
  - MIT
210
213
  metadata: {}
211
- post_install_message:
214
+ post_install_message:
212
215
  rdoc_options: []
213
216
  require_paths:
214
217
  - lib
@@ -223,8 +226,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
226
  - !ruby/object:Gem::Version
224
227
  version: '0'
225
228
  requirements: []
226
- rubygems_version: 3.1.1
227
- signing_key:
229
+ rubygems_version: 3.2.14
230
+ signing_key:
228
231
  specification_version: 4
229
232
  summary: It's a small library to provide the Rails I18n translations on the Javascript.
230
233
  test_files: