i18n 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of i18n might be problematic. Click here for more details.

Files changed (68) hide show
  1. data/CHANGELOG.textile +19 -0
  2. data/README.textile +41 -15
  3. data/Rakefile +1 -1
  4. data/VERSION +1 -1
  5. data/lib/i18n.rb +1 -1
  6. data/lib/i18n/backend/active_record.rb +6 -5
  7. data/lib/i18n/backend/active_record/missing.rb +64 -0
  8. data/lib/i18n/backend/active_record/translation.rb +2 -2
  9. data/lib/i18n/backend/base.rb +2 -2
  10. data/lib/i18n/backend/chain.rb +3 -3
  11. data/lib/i18n/helpers/gettext.rb +1 -1
  12. data/test/all.rb +4 -4
  13. data/test/api/basics.rb +8 -8
  14. data/test/api/defaults.rb +40 -0
  15. data/test/api/interpolation.rb +65 -67
  16. data/test/api/link.rb +31 -38
  17. data/test/api/localization/date.rb +69 -50
  18. data/test/api/localization/date_time.rb +66 -42
  19. data/test/api/localization/procs.rb +54 -0
  20. data/test/api/localization/time.rb +71 -52
  21. data/test/api/lookup.rb +38 -0
  22. data/test/api/pluralization.rb +22 -24
  23. data/test/api/procs.rb +40 -0
  24. data/test/cases/api/active_record_test.rb +28 -0
  25. data/test/cases/api/all_features_test.rb +37 -0
  26. data/test/cases/api/chain_test.rb +26 -0
  27. data/test/cases/api/fallbacks_test.rb +33 -0
  28. data/test/cases/api/pluralization_test.rb +33 -0
  29. data/test/cases/api/simple_test.rb +21 -0
  30. data/test/{backend/active_record_missing/active_record_missing_test.rb → cases/backend/active_record/missing_test.rb} +5 -8
  31. data/test/{backend/active_record → cases/backend}/active_record_test.rb +13 -3
  32. data/test/{backend/cache → cases/backend}/cache_test.rb +7 -7
  33. data/test/{backend/chain → cases/backend}/chain_test.rb +1 -1
  34. data/test/{backend/fallbacks → cases/backend}/fallbacks_test.rb +5 -5
  35. data/test/{backend/simple → cases/backend}/helpers_test.rb +1 -1
  36. data/test/{backend/metadata → cases/backend}/metadata_test.rb +12 -10
  37. data/test/{backend/pluralization → cases/backend}/pluralization_test.rb +10 -10
  38. data/test/cases/backend/simple_test.rb +77 -0
  39. data/test/{core_ext → cases/core_ext}/string/interpolate_test.rb +15 -15
  40. data/test/{gettext → cases/gettext}/api_test.rb +1 -4
  41. data/test/{gettext → cases/gettext}/backend_test.rb +9 -2
  42. data/test/{i18n_exceptions_test.rb → cases/i18n_exceptions_test.rb} +1 -1
  43. data/test/{i18n_load_path_test.rb → cases/i18n_load_path_test.rb} +2 -2
  44. data/test/{i18n_test.rb → cases/i18n_test.rb} +1 -1
  45. data/test/{locale → cases/locale}/fallbacks_test.rb +1 -3
  46. data/test/{locale → cases/locale}/tag/rfc4646_test.rb +1 -3
  47. data/test/{locale → cases/locale}/tag/simple_test.rb +1 -3
  48. data/test/fixtures/locales/de.po +5 -0
  49. data/test/test_helper.rb +45 -30
  50. data/vendor/po_parser.rb +329 -0
  51. metadata +60 -69
  52. data/lib/i18n/backend/active_record_missing.rb +0 -55
  53. data/test/api/lambda.rb +0 -52
  54. data/test/api/localization/lambda.rb +0 -26
  55. data/test/api/translation.rb +0 -51
  56. data/test/backend/active_record/all.rb +0 -3
  57. data/test/backend/active_record/api_test.rb +0 -54
  58. data/test/backend/active_record/setup.rb +0 -166
  59. data/test/backend/all/api_test.rb +0 -88
  60. data/test/backend/chain/api_test.rb +0 -80
  61. data/test/backend/fallbacks/api_test.rb +0 -84
  62. data/test/backend/pluralization/api_test.rb +0 -86
  63. data/test/backend/simple/all.rb +0 -5
  64. data/test/backend/simple/api_test.rb +0 -92
  65. data/test/backend/simple/lookup_test.rb +0 -24
  66. data/test/backend/simple/setup.rb +0 -147
  67. data/test/backend/simple/translations_test.rb +0 -83
  68. data/test/with_options.rb +0 -34
@@ -4,6 +4,25 @@ h2. master
4
4
 
5
5
  * (no changes)
6
6
 
7
+ h2. 0.3.1 (2009-12-11)
8
+
9
+ * "Add PoParser to gemspec":http://github.com/svenfuchs/i18n/commit/d6b2763f39c932f66adb039b96882a472f883c51
10
+ * "Enable custom separators for ActiveRecord backend":http://github.com/svenfuchs/i18n/commit/9341d3fcfc951cc31807ba672d2b5d90909ef3e5
11
+ * "Pass interpolation values to interpolation procs":http://github.com/svenfuchs/i18n/commit/39c2ed8fbad645671cd5520ce7ad0aeefe2b0cca
12
+ * "Fix that ngettext supports keys with dots":http://github.com/svenfuchs/i18n/commit/7362a43c34364d500de8899cfcca6bf1a5e6d1c8
13
+
14
+ h2. 0.3.0 (2009-11-30)
15
+
16
+ * "Gettext backend and helpers":http://github.com/svenfuchs/i18n/commit/35a1740d2f10b808548af352006950da4017e374
17
+ * "Metadata module":http://github.com/svenfuchs/i18n/commit/2677208555179b36fcbe958c0e8bc642cf5bc020
18
+ * "Basic ActiveRecord backend":http://github.com/svenfuchs/i18n/commit/786632d0b42de423ecf0969622efc87f1691e2a2
19
+ * "Set encoding to UTF8 for all files":http://github.com/svenfuchs/i18n/commit/9be3d4a311b5bf583eec5d39986176cc40c112f2
20
+ * "Chain backend":http://github.com/svenfuchs/i18n/commit/08259ffb88b3005403648d77bc1cbca0b92f3cf5
21
+ * "Backend/cache implementation":http://github.com/svenfuchs/i18n/commit/e7bf15351cd2e27f5972eb40e65a5dd6f4a0feed
22
+ * "Pluralization module":http://github.com/svenfuchs/i18n/commit/9ca4c9ed52d4706566a6abeb2d78722dcc5d4764
23
+ * "add and adapt Globalize2 fallback implementation":http://github.com/svenfuchs/i18n/commit/1b37a303b27d6222b17162804b06323e5628768f
24
+ * "move Simple backend implementation to a Base backend class and extend Simple from Base.":http://github.com/svenfuchs/i18n/commit/32ddc80a04e6aa247f6d6613bde7f78c73396cb4
25
+
7
26
  h2. 0.2.0 (2009-07-12)
8
27
 
9
28
  * "Allow using Ruby 1.9 syntax for string interpolation (API addition)":http://github.com/svenfuchs/i18n/commit/c6e0b06d512f2af57199a843a1d8a40241b32861
@@ -14,31 +14,40 @@ Features:
14
14
  * custom exception handlers
15
15
  * extensible architecture with a swappable backend
16
16
 
17
- Experimental, pluggable features:
17
+ Pluggable features:
18
18
 
19
- * lambda pluralizers stored as translation data
20
- * RFC4647 compliant locale fallbacks (with optional RFC4646 locale validation)
21
- * backend cache
19
+ * Cache
20
+ * Pluralization: lambda pluralizers stored as translation data
21
+ * Locale fallbacks, RFC4647 compliant (optionally: RFC4646 locale validation)
22
+ * Gettext support
23
+ * Translation metadata
24
+
25
+ Alternative backends:
26
+
27
+ * Chain
28
+ * ActiveRecord (optionally: ActiveRecordMissing)
22
29
 
23
30
  For more information and lots of resources see: "http://rails-i18n.org/wiki":http://rails-i18n.org/wiki
24
31
 
25
- h2. Install
32
+ h2. Installation
26
33
 
27
34
  gem install i18n
28
35
 
29
- h3. Install on Rails 2.3.4 (deprecated)
36
+ h3. Installation on Rails < 2.3.5 (deprecated)
30
37
 
31
- Rails 2.3.4 will not accept i18n gems > 0.1.3. There is an unpacked gem inside of active_support/lib/vendor which gets loaded unless gem 'i18n', '~> 0.1.3'. This requirement is relaxed in http://github.com/rails/rails/commit/6da036538334fd459156ab2789c9fae5512be5d2
38
+ Up to version 2.3.4 Rails will not accept i18n gems > 0.1.3. There is an unpacked
39
+ gem inside of active_support/lib/vendor which gets loaded unless gem 'i18n', '~> 0.1.3'.
40
+ This requirement is relaxed in "6da03653":http://github.com/rails/rails/commit/6da03653
32
41
 
33
- The following is needed to get the new i18n gem inside vendor/plugins loaded properly.
42
+ The new i18n gem can be loaded from vendor/plugins like this:
34
43
 
35
- def reload_i18n!
36
- raise "Move to i18n version 0.2.0 or greater" if Rails.version > "2.3.4"
37
-
38
- $:.grep(/i18n/).each { |path| $:.delete(path) }
39
- I18n::Backend.send :remove_const, "Simple"
40
- $: << Rails.root.join('vendor', 'plugins', 'i18n', 'lib').to_s
41
- end
44
+ def reload_i18n!
45
+ raise "Move to i18n version 0.2.0 or greater" if Rails.version > "2.3.4"
46
+
47
+ $:.grep(/i18n/).each { |path| $:.delete(path) }
48
+ I18n::Backend.send :remove_const, "Simple"
49
+ $: << Rails.root.join('vendor', 'plugins', 'i18n', 'lib').to_s
50
+ end
42
51
 
43
52
  Then you can `reload_i18n!` inside an i18n initializer.
44
53
 
@@ -50,6 +59,23 @@ h2. Authors
50
59
  * "Saimon Moore":http://saimonmoore.net
51
60
  * "Matt Aimonetti":http://railsontherun.com
52
61
 
62
+ h2. Contributors
63
+
64
+ * Akira Matsuda
65
+ * Andrew Briening
66
+ * Clemens Kofler
67
+ * Frederick Cheung
68
+ * Jeremy Kemper
69
+ * José Valim
70
+ * Lawrence Pit
71
+ * Luca Guidi
72
+ * M4SSIVE
73
+ * Marko Seppae
74
+ * Mathias Meyer
75
+ * Michael Lang
76
+ * Theo Cushion
77
+ * Yaroslav Markin
78
+
53
79
  h2. License
54
80
 
55
81
  MIT License. See the included MIT-LICENCE file.
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ begin
14
14
  s.homepage = "http://rails-i18n.org"
15
15
  s.description = "Add Internationalization support to your Ruby application."
16
16
  s.authors = ['Sven Fuchs', 'Joshua Harvey', 'Matt Aimonetti', 'Stephan Soller', 'Saimon Moore']
17
- s.files = FileList["[A-Z]*", "{lib,test}/**/*"]
17
+ s.files = FileList["[A-Z]*", "{lib,test,vendor}/**/*"]
18
18
  end
19
19
  rescue LoadError
20
20
  puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -203,7 +203,7 @@ module I18n
203
203
  def translate(*args)
204
204
  options = args.last.is_a?(Hash) ? args.pop : {}
205
205
  key = args.shift
206
- locale = options.delete(:locale) || I18n.locale
206
+ locale = options.key?(:locale) ? options.delete(:locale) : I18n.locale
207
207
  raises = options.delete(:raise)
208
208
  backend.translate(locale, key, options)
209
209
  rescue I18n::ArgumentError => exception
@@ -7,10 +7,11 @@ require 'i18n/backend/active_record/translation'
7
7
  # as the following example shows:
8
8
  #
9
9
  # I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18.backend, I18n::Backend::ActiveRecordMissing.new)
10
- #
10
+ #
11
11
  module I18n
12
12
  module Backend
13
13
  class ActiveRecord
14
+ autoload :Missing, 'i18n/backend/active_record/missing'
14
15
  autoload :StoreProcs, 'i18n/backend/active_record/store_procs'
15
16
  autoload :Translation, 'i18n/backend/active_record/translation'
16
17
 
@@ -19,9 +20,9 @@ module I18n
19
20
  def reload!
20
21
  end
21
22
 
22
- def store_translations(locale, data)
23
- separator = I18n.default_separator # TODO allow to pass as an option?
24
- wind_keys(data).each do |key, v|
23
+ def store_translations(locale, data, options = {})
24
+ separator = options[:separator] || I18n.default_separator
25
+ wind_keys(data, separator).each do |key, v|
25
26
  Translation.locale(locale).lookup(expand_keys(key, separator), separator).delete_all
26
27
  Translation.create(:locale => locale.to_s, :key => key, :value => v)
27
28
  end
@@ -54,7 +55,7 @@ module I18n
54
55
  hash[r.key.slice(chop_range)] = r.value
55
56
  hash
56
57
  end
57
- deep_symbolize_keys(unwind_keys(result))
58
+ deep_symbolize_keys(unwind_keys(result, separator))
58
59
  end
59
60
  end
60
61
 
@@ -0,0 +1,64 @@
1
+ # This extension stores translation stub records for missing translations to
2
+ # the database.
3
+ #
4
+ # This is useful if you have a web based translation tool. It will populate
5
+ # the database with untranslated keys as the application is being used. A
6
+ # translator can then go through these and add missing translations.
7
+ #
8
+ # Example usage:
9
+ #
10
+ # I18n::Backend::Chain.send(:include, I18n::Backend::ActiveRecord::Missing)
11
+ # I18n.backend = I18nChainBackend.new(I18n::Backend::ActiveRecord.new, I18n::Backend::Simple.new)
12
+ #
13
+ # Stub records for pluralizations will also be created for each key defined
14
+ # in i18n.plural_keys.
15
+ #
16
+ # For example:
17
+ #
18
+ # # en.yml
19
+ # en:
20
+ # i18n:
21
+ # plural_keys: [:zero, :one, :other]
22
+ #
23
+ # # pl.yml
24
+ # pl:
25
+ # i18n:
26
+ # plural_keys: [:zero, :one, :few, :other]
27
+ #
28
+ # It will also persist interpolation keys in Translation#interpolations so
29
+ # translators will be able to review and use them.
30
+ module I18n
31
+ module Backend
32
+ class ActiveRecord
33
+ module Missing
34
+ def store_default_translations(locale, key, options = {})
35
+ count, scope, default, separator = options.values_at(:count, *Base::RESERVED_KEYS)
36
+ separator ||= I18n.default_separator
37
+ keys = I18n.send(:normalize_translation_keys, locale, key, scope, separator)[1..-1]
38
+ key = keys.join(separator || I18n.default_separator)
39
+
40
+ unless ActiveRecord::Translation.locale(locale).lookup(key, separator).exists?
41
+ interpolations = options.reject { |name, value| Base::RESERVED_KEYS.include?(name) }.keys
42
+ keys = count ? I18n.t('i18n.plural_keys', :locale => locale).map { |k| [key, k].join(separator) } : [key]
43
+ keys.each { |key| store_default_translation(locale, key, interpolations) }
44
+ end
45
+ end
46
+
47
+ def store_default_translation(locale, key, interpolations)
48
+ translation = ActiveRecord::Translation.new :locale => locale.to_s, :key => key
49
+ translation.interpolations = interpolations
50
+ translation.save
51
+ end
52
+
53
+ def translate(locale, key, options = {})
54
+ super
55
+
56
+ rescue I18n::MissingTranslationData => e
57
+ self.store_default_translations(locale, key, options)
58
+
59
+ raise e
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -57,12 +57,12 @@ module I18n
57
57
 
58
58
  named_scope :lookup, lambda { |keys, *separator|
59
59
  keys = Array(keys).map! { |key| key.to_s }
60
- separator ||= I18n.default_separator
60
+ separator = separator.first || I18n.default_separator
61
61
  { :conditions => ["`key` IN (?) OR `key` LIKE '#{keys.last}#{separator}%'", keys] }
62
62
  }
63
63
 
64
64
  def self.available_locales
65
- Translation.find(:all, :select => 'DISTINCT locale').map { |t| t.locale }
65
+ Translation.find(:all, :select => 'DISTINCT locale').map { |t| t.locale.to_sym }
66
66
  end
67
67
 
68
68
  def interpolates?(key)
@@ -21,7 +21,7 @@ module I18n
21
21
  # This uses a deep merge for the translations hash, so existing
22
22
  # translations will be overwritten by new ones only at the deepest
23
23
  # level of the hash.
24
- def store_translations(locale, data)
24
+ def store_translations(locale, data, options = {})
25
25
  merge_translations(locale, data)
26
26
  end
27
27
 
@@ -183,7 +183,7 @@ module I18n
183
183
  "%{#{key}}"
184
184
  end
185
185
  end
186
- values.each { |key, value| values[key] = value.call if interpolate_lambda?(value, s, key) }
186
+ values.each { |key, value| values[key] = value.call(values) if interpolate_lambda?(value, s, key) }
187
187
  s % values
188
188
 
189
189
  rescue KeyError => e
@@ -27,8 +27,8 @@ module I18n
27
27
  backends.each { |backend| backend.reload! }
28
28
  end
29
29
 
30
- def store_translations(locale, data)
31
- backends.first.store_translations(locale, data)
30
+ def store_translations(locale, data, options = {})
31
+ backends.first.store_translations(locale, data, options = {})
32
32
  end
33
33
 
34
34
  def available_locales
@@ -62,7 +62,7 @@ module I18n
62
62
  result = backend.localize(locale, object, format, options) and return result
63
63
  rescue MissingTranslationData
64
64
  end
65
- end or nil
65
+ end and nil
66
66
  end
67
67
 
68
68
  protected
@@ -40,7 +40,7 @@ module I18n
40
40
 
41
41
  scope, msgid = I18n::Gettext.extract_scope(msgid, separator)
42
42
  default = { :one => msgid, :other => msgid_plural }
43
- I18n.t(msgid, :default => default, :count => n, :scope => scope)
43
+ I18n.t(msgid, :default => default, :count => n, :scope => scope, :separator => separator)
44
44
  end
45
45
  alias ns_ nsgettext
46
46
 
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
- base = File.dirname(__FILE__)
4
- $LOAD_PATH.unshift base
3
+ dir = File.dirname(__FILE__)
4
+ $LOAD_PATH.unshift(dir)
5
5
 
6
- Dir["#{base}/**/*_test.rb"].sort.each do |file|
7
- require file.sub(/^#{base}\/(.*)\.rb$/, '\1')
6
+ Dir["#{dir}/**/*_test.rb"].sort.each do |file|
7
+ require file.sub(/^#{dir}\/(.*)\.rb$/, '\1')
8
8
  end
@@ -1,14 +1,14 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Tests
4
- module Backend
5
- module Api
6
- module Basics
7
- def test_available_locales
8
- backend_store_translations 'de', :foo => 'bar'
9
- backend_store_translations 'en', :foo => 'foo'
10
- assert_equal ['de', 'en'], I18n.backend.available_locales.map{|locale| locale.to_s }.sort
11
- end
4
+ module Api
5
+ module Basics
6
+ def test_available_locales
7
+ store_translations('de', :foo => 'bar')
8
+ store_translations('en', :foo => 'foo')
9
+
10
+ assert I18n.available_locales.include?(:de)
11
+ assert I18n.available_locales.include?(:en)
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ module Tests
4
+ module Api
5
+ module Defaults
6
+ def setup
7
+ super
8
+ store_translations(:foo => { :bar => 'bar', :baz => 'baz' })
9
+ end
10
+
11
+ define_method "test defaults: given nil as a key it returns the given default" do
12
+ assert_equal 'default', I18n.t(nil, :default => 'default')
13
+ end
14
+
15
+ define_method "test defaults: given a symbol as a default it translates the symbol" do
16
+ assert_equal 'bar', I18n.t(nil, :default => :'foo.bar')
17
+ end
18
+
19
+ define_method "test defaults: given a symbol as a default and a scope it stays inside the scope when looking up the symbol" do
20
+ assert_equal 'bar', I18n.t(:missing, :default => :bar, :scope => :foo)
21
+ end
22
+
23
+ define_method "test defaults: given an array as a default it returns the first match" do
24
+ assert_equal 'bar', I18n.t(:does_not_exist, :default => [:does_not_exist_2, :'foo.bar'])
25
+ end
26
+
27
+ define_method "test defaults: given an array of missing keys it raises a MissingTranslationData exception" do
28
+ assert_raises I18n::MissingTranslationData do
29
+ I18n.t(:does_not_exist, :default => [:does_not_exist_2, :does_not_exist_3], :raise => true)
30
+ end
31
+ end
32
+
33
+ define_method "test defaults: using a custom scope separator" do
34
+ # data must have been stored using the custom separator when using the ActiveRecord backend
35
+ I18n.backend.store_translations(:en, { :foo => { :bar => 'bar' } }, { :separator => '|' })
36
+ assert_equal 'bar', I18n.t(nil, :default => :'foo|bar', :separator => '|')
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,93 +1,91 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Tests
4
- module Backend
5
- module Api
6
- module Interpolation
7
- def interpolate(*args)
8
- options = args.last.is_a?(Hash) ? args.pop : {}
9
- key = args.pop
10
- I18n.backend.translate('en', key, options)
11
- end
4
+ module Api
5
+ module Interpolation
6
+ def interpolate(*args)
7
+ options = args.last.is_a?(Hash) ? args.pop : {}
8
+ key = args.pop
9
+ I18n.backend.translate('en', key, options)
10
+ end
12
11
 
13
- def test_interpolation_given_no_interpolation_values_it_does_not_alter_the_string
14
- assert_equal 'Hi {{name}}!', interpolate(:default => 'Hi {{name}}!')
15
- end
12
+ define_method "test interpolation: given no values it does not alter the string" do
13
+ assert_equal 'Hi {{name}}!', interpolate(:default => 'Hi {{name}}!')
14
+ end
16
15
 
17
- def test_interpolation_given_interpolation_values_it_interpolates_the_values_to_the_string
18
- assert_equal 'Hi David!', interpolate(:default => 'Hi {{name}}!', :name => 'David')
19
- end
16
+ define_method "test interpolation: given values it interpolates them into the string" do
17
+ assert_equal 'Hi David!', interpolate(:default => 'Hi {{name}}!', :name => 'David')
18
+ end
20
19
 
21
- def test_interpolation_given_interpolation_values_with_nil_values_it_interpolates_the_values_to_the_string
22
- assert_equal 'Hi !', interpolate(:default => 'Hi {{name}}!', :name => nil)
23
- end
20
+ define_method "test interpolation: given a nil value it still interpolates it into the string" do
21
+ assert_equal 'Hi !', interpolate(:default => 'Hi {{name}}!', :name => nil)
22
+ end
24
23
 
25
- def test_interpolation_given_a_lambda_as_a_value_it_calls_it_when_the_string_contains_the_key
26
- assert_equal 'Hi David!', interpolate(:default => 'Hi {{name}}!', :name => lambda { 'David' })
27
- end
24
+ define_method "test interpolation: given a lambda as a value it calls it if the string contains the key" do
25
+ assert_equal 'Hi David!', interpolate(:default => 'Hi {{name}}!', :name => lambda { 'David' })
26
+ end
28
27
 
29
- def test_interpolation_given_a_lambda_as_a_value_it_does_not_call_it_when_the_string_does_not_contain_the_key
30
- assert_nothing_raised { interpolate(:default => 'Hi!', :name => lambda { raise 'fail' }) }
31
- end
28
+ define_method "test interpolation: given a lambda as a value it does not call it if the string does not contain the key" do
29
+ assert_nothing_raised { interpolate(:default => 'Hi!', :name => lambda { raise 'fail' }) }
30
+ end
32
31
 
33
- def test_interpolation_given_interpolation_values_but_missing_a_key_it_raises_a_missing_interpolation_argument_exception
34
- assert_raises(I18n::MissingInterpolationArgument) do
35
- interpolate(:default => '{{foo}}', :bar => 'bar')
36
- end
32
+ define_method "test interpolation: given values but missing a key it raises I18n::MissingInterpolationArgument" do
33
+ assert_raises(I18n::MissingInterpolationArgument) do
34
+ interpolate(:default => '{{foo}}', :bar => 'bar')
37
35
  end
36
+ end
38
37
 
39
- def test_interpolation_does_not_raise_missing_interpolation_argument_exceptions_for_escaped_variables
40
- assert_nothing_raised(I18n::MissingInterpolationArgument) do
41
- assert_equal 'Barr {{foo}}', interpolate(:default => '{{bar}} \{{foo}}', :bar => 'Barr')
42
- end
38
+ define_method "test interpolation: it does not raise I18n::MissingInterpolationArgument for escaped variables" do
39
+ assert_nothing_raised(I18n::MissingInterpolationArgument) do
40
+ assert_equal 'Barr {{foo}}', interpolate(:default => '{{bar}} \{{foo}}', :bar => 'Barr')
43
41
  end
42
+ end
44
43
 
45
- def test_interpolation_does_not_change_the_original_stored_translation_string_and_allows_reinterpolation
46
- I18n.backend.store_translations(:en, :interpolate => 'Hi {{name}}!')
47
- assert_equal 'Hi David!', interpolate(:interpolate, :name => 'David')
48
- assert_equal 'Hi Yehuda!', interpolate(:interpolate, :name => 'Yehuda')
49
- # assert_equal 'Hi {{name}}!', I18n.backend.instance_variable_get(:@translations)[:en][:interpolate]
50
- end
44
+ define_method "test interpolation: it does not change the original, stored translation string" do
45
+ I18n.backend.store_translations(:en, :interpolate => 'Hi {{name}}!')
46
+ assert_equal 'Hi David!', interpolate(:interpolate, :name => 'David')
47
+ assert_equal 'Hi Yehuda!', interpolate(:interpolate, :name => 'Yehuda')
48
+ end
51
49
 
52
- def test_interpolate_with_ruby_1_9_syntax
53
- assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => 'David')
54
- end
50
+ define_method "test interpolation: works with Ruby 1.9 syntax" do
51
+ assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => 'David')
52
+ end
55
53
 
56
- def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
57
- assert_equal 'Häi David!', interpolate(:default => 'Häi {{name}}!', :name => 'David')
58
- end
54
+ define_method "test interpolation: given the translation is in utf-8 it still works" do
55
+ assert_equal 'Häi David!', interpolate(:default => 'Häi {{name}}!', :name => 'David')
56
+ end
59
57
 
60
- def test_interpolate_given_a_unicode_value_hash_interpolates_to_the_string
61
- assert_equal 'Hi ゆきひろ!', interpolate(:default => 'Hi {{name}}!', :name => 'ゆきひろ')
62
- end
58
+ define_method "test interpolation: given the value is in utf-8 it still works" do
59
+ assert_equal 'Hi ゆきひろ!', interpolate(:default => 'Hi {{name}}!', :name => 'ゆきひろ')
60
+ end
63
61
 
64
- def test_interpolate_given_a_unicode_value_hash_interpolates_into_unicode_string
65
- assert_equal 'こんにちは、ゆきひろさん!', interpolate(:default => 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
66
- end
62
+ define_method "test interpolation: given the translation and the value are in utf-8 it still works" do
63
+ assert_equal 'こんにちは、ゆきひろさん!', interpolate(:default => 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
64
+ end
67
65
 
68
- if Kernel.const_defined?(:Encoding)
69
- def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
70
- assert_equal euc_jp('Hi ゆきひろ!'), interpolate(:default => 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
71
- end
66
+ if Kernel.const_defined?(:Encoding)
67
+ define_method "test interpolation: given a utf-8 translation and a euc-jp value it returns a translation in euc-jp" do
68
+ assert_equal euc_jp('Hi ゆきひろ!'), interpolate(:default => 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
69
+ end
72
70
 
73
- def test_interpolate_given_a_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
74
- assert_raises(Encoding::CompatibilityError) do
75
- interpolate(:default => euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
76
- end
71
+ define_method "test interpolation: given a euc-jp translation and a utf-8 value it raises Encoding::CompatibilityError" do
72
+ assert_raises(Encoding::CompatibilityError) do
73
+ interpolate(:default => euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
77
74
  end
78
-
79
- def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
80
- assert_raises(Encoding::CompatibilityError) do
81
- interpolate(:default => 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
82
- end
75
+ end
76
+
77
+ # TODO should better explain how this relates to the test above with the simpler utf-8 default string
78
+ define_method "test interpolation: given a utf-8 translation and a euc-jp value it raises Encoding::CompatibilityError" do
79
+ assert_raises(Encoding::CompatibilityError) do
80
+ interpolate(:default => 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
83
81
  end
84
82
  end
83
+ end
85
84
 
86
- def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
87
- assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{default}}', :foo => :bar) }
88
- assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{scope}}', :foo => :bar) }
89
- assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{separator}}', :foo => :bar) }
90
- end
85
+ define_method "test interpolation: given a translations containing a reserved key it raises I18n::ReservedInterpolationKey" do
86
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{default}}', :foo => :bar) }
87
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{scope}}', :foo => :bar) }
88
+ assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '{{separator}}', :foo => :bar) }
91
89
  end
92
90
  end
93
91
  end