i18n 0.9.5 → 1.8.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +70 -32
  3. data/lib/i18n.rb +95 -36
  4. data/lib/i18n/backend.rb +3 -0
  5. data/lib/i18n/backend/base.rb +53 -23
  6. data/lib/i18n/backend/cache.rb +10 -11
  7. data/lib/i18n/backend/cache_file.rb +36 -0
  8. data/lib/i18n/backend/cascade.rb +3 -1
  9. data/lib/i18n/backend/chain.rb +38 -5
  10. data/lib/i18n/backend/fallbacks.rb +25 -14
  11. data/lib/i18n/backend/flatten.rb +10 -5
  12. data/lib/i18n/backend/gettext.rb +4 -0
  13. data/lib/i18n/backend/interpolation_compiler.rb +3 -1
  14. data/lib/i18n/backend/key_value.rb +31 -2
  15. data/lib/i18n/backend/memoize.rb +10 -2
  16. data/lib/i18n/backend/metadata.rb +5 -3
  17. data/lib/i18n/backend/pluralization.rb +3 -1
  18. data/lib/i18n/backend/simple.rb +26 -10
  19. data/lib/i18n/backend/transliterator.rb +2 -0
  20. data/lib/i18n/config.rb +20 -2
  21. data/lib/i18n/core_ext/hash.rb +54 -24
  22. data/lib/i18n/exceptions.rb +23 -16
  23. data/lib/i18n/gettext.rb +2 -0
  24. data/lib/i18n/gettext/helpers.rb +4 -2
  25. data/lib/i18n/gettext/po_parser.rb +7 -7
  26. data/lib/i18n/interpolate/ruby.rb +6 -4
  27. data/lib/i18n/locale.rb +2 -0
  28. data/lib/i18n/locale/fallbacks.rb +9 -6
  29. data/lib/i18n/locale/tag/parents.rb +8 -6
  30. data/lib/i18n/locale/tag/simple.rb +1 -1
  31. data/lib/i18n/middleware.rb +2 -0
  32. data/lib/i18n/tests.rb +2 -0
  33. data/lib/i18n/tests/interpolation.rb +9 -4
  34. data/lib/i18n/tests/link.rb +11 -1
  35. data/lib/i18n/tests/localization/date.rb +29 -7
  36. data/lib/i18n/tests/localization/date_time.rb +27 -6
  37. data/lib/i18n/tests/localization/procs.rb +6 -5
  38. data/lib/i18n/tests/localization/time.rb +26 -4
  39. data/lib/i18n/tests/lookup.rb +2 -2
  40. data/lib/i18n/tests/procs.rb +5 -0
  41. data/lib/i18n/version.rb +3 -1
  42. metadata +14 -60
  43. data/gemfiles/Gemfile.rails-3.2.x +0 -10
  44. data/gemfiles/Gemfile.rails-4.0.x +0 -10
  45. data/gemfiles/Gemfile.rails-4.1.x +0 -10
  46. data/gemfiles/Gemfile.rails-4.2.x +0 -10
  47. data/gemfiles/Gemfile.rails-5.0.x +0 -10
  48. data/gemfiles/Gemfile.rails-5.1.x +0 -10
  49. data/gemfiles/Gemfile.rails-master +0 -10
  50. data/lib/i18n/core_ext/kernel/suppress_warnings.rb +0 -8
  51. data/lib/i18n/core_ext/string/interpolate.rb +0 -9
  52. data/test/api/all_features_test.rb +0 -58
  53. data/test/api/cascade_test.rb +0 -28
  54. data/test/api/chain_test.rb +0 -24
  55. data/test/api/fallbacks_test.rb +0 -30
  56. data/test/api/key_value_test.rb +0 -24
  57. data/test/api/memoize_test.rb +0 -56
  58. data/test/api/override_test.rb +0 -42
  59. data/test/api/pluralization_test.rb +0 -30
  60. data/test/api/simple_test.rb +0 -28
  61. data/test/backend/cache_test.rb +0 -109
  62. data/test/backend/cascade_test.rb +0 -86
  63. data/test/backend/chain_test.rb +0 -122
  64. data/test/backend/exceptions_test.rb +0 -36
  65. data/test/backend/fallbacks_test.rb +0 -219
  66. data/test/backend/interpolation_compiler_test.rb +0 -118
  67. data/test/backend/key_value_test.rb +0 -61
  68. data/test/backend/memoize_test.rb +0 -79
  69. data/test/backend/metadata_test.rb +0 -48
  70. data/test/backend/pluralization_test.rb +0 -45
  71. data/test/backend/simple_test.rb +0 -103
  72. data/test/backend/transliterator_test.rb +0 -84
  73. data/test/core_ext/hash_test.rb +0 -36
  74. data/test/gettext/api_test.rb +0 -214
  75. data/test/gettext/backend_test.rb +0 -92
  76. data/test/i18n/exceptions_test.rb +0 -117
  77. data/test/i18n/gettext_plural_keys_test.rb +0 -20
  78. data/test/i18n/interpolate_test.rb +0 -91
  79. data/test/i18n/load_path_test.rb +0 -34
  80. data/test/i18n/middleware_test.rb +0 -24
  81. data/test/i18n_test.rb +0 -462
  82. data/test/locale/fallbacks_test.rb +0 -133
  83. data/test/locale/tag/rfc4646_test.rb +0 -143
  84. data/test/locale/tag/simple_test.rb +0 -32
  85. data/test/run_all.rb +0 -20
  86. data/test/test_data/locales/de.po +0 -82
  87. data/test/test_data/locales/en.rb +0 -3
  88. data/test/test_data/locales/en.yml +0 -3
  89. data/test/test_data/locales/invalid/empty.yml +0 -0
  90. data/test/test_data/locales/invalid/syntax.yml +0 -4
  91. data/test/test_data/locales/plurals.rb +0 -113
  92. data/test/test_helper.rb +0 -61
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This module allows you to easily cache all responses from the backend - thus
2
4
  # speeding up the I18n aspects of your application quite a bit.
3
5
  #
@@ -14,12 +16,12 @@
14
16
  # ActiveSupport::Cache (only the methods #fetch and #write are being used).
15
17
  #
16
18
  # The cache_key implementation by default assumes you pass values that return
17
- # a valid key from #hash (see
18
- # http://www.ruby-doc.org/core/classes/Object.html#M000337). However, you can
19
- # configure your own digest method via which responds to #hexdigest (see
20
- # http://ruby-doc.org/stdlib/libdoc/digest/rdoc/index.html):
19
+ # a valid key from #hash (see
20
+ # https://www.ruby-doc.org/core/classes/Object.html#M000337). However, you can
21
+ # configure your own digest method via which responds to #hexdigest (see
22
+ # https://ruby-doc.org/stdlib/libdoc/openssl/rdoc/OpenSSL/Digest.html):
21
23
  #
22
- # I18n.cache_key_digest = Digest::MD5.new
24
+ # I18n.cache_key_digest = OpenSSL::Digest::SHA256.new
23
25
  #
24
26
  # If you use a lambda as a default value in your translation like this:
25
27
  #
@@ -75,7 +77,7 @@ module I18n
75
77
  module Backend
76
78
  # TODO Should the cache be cleared if new translations are stored?
77
79
  module Cache
78
- def translate(locale, key, options = {})
80
+ def translate(locale, key, options = EMPTY_HASH)
79
81
  I18n.perform_caching? ? fetch(cache_key(locale, key, options)) { super } : super
80
82
  end
81
83
 
@@ -98,16 +100,13 @@ module I18n
98
100
 
99
101
  def cache_key(locale, key, options)
100
102
  # This assumes that only simple, native Ruby values are passed to I18n.translate.
101
- "i18n/#{I18n.cache_namespace}/#{locale}/#{digest_item(key)}/#{USE_INSPECT_HASH ? digest_item(options.inspect) : digest_item(options)}"
103
+ "i18n/#{I18n.cache_namespace}/#{locale}/#{digest_item(key)}/#{digest_item(options)}"
102
104
  end
103
105
 
104
106
  private
105
- # In Ruby < 1.9 the following is true: { :foo => 1, :bar => 2 }.hash == { :foo => 2, :bar => 1 }.hash
106
- # Therefore we must use the hash of the inspect string instead to avoid cache key colisions.
107
- USE_INSPECT_HASH = RUBY_VERSION <= "1.9"
108
107
 
109
108
  def digest_item(key)
110
- I18n.cache_key_digest ? I18n.cache_key_digest.hexdigest(key.to_s) : key.hash
109
+ I18n.cache_key_digest ? I18n.cache_key_digest.hexdigest(key.to_s) : key.to_s.hash
111
110
  end
112
111
  end
113
112
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+
5
+ module I18n
6
+ module Backend
7
+ # Overwrites the Base load_file method to cache loaded file contents.
8
+ module CacheFile
9
+ # Optionally provide path_roots array to normalize filename paths,
10
+ # to make the cached i18n data portable across environments.
11
+ attr_accessor :path_roots
12
+
13
+ protected
14
+
15
+ # Track loaded translation files in the `i18n.load_file` scope,
16
+ # and skip loading the file if its contents are still up-to-date.
17
+ def load_file(filename)
18
+ initialized = !respond_to?(:initialized?) || initialized?
19
+ key = I18n::Backend::Flatten.escape_default_separator(normalized_path(filename))
20
+ old_mtime, old_digest = initialized && lookup(:i18n, key, :load_file)
21
+ return if (mtime = File.mtime(filename).to_i) == old_mtime ||
22
+ (digest = OpenSSL::Digest::SHA256.file(filename).hexdigest) == old_digest
23
+ super
24
+ store_translations(:i18n, load_file: { key => [mtime, digest] })
25
+ end
26
+
27
+ # Translate absolute filename to relative path for i18n key.
28
+ def normalized_path(file)
29
+ return file unless path_roots
30
+ path = path_roots.find(&file.method(:start_with?)) ||
31
+ raise(InvalidLocaleData.new(file, 'outside expected path roots'))
32
+ file.sub(path, path_roots.index(path).to_s)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # The Cascade module adds the ability to do cascading lookups to backends that
2
4
  # are compatible to the Simple backend.
3
5
  #
@@ -31,7 +33,7 @@
31
33
  module I18n
32
34
  module Backend
33
35
  module Cascade
34
- def lookup(locale, key, scope = [], options = {})
36
+ def lookup(locale, key, scope = [], options = EMPTY_HASH)
35
37
  return super unless cascade = options[:cascade]
36
38
 
37
39
  cascade = { :step => 1 } unless cascade.is_a?(Hash)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module I18n
2
4
  module Backend
3
5
  # Backend that chains multiple other backends and checks each of them when
@@ -15,6 +17,8 @@ module I18n
15
17
  # The implementation assumes that all backends added to the Chain implement
16
18
  # a lookup method with the same API as Simple backend does.
17
19
  class Chain
20
+ using I18n::HashRefinements
21
+
18
22
  module Implementation
19
23
  include Base
20
24
 
@@ -24,11 +28,24 @@ module I18n
24
28
  self.backends = backends
25
29
  end
26
30
 
31
+ def initialized?
32
+ backends.all? do |backend|
33
+ backend.instance_eval do
34
+ return false unless initialized?
35
+ end
36
+ end
37
+ true
38
+ end
39
+
27
40
  def reload!
28
41
  backends.each { |backend| backend.reload! }
29
42
  end
30
43
 
31
- def store_translations(locale, data, options = {})
44
+ def eager_load!
45
+ backends.each { |backend| backend.eager_load! }
46
+ end
47
+
48
+ def store_translations(locale, data, options = EMPTY_HASH)
32
49
  backends.first.store_translations(locale, data, options)
33
50
  end
34
51
 
@@ -36,7 +53,7 @@ module I18n
36
53
  backends.map { |backend| backend.available_locales }.flatten.uniq
37
54
  end
38
55
 
39
- def translate(locale, key, default_options = {})
56
+ def translate(locale, key, default_options = EMPTY_HASH)
40
57
  namespace = nil
41
58
  options = default_options.except(:default)
42
59
 
@@ -56,13 +73,13 @@ module I18n
56
73
  throw(:exception, I18n::MissingTranslation.new(locale, key, options))
57
74
  end
58
75
 
59
- def exists?(locale, key)
76
+ def exists?(locale, key, options = EMPTY_HASH)
60
77
  backends.any? do |backend|
61
- backend.exists?(locale, key)
78
+ backend.exists?(locale, key, options)
62
79
  end
63
80
  end
64
81
 
65
- def localize(locale, object, format = :default, options = {})
82
+ def localize(locale, object, format = :default, options = EMPTY_HASH)
66
83
  backends.each do |backend|
67
84
  catch(:exception) do
68
85
  result = backend.localize(locale, object, format, options) and return result
@@ -72,6 +89,22 @@ module I18n
72
89
  end
73
90
 
74
91
  protected
92
+ def init_translations
93
+ backends.each do |backend|
94
+ backend.send(:init_translations)
95
+ end
96
+ end
97
+
98
+ def translations
99
+ backends.reverse.each_with_object({}) do |backend, memo|
100
+ partial_translations = backend.instance_eval do
101
+ init_translations unless initialized?
102
+ translations
103
+ end
104
+ memo.deep_merge!(partial_translations) { |_, a, b| b || a }
105
+ end
106
+ end
107
+
75
108
  def namespace_lookup?(result, options)
76
109
  result.is_a?(Hash) && !options.has_key?(:count)
77
110
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # I18n locale fallbacks are useful when you want your application to use
2
4
  # translations from other locales when translations for the current locale are
3
5
  # missing. E.g. you might want to use :en translations when translations in
@@ -14,11 +16,13 @@ module I18n
14
16
  # Returns the current fallbacks implementation. Defaults to +I18n::Locale::Fallbacks+.
15
17
  def fallbacks
16
18
  @@fallbacks ||= I18n::Locale::Fallbacks.new
19
+ Thread.current[:i18n_fallbacks] || @@fallbacks
17
20
  end
18
21
 
19
22
  # Sets the current fallbacks implementation. Use this to set a different fallbacks implementation.
20
23
  def fallbacks=(fallbacks)
21
- @@fallbacks = fallbacks
24
+ @@fallbacks = fallbacks.is_a?(Array) ? I18n::Locale::Fallbacks.new(fallbacks) : fallbacks
25
+ Thread.current[:i18n_fallbacks] = @@fallbacks
22
26
  end
23
27
  end
24
28
 
@@ -34,25 +38,24 @@ module I18n
34
38
  # The default option takes precedence over fallback locales only when
35
39
  # it's a Symbol. When the default contains a String, Proc or Hash
36
40
  # it is evaluated last after all the fallback locales have been tried.
37
- def translate(locale, key, options = {})
41
+ def translate(locale, key, options = EMPTY_HASH)
38
42
  return super unless options.fetch(:fallback, true)
39
43
  return super if options[:fallback_in_progress]
40
44
  default = extract_non_symbol_default!(options) if options[:default]
41
45
 
42
- begin
43
- options[:fallback_in_progress] = true
44
- I18n.fallbacks[locale].each do |fallback|
45
- begin
46
- catch(:exception) do
47
- result = super(fallback, key, options)
48
- return result unless result.nil?
46
+ fallback_options = options.merge(:fallback_in_progress => true)
47
+ I18n.fallbacks[locale].each do |fallback|
48
+ begin
49
+ catch(:exception) do
50
+ result = super(fallback, key, fallback_options)
51
+ unless result.nil?
52
+ on_fallback(locale, fallback, key, options) if locale != fallback
53
+ return result
49
54
  end
50
- rescue I18n::InvalidLocale
51
- # we do nothing when the locale is invalid, as this is a fallback anyways.
52
55
  end
56
+ rescue I18n::InvalidLocale
57
+ # we do nothing when the locale is invalid, as this is a fallback anyways.
53
58
  end
54
- ensure
55
- options.delete(:fallback_in_progress)
56
59
  end
57
60
 
58
61
  return if options.key?(:default) && options[:default].nil?
@@ -70,7 +73,8 @@ module I18n
70
73
  return first_non_symbol_default
71
74
  end
72
75
 
73
- def exists?(locale, key)
76
+ def exists?(locale, key, options = EMPTY_HASH)
77
+ return super unless options.fetch(:fallback, true)
74
78
  I18n.fallbacks[locale].each do |fallback|
75
79
  begin
76
80
  return true if super(fallback, key)
@@ -81,6 +85,13 @@ module I18n
81
85
 
82
86
  false
83
87
  end
88
+
89
+ private
90
+
91
+ # Overwrite on_fallback to add specified logic when the fallback succeeds.
92
+ def on_fallback(_original_locale, _fallback_locale, _key, _optoins)
93
+ nil
94
+ end
84
95
  end
85
96
  end
86
97
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module I18n
2
4
  module Backend
3
5
  # This module contains several helpers to assist flattening translations.
@@ -16,14 +18,17 @@ module I18n
16
18
  # and creates way less objects than the one at I18n.normalize_keys.
17
19
  # It also handles escaping the translation keys.
18
20
  def self.normalize_flat_keys(locale, key, scope, separator)
19
- keys = [scope, key].flatten.compact
21
+ keys = [scope, key]
22
+ keys.flatten!
23
+ keys.compact!
24
+
20
25
  separator ||= I18n.default_separator
21
26
 
22
27
  if separator != FLATTEN_SEPARATOR
23
- keys.map! do |k|
24
- k.to_s.tr("#{FLATTEN_SEPARATOR}#{separator}",
25
- "#{SEPARATOR_ESCAPE_CHAR}#{FLATTEN_SEPARATOR}")
26
- end
28
+ from_str = "#{FLATTEN_SEPARATOR}#{separator}"
29
+ to_str = "#{SEPARATOR_ESCAPE_CHAR}#{FLATTEN_SEPARATOR}"
30
+
31
+ keys.map! { |k| k.to_s.tr from_str, to_str }
27
32
  end
28
33
 
29
34
  keys.join(".")
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'i18n/gettext'
2
4
  require 'i18n/gettext/po_parser'
3
5
 
@@ -29,6 +31,8 @@ module I18n
29
31
  # Without it strings containing periods (".") will not be translated.
30
32
 
31
33
  module Gettext
34
+ using I18n::HashRefinements
35
+
32
36
  class PoData < Hash
33
37
  def set_comment(msgid_or_sym, comment)
34
38
  # ignore
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # The InterpolationCompiler module contains optimizations that can tremendously
2
4
  # speed up the interpolation process on the Simple backend.
3
5
  #
@@ -104,7 +106,7 @@ module I18n
104
106
  end
105
107
  end
106
108
 
107
- def store_translations(locale, data, options = {})
109
+ def store_translations(locale, data, options = EMPTY_HASH)
108
110
  compile_all_strings_in(data)
109
111
  super
110
112
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'i18n/backend/base'
2
4
 
3
5
  module I18n
@@ -65,6 +67,8 @@ module I18n
65
67
  #
66
68
  # This is useful if you are using a KeyValue backend chained to a Simple backend.
67
69
  class KeyValue
70
+ using I18n::HashRefinements
71
+
68
72
  module Implementation
69
73
  attr_accessor :store
70
74
 
@@ -74,7 +78,11 @@ module I18n
74
78
  @store, @subtrees = store, subtrees
75
79
  end
76
80
 
77
- def store_translations(locale, data, options = {})
81
+ def initialized?
82
+ !@store.nil?
83
+ end
84
+
85
+ def store_translations(locale, data, options = EMPTY_HASH)
78
86
  escape = options.fetch(:escape, true)
79
87
  flatten_translations(locale, data, escape, @subtrees).each do |key, value|
80
88
  key = "#{locale}.#{key}"
@@ -103,11 +111,31 @@ module I18n
103
111
 
104
112
  protected
105
113
 
114
+ # Queries the translations from the key-value store and converts
115
+ # them into a hash such as the one returned from loading the
116
+ # haml files
117
+ def translations
118
+ @translations = @store.keys.clone.map do |main_key|
119
+ main_value = JSON.decode(@store[main_key])
120
+ main_key.to_s.split(".").reverse.inject(main_value) do |value, key|
121
+ {key.to_sym => value}
122
+ end
123
+ end.inject{|hash, elem| hash.deep_merge!(elem)}.deep_symbolize_keys
124
+ end
125
+
126
+ def init_translations
127
+ # NO OP
128
+ # This call made also inside Simple Backend and accessed by
129
+ # other plugins like I18n-js and babilu and
130
+ # to use it along with the Chain backend we need to
131
+ # provide a uniform API even for protected methods :S
132
+ end
133
+
106
134
  def subtrees?
107
135
  @subtrees
108
136
  end
109
137
 
110
- def lookup(locale, key, scope = [], options = {})
138
+ def lookup(locale, key, scope = [], options = EMPTY_HASH)
111
139
  key = normalize_flat_keys(locale, key, scope, options[:separator])
112
140
  value = @store["#{locale}.#{key}"]
113
141
  value = JSON.decode(value) if value
@@ -125,6 +153,7 @@ module I18n
125
153
  if subtrees?
126
154
  super
127
155
  else
156
+ return entry unless entry.is_a?(Hash)
128
157
  key = pluralization_key(entry, count)
129
158
  entry[key]
130
159
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Memoize module simply memoizes the values returned by lookup using
2
4
  # a flat hash and can tremendously speed up the lookup process in a backend.
3
5
  #
@@ -14,7 +16,7 @@ module I18n
14
16
  @memoized_locales ||= super
15
17
  end
16
18
 
17
- def store_translations(locale, data, options = {})
19
+ def store_translations(locale, data, options = EMPTY_HASH)
18
20
  reset_memoizations!(locale)
19
21
  super
20
22
  end
@@ -24,9 +26,15 @@ module I18n
24
26
  super
25
27
  end
26
28
 
29
+ def eager_load!
30
+ memoized_lookup
31
+ available_locales
32
+ super
33
+ end
34
+
27
35
  protected
28
36
 
29
- def lookup(locale, key, scope = nil, options = {})
37
+ def lookup(locale, key, scope = nil, options = EMPTY_HASH)
30
38
  flat_key = I18n::Backend::Flatten.normalize_flat_keys(locale,
31
39
  key, scope, options[:separator]).to_sym
32
40
  flat_hash = memoized_lookup[locale.to_sym]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # I18n translation metadata is useful when you want to access information
2
4
  # about how a translation was looked up, pluralized or interpolated in
3
5
  # your application.
@@ -35,19 +37,19 @@ module I18n
35
37
  end
36
38
  end
37
39
 
38
- def translate(locale, key, options = {})
40
+ def translate(locale, key, options = EMPTY_HASH)
39
41
  metadata = {
40
42
  :locale => locale,
41
43
  :key => key,
42
44
  :scope => options[:scope],
43
45
  :default => options[:default],
44
46
  :separator => options[:separator],
45
- :values => options.reject { |name, value| RESERVED_KEYS.include?(name) }
47
+ :values => options.reject { |name, _value| RESERVED_KEYS.include?(name) }
46
48
  }
47
49
  with_metadata(metadata) { super }
48
50
  end
49
51
 
50
- def interpolate(locale, entry, values = {})
52
+ def interpolate(locale, entry, values = EMPTY_HASH)
51
53
  metadata = entry.translation_metadata.merge(:original => entry)
52
54
  with_metadata(metadata) { super }
53
55
  end