Guest User

Untitled

a guest
May 24th, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. From 3fa9fdd93de1eef83653b02cd4ac8201654543c5 Mon Sep 17 00:00:00 2001
  2. From: =?utf-8?q?Tarmo=20T=C3=A4nav?= <tarmo@itech.ee>
  3. Date: Tue, 7 Oct 2008 14:23:05 +0300
  4. Subject: [PATCH] Made i18n simple backend able to store false values (and not confuse them with nil or lack of value)
  5.  
  6. Implemented support.array.skip_last_comma i18n key for
  7. Array#to_sentence, this also tests the ability to store false.
  8. ---
  9. .../active_support/core_ext/array/conversions.rb | 3 ++-
  10. activesupport/lib/active_support/locale/en-US.yml | 1 +
  11. .../vendor/i18n-0.0.1/i18n/backend/simple.rb | 16 ++++++++++++++--
  12. activesupport/test/i18n_test.rb | 16 ++++++++++++++++
  13. 4 files changed, 33 insertions(+), 3 deletions(-)
  14.  
  15. diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
  16. index e67b719..11c128d 100644
  17. --- a/activesupport/lib/active_support/core_ext/array/conversions.rb
  18. +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
  19. @@ -11,7 +11,8 @@ module ActiveSupport #:nodoc:
  20. options.assert_valid_keys(:connector, :skip_last_comma, :locale)
  21.  
  22. default = I18n.translate(:'support.array.sentence_connector', :locale => options[:locale])
  23. - options.reverse_merge! :connector => default, :skip_last_comma => false
  24. + default_skip_last_comma = I18n.translate(:'support.array.skip_last_comma', :locale => options[:locale])
  25. + options.reverse_merge! :connector => default, :skip_last_comma => default_skip_last_comma
  26. options[:connector] = "#{options[:connector]} " unless options[:connector].nil? || options[:connector].strip == ''
  27.  
  28. case length
  29. diff --git a/activesupport/lib/active_support/locale/en-US.yml b/activesupport/lib/active_support/locale/en-US.yml
  30. index 60ecb1d..c31694b 100644
  31. --- a/activesupport/lib/active_support/locale/en-US.yml
  32. +++ b/activesupport/lib/active_support/locale/en-US.yml
  33. @@ -29,3 +29,4 @@ en-US:
  34. support:
  35. array:
  36. sentence_connector: "and"
  37. + skip_last_comma: false
  38. diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
  39. index 2dbaf8a..30e3655 100644
  40. --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
  41. +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
  42. @@ -30,7 +30,13 @@ module I18n
  43. options.delete(:default)
  44. values = options.reject{|name, value| reserved.include? name }
  45.  
  46. - entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
  47. + entry = lookup(locale, key, scope)
  48. + if entry.nil?
  49. + entry = default(locale, default, options)
  50. + if entry.nil?
  51. + raise(I18n::MissingTranslationData.new(locale, key, options))
  52. + end
  53. + end
  54. entry = pluralize locale, entry, count
  55. entry = interpolate locale, entry, values
  56. entry
  57. @@ -83,7 +89,13 @@ module I18n
  58. return unless key
  59. init_translations unless initialized?
  60. keys = I18n.send :normalize_translation_keys, locale, key, scope
  61. - keys.inject(translations){|result, k| result[k.to_sym] or return nil }
  62. + keys.inject(translations) do |result, k|
  63. + if (x = result[k.to_sym]).nil?
  64. + return nil
  65. + else
  66. + x
  67. + end
  68. + end
  69. end
  70.  
  71. # Evaluates a default translation.
  72. diff --git a/activesupport/test/i18n_test.rb b/activesupport/test/i18n_test.rb
  73. index 4b17e3c..db5bd5e 100644
  74. --- a/activesupport/test/i18n_test.rb
  75. +++ b/activesupport/test/i18n_test.rb
  76. @@ -72,4 +72,20 @@ class I18nTest < Test::Unit::TestCase
  77. def test_time_pm
  78. assert_equal 'pm', I18n.translate(:'time.pm')
  79. end
  80. +
  81. + def test_sentence_connector
  82. + assert_equal 'and', I18n.translate(:'support.array.sentence_connector')
  83. + end
  84. +
  85. + def test_skip_last_comma
  86. + assert_equal false, I18n.translate(:'support.array.skip_last_comma')
  87. + end
  88. +
  89. + def test_to_sentence
  90. + assert_equal 'a, b, and c', %w[a b c].to_sentence
  91. + I18n.backend.store_translations 'en-US', :support => { :array => { :skip_last_comma => true } }
  92. + assert_equal 'a, b and c', %w[a b c].to_sentence
  93. + ensure
  94. + I18n.backend.store_translations 'en-US', :support => { :array => { :skip_last_comma => false } }
  95. + end
  96. end
  97. --
  98. 1.6.0.2
Add Comment
Please, Sign In to add comment