activesupport 2.3.5 → 2.3.6.pre

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

Potentially problematic release.


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

Files changed (121) hide show
  1. data/CHANGELOG +23 -0
  2. data/lib/active_support.rb +1 -0
  3. data/lib/active_support/core_ext/date_time/conversions.rb +14 -3
  4. data/lib/active_support/core_ext/enumerable.rb +6 -0
  5. data/lib/active_support/core_ext/file/atomic.rb +2 -1
  6. data/lib/active_support/core_ext/numeric/conversions.rb +2 -2
  7. data/lib/active_support/core_ext/object.rb +1 -0
  8. data/lib/active_support/core_ext/object/metaclass.rb +6 -5
  9. data/lib/active_support/core_ext/object/singleton_class.rb +13 -0
  10. data/lib/active_support/core_ext/string.rb +0 -1
  11. data/lib/active_support/core_ext/string/output_safety.rb +148 -44
  12. data/lib/active_support/core_ext/time/calculations.rb +1 -1
  13. data/lib/active_support/inflector.rb +1 -1
  14. data/lib/active_support/json/backends/yajl.rb +40 -0
  15. data/lib/active_support/json/decoding.rb +16 -1
  16. data/lib/active_support/json/encoding.rb +11 -2
  17. data/lib/active_support/ordered_hash.rb +24 -1
  18. data/lib/active_support/vendor.rb +10 -2
  19. data/lib/active_support/vendor/i18n-0.3.3/CHANGELOG.textile +76 -0
  20. data/lib/active_support/vendor/{i18n-0.1.3 → i18n-0.3.3}/MIT-LICENSE +0 -0
  21. data/lib/active_support/vendor/i18n-0.3.3/README.textile +81 -0
  22. data/lib/active_support/vendor/i18n-0.3.3/Rakefile +24 -0
  23. data/lib/active_support/vendor/i18n-0.3.3/benchmark/example.yml +144 -0
  24. data/lib/active_support/vendor/i18n-0.3.3/benchmark/run.rb +71 -0
  25. data/lib/active_support/vendor/i18n-0.3.3/contributors.txt +17 -0
  26. data/lib/active_support/vendor/i18n-0.3.3/i18n.gemspec +165 -0
  27. data/lib/active_support/vendor/i18n-0.3.3/init.rb +1 -0
  28. data/lib/active_support/vendor/{i18n-0.1.3 → i18n-0.3.3}/lib/i18n.rb +99 -21
  29. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend.rb +17 -0
  30. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/active_record.rb +70 -0
  31. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/active_record/missing.rb +67 -0
  32. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/active_record/store_procs.rb +38 -0
  33. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/active_record/translation.rb +83 -0
  34. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/base.rb +259 -0
  35. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/cache.rb +75 -0
  36. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/cascade.rb +44 -0
  37. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/chain.rb +74 -0
  38. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/fallbacks.rb +52 -0
  39. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/fast.rb +68 -0
  40. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/gettext.rb +75 -0
  41. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/helpers.rb +80 -0
  42. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/interpolation_compiler.rb +119 -0
  43. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/metadata.rb +73 -0
  44. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/pluralization.rb +57 -0
  45. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/backend/simple.rb +22 -0
  46. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/core_ext/object/meta_class.rb +5 -0
  47. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/core_ext/string/interpolate.rb +99 -0
  48. data/lib/active_support/vendor/{i18n-0.1.3 → i18n-0.3.3}/lib/i18n/exceptions.rb +14 -6
  49. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/gettext.rb +25 -0
  50. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/helpers.rb +5 -0
  51. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/helpers/gettext.rb +64 -0
  52. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale.rb +6 -0
  53. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/fallbacks.rb +98 -0
  54. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/tag.rb +28 -0
  55. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/tag/parents.rb +24 -0
  56. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/tag/rfc4646.rb +76 -0
  57. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/locale/tag/simple.rb +41 -0
  58. data/lib/active_support/vendor/i18n-0.3.3/lib/i18n/version.rb +3 -0
  59. data/lib/active_support/vendor/i18n-0.3.3/test/all.rb +8 -0
  60. data/lib/active_support/vendor/i18n-0.3.3/test/api/basics.rb +15 -0
  61. data/lib/active_support/vendor/i18n-0.3.3/test/api/defaults.rb +40 -0
  62. data/lib/active_support/vendor/i18n-0.3.3/test/api/interpolation.rb +92 -0
  63. data/lib/active_support/vendor/i18n-0.3.3/test/api/link.rb +55 -0
  64. data/lib/active_support/vendor/i18n-0.3.3/test/api/localization/date.rb +91 -0
  65. data/lib/active_support/vendor/i18n-0.3.3/test/api/localization/date_time.rb +90 -0
  66. data/lib/active_support/vendor/i18n-0.3.3/test/api/localization/procs.rb +54 -0
  67. data/lib/active_support/vendor/i18n-0.3.3/test/api/localization/time.rb +84 -0
  68. data/lib/active_support/vendor/i18n-0.3.3/test/api/lookup.rb +45 -0
  69. data/lib/active_support/vendor/i18n-0.3.3/test/api/pluralization.rb +35 -0
  70. data/lib/active_support/vendor/i18n-0.3.3/test/api/procs.rb +40 -0
  71. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/active_record_test.rb +29 -0
  72. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/all_features_test.rb +40 -0
  73. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/cascade_test.rb +31 -0
  74. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/chain_test.rb +26 -0
  75. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/fallbacks_test.rb +33 -0
  76. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/fast_test.rb +31 -0
  77. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/pluralization_test.rb +33 -0
  78. data/lib/active_support/vendor/i18n-0.3.3/test/cases/api/simple_test.rb +21 -0
  79. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/active_record/missing_test.rb +60 -0
  80. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/active_record_test.rb +52 -0
  81. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/cache_test.rb +72 -0
  82. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/cascade_test.rb +66 -0
  83. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/chain_test.rb +64 -0
  84. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/fallbacks_test.rb +57 -0
  85. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/fast_test.rb +50 -0
  86. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/helpers_test.rb +26 -0
  87. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/interpolation_compiler_test.rb +107 -0
  88. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/metadata_test.rb +67 -0
  89. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/pluralization_test.rb +43 -0
  90. data/lib/active_support/vendor/i18n-0.3.3/test/cases/backend/simple_test.rb +77 -0
  91. data/lib/active_support/vendor/i18n-0.3.3/test/cases/core_ext/string/interpolate_test.rb +94 -0
  92. data/lib/active_support/vendor/i18n-0.3.3/test/cases/gettext/api_test.rb +201 -0
  93. data/lib/active_support/vendor/i18n-0.3.3/test/cases/gettext/backend_test.rb +91 -0
  94. data/lib/active_support/vendor/{i18n-0.1.3/test → i18n-0.3.3/test/cases}/i18n_exceptions_test.rb +8 -10
  95. data/lib/active_support/vendor/i18n-0.3.3/test/cases/i18n_load_path_test.rb +23 -0
  96. data/lib/active_support/vendor/i18n-0.3.3/test/cases/i18n_test.rb +172 -0
  97. data/lib/active_support/vendor/i18n-0.3.3/test/cases/locale/fallbacks_test.rb +126 -0
  98. data/lib/active_support/vendor/i18n-0.3.3/test/cases/locale/tag/rfc4646_test.rb +143 -0
  99. data/lib/active_support/vendor/i18n-0.3.3/test/cases/locale/tag/simple_test.rb +33 -0
  100. data/lib/active_support/vendor/i18n-0.3.3/test/fixtures/locales/de.po +72 -0
  101. data/lib/active_support/vendor/i18n-0.3.3/test/fixtures/locales/en.rb +3 -0
  102. data/lib/active_support/vendor/i18n-0.3.3/test/fixtures/locales/en.yml +3 -0
  103. data/lib/active_support/vendor/i18n-0.3.3/test/fixtures/locales/plurals.rb +113 -0
  104. data/lib/active_support/vendor/i18n-0.3.3/test/test_helper.rb +100 -0
  105. data/lib/active_support/vendor/i18n-0.3.3/vendor/po_parser.rb +329 -0
  106. data/lib/active_support/version.rb +1 -1
  107. data/lib/active_support/whiny_nil.rb +1 -1
  108. data/lib/active_support/xml_mini/libxml.rb +23 -83
  109. data/lib/active_support/xml_mini/libxmlsax.rb +74 -0
  110. data/lib/active_support/xml_mini/nokogiri.rb +25 -22
  111. data/lib/active_support/xml_mini/nokogirisax.rb +73 -0
  112. metadata +108 -20
  113. data/lib/active_support/vendor/i18n-0.1.3/README.textile +0 -20
  114. data/lib/active_support/vendor/i18n-0.1.3/Rakefile +0 -5
  115. data/lib/active_support/vendor/i18n-0.1.3/i18n.gemspec +0 -27
  116. data/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb +0 -214
  117. data/lib/active_support/vendor/i18n-0.1.3/test/all.rb +0 -5
  118. data/lib/active_support/vendor/i18n-0.1.3/test/i18n_test.rb +0 -124
  119. data/lib/active_support/vendor/i18n-0.1.3/test/locale/en.rb +0 -1
  120. data/lib/active_support/vendor/i18n-0.1.3/test/locale/en.yml +0 -3
  121. data/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb +0 -567
@@ -0,0 +1,17 @@
1
+ Akira Matsuda
2
+ Andrew Briening
3
+ Clemens Kofler
4
+ Frederick Cheung
5
+ Jeremy Kemper
6
+ Josh Harvey
7
+ José Valim
8
+ Lawrence Pit
9
+ Luca Guidi
10
+ M4SSIVE
11
+ Marko Seppae
12
+ Mathias Meyer
13
+ Matt Aimonetti
14
+ Michael Lang
15
+ Sven Fuchs
16
+ Theo Cushion
17
+ Yaroslav Markin
@@ -0,0 +1,165 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{i18n}
8
+ s.version = "0.3.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sven Fuchs", "Joshua Harvey", "Matt Aimonetti", "Stephan Soller", "Saimon Moore"]
12
+ s.date = %q{2009-12-29}
13
+ s.description = %q{Add Internationalization support to your Ruby application.}
14
+ s.email = %q{rails-i18n@googlegroups.com}
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ "CHANGELOG.textile",
20
+ "MIT-LICENSE",
21
+ "README.textile",
22
+ "Rakefile",
23
+ "lib/i18n.rb",
24
+ "lib/i18n/backend.rb",
25
+ "lib/i18n/backend/active_record.rb",
26
+ "lib/i18n/backend/active_record/missing.rb",
27
+ "lib/i18n/backend/active_record/store_procs.rb",
28
+ "lib/i18n/backend/active_record/translation.rb",
29
+ "lib/i18n/backend/base.rb",
30
+ "lib/i18n/backend/cache.rb",
31
+ "lib/i18n/backend/cascade.rb",
32
+ "lib/i18n/backend/chain.rb",
33
+ "lib/i18n/backend/fallbacks.rb",
34
+ "lib/i18n/backend/fast.rb",
35
+ "lib/i18n/backend/gettext.rb",
36
+ "lib/i18n/backend/helpers.rb",
37
+ "lib/i18n/backend/interpolation_compiler.rb",
38
+ "lib/i18n/backend/metadata.rb",
39
+ "lib/i18n/backend/pluralization.rb",
40
+ "lib/i18n/backend/simple.rb",
41
+ "lib/i18n/core_ext/object/meta_class.rb",
42
+ "lib/i18n/core_ext/string/interpolate.rb",
43
+ "lib/i18n/exceptions.rb",
44
+ "lib/i18n/gettext.rb",
45
+ "lib/i18n/helpers.rb",
46
+ "lib/i18n/helpers/gettext.rb",
47
+ "lib/i18n/locale.rb",
48
+ "lib/i18n/locale/fallbacks.rb",
49
+ "lib/i18n/locale/tag.rb",
50
+ "lib/i18n/locale/tag/parents.rb",
51
+ "lib/i18n/locale/tag/rfc4646.rb",
52
+ "lib/i18n/locale/tag/simple.rb",
53
+ "lib/i18n/version.rb",
54
+ "test/all.rb",
55
+ "test/api/basics.rb",
56
+ "test/api/defaults.rb",
57
+ "test/api/interpolation.rb",
58
+ "test/api/link.rb",
59
+ "test/api/localization/date.rb",
60
+ "test/api/localization/date_time.rb",
61
+ "test/api/localization/procs.rb",
62
+ "test/api/localization/time.rb",
63
+ "test/api/lookup.rb",
64
+ "test/api/pluralization.rb",
65
+ "test/api/procs.rb",
66
+ "test/cases/api/active_record_test.rb",
67
+ "test/cases/api/all_features_test.rb",
68
+ "test/cases/api/cascade_test.rb",
69
+ "test/cases/api/chain_test.rb",
70
+ "test/cases/api/fallbacks_test.rb",
71
+ "test/cases/api/fast_test.rb",
72
+ "test/cases/api/pluralization_test.rb",
73
+ "test/cases/api/simple_test.rb",
74
+ "test/cases/backend/active_record/missing_test.rb",
75
+ "test/cases/backend/active_record_test.rb",
76
+ "test/cases/backend/cache_test.rb",
77
+ "test/cases/backend/cascade_test.rb",
78
+ "test/cases/backend/chain_test.rb",
79
+ "test/cases/backend/fallbacks_test.rb",
80
+ "test/cases/backend/fast_test.rb",
81
+ "test/cases/backend/helpers_test.rb",
82
+ "test/cases/backend/interpolation_compiler_test.rb",
83
+ "test/cases/backend/metadata_test.rb",
84
+ "test/cases/backend/pluralization_test.rb",
85
+ "test/cases/backend/simple_test.rb",
86
+ "test/cases/core_ext/string/interpolate_test.rb",
87
+ "test/cases/gettext/api_test.rb",
88
+ "test/cases/gettext/backend_test.rb",
89
+ "test/cases/i18n_exceptions_test.rb",
90
+ "test/cases/i18n_load_path_test.rb",
91
+ "test/cases/i18n_test.rb",
92
+ "test/cases/locale/fallbacks_test.rb",
93
+ "test/cases/locale/tag/rfc4646_test.rb",
94
+ "test/cases/locale/tag/simple_test.rb",
95
+ "test/fixtures/locales/de.po",
96
+ "test/fixtures/locales/en.rb",
97
+ "test/fixtures/locales/en.yml",
98
+ "test/fixtures/locales/plurals.rb",
99
+ "test/test_helper.rb",
100
+ "vendor/po_parser.rb"
101
+ ]
102
+ s.homepage = %q{http://rails-i18n.org}
103
+ s.rdoc_options = ["--charset=UTF-8"]
104
+ s.require_paths = ["lib"]
105
+ s.rubyforge_project = %q{i18n}
106
+ s.rubygems_version = %q{1.3.5}
107
+ s.summary = %q{New wave Internationalization support for Ruby}
108
+ s.test_files = [
109
+ "test/all.rb",
110
+ "test/api/basics.rb",
111
+ "test/api/defaults.rb",
112
+ "test/api/interpolation.rb",
113
+ "test/api/link.rb",
114
+ "test/api/localization/date.rb",
115
+ "test/api/localization/date_time.rb",
116
+ "test/api/localization/procs.rb",
117
+ "test/api/localization/time.rb",
118
+ "test/api/lookup.rb",
119
+ "test/api/pluralization.rb",
120
+ "test/api/procs.rb",
121
+ "test/cases/api/active_record_test.rb",
122
+ "test/cases/api/all_features_test.rb",
123
+ "test/cases/api/cascade_test.rb",
124
+ "test/cases/api/chain_test.rb",
125
+ "test/cases/api/fallbacks_test.rb",
126
+ "test/cases/api/fast_test.rb",
127
+ "test/cases/api/pluralization_test.rb",
128
+ "test/cases/api/simple_test.rb",
129
+ "test/cases/backend/active_record/missing_test.rb",
130
+ "test/cases/backend/active_record_test.rb",
131
+ "test/cases/backend/cache_test.rb",
132
+ "test/cases/backend/cascade_test.rb",
133
+ "test/cases/backend/chain_test.rb",
134
+ "test/cases/backend/fallbacks_test.rb",
135
+ "test/cases/backend/fast_test.rb",
136
+ "test/cases/backend/helpers_test.rb",
137
+ "test/cases/backend/interpolation_compiler_test.rb",
138
+ "test/cases/backend/metadata_test.rb",
139
+ "test/cases/backend/pluralization_test.rb",
140
+ "test/cases/backend/simple_test.rb",
141
+ "test/cases/core_ext/string/interpolate_test.rb",
142
+ "test/cases/gettext/api_test.rb",
143
+ "test/cases/gettext/backend_test.rb",
144
+ "test/cases/i18n_exceptions_test.rb",
145
+ "test/cases/i18n_load_path_test.rb",
146
+ "test/cases/i18n_test.rb",
147
+ "test/cases/locale/fallbacks_test.rb",
148
+ "test/cases/locale/tag/rfc4646_test.rb",
149
+ "test/cases/locale/tag/simple_test.rb",
150
+ "test/fixtures/locales/en.rb",
151
+ "test/fixtures/locales/plurals.rb",
152
+ "test/test_helper.rb"
153
+ ]
154
+
155
+ if s.respond_to? :specification_version then
156
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
157
+ s.specification_version = 3
158
+
159
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
160
+ else
161
+ end
162
+ else
163
+ end
164
+ end
165
+
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/lib/i18n'
@@ -1,17 +1,24 @@
1
- # Authors:: Matt Aimonetti (http://railsontherun.com/),
2
- # Sven Fuchs (http://www.artweb-design.de),
1
+ # encoding: utf-8
2
+
3
+ # Authors:: Sven Fuchs (http://www.artweb-design.de),
3
4
  # Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
5
+ # Stephan Soller (http://www.arkanis-development.de/),
4
6
  # Saimon Moore (http://saimonmoore.net),
5
- # Stephan Soller (http://www.arkanis-development.de/)
7
+ # Matt Aimonetti (http://railsontherun.com/)
6
8
  # Copyright:: Copyright (c) 2008 The Ruby i18n Team
7
9
  # License:: MIT
8
- require 'i18n/backend/simple'
9
10
  require 'i18n/exceptions'
11
+ require 'i18n/core_ext/string/interpolate'
10
12
 
11
13
  module I18n
14
+ autoload :Backend, 'i18n/backend'
15
+ autoload :Helpers, 'i18n/helpers'
16
+ autoload :Locale, 'i18n/locale'
17
+
12
18
  @@backend = nil
13
19
  @@load_path = nil
14
- @@default_locale = :'en'
20
+ @@default_locale = :en
21
+ @@default_separator = '.'
15
22
  @@exception_handler = :default_exception_handler
16
23
 
17
24
  class << self
@@ -32,7 +39,7 @@ module I18n
32
39
 
33
40
  # Sets the current default locale. Used to set a custom default locale.
34
41
  def default_locale=(locale)
35
- @@default_locale = locale
42
+ @@default_locale = locale.to_sym rescue nil
36
43
  end
37
44
 
38
45
  # Returns the current locale. Defaults to I18n.default_locale.
@@ -42,12 +49,29 @@ module I18n
42
49
 
43
50
  # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
44
51
  def locale=(locale)
45
- Thread.current[:locale] = locale
52
+ Thread.current[:locale] = locale.to_sym rescue nil
46
53
  end
47
54
 
48
- # Returns an array of locales for which translations are available
55
+ # Returns an array of locales for which translations are available.
56
+ # Unless you explicitely set the these through I18n.available_locales=
57
+ # the call will be delegated to the backend and memoized on the I18n module.
49
58
  def available_locales
50
- backend.available_locales
59
+ @@available_locales ||= backend.available_locales
60
+ end
61
+
62
+ # Sets the available locales.
63
+ def available_locales=(locales)
64
+ @@available_locales = locales
65
+ end
66
+
67
+ # Returns the current default scope separator. Defaults to '.'
68
+ def default_separator
69
+ @@default_separator
70
+ end
71
+
72
+ # Sets the current default scope separator.
73
+ def default_separator=(separator)
74
+ @@default_separator = separator
51
75
  end
52
76
 
53
77
  # Sets the exception handler.
@@ -150,7 +174,7 @@ module I18n
150
174
  # or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
151
175
  # I18n.t :foo, :default => [:bar, 'default']
152
176
  #
153
- # <b>BULK LOOKUP</b>
177
+ # *BULK LOOKUP*
154
178
  #
155
179
  # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
156
180
  # I18n.t [:foo, :bar]
@@ -160,15 +184,39 @@ module I18n
160
184
  #
161
185
  # Which is the same as using a scope option:
162
186
  # I18n.t [:foo, :bar], :scope => :baz
163
- def translate(key, options = {})
164
- locale = options.delete(:locale) || I18n.locale
165
- backend.translate(locale, key, options)
166
- rescue I18n::ArgumentError => e
167
- raise e if options[:raise]
168
- send(@@exception_handler, e, locale, key, options)
187
+ #
188
+ # *LAMBDAS*
189
+ #
190
+ # Both translations and defaults can be given as Ruby lambdas. Lambdas will be
191
+ # called and passed the key and options.
192
+ #
193
+ # E.g. assuming the key <tt>:salutation</tt> resolves to:
194
+ # lambda { |key, options| options[:gender] == 'm' ? "Mr. {{options[:name]}}" : "Mrs. {{options[:name]}}" }
195
+ #
196
+ # Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
197
+ #
198
+ # It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
199
+ # a cache layer is put in front of I18n.translate it will generate a cache key
200
+ # from the argument values passed to #translate. Therefor your lambdas should
201
+ # always return the same translations/values per unique combination of argument
202
+ # values.
203
+ def translate(*args)
204
+ options = args.pop if args.last.is_a?(Hash)
205
+ key = args.shift
206
+ locale = options && options.delete(:locale) || I18n.locale
207
+ raises = options && options.delete(:raise)
208
+ backend.translate(locale, key, options || {})
209
+ rescue I18n::ArgumentError => exception
210
+ raise exception if raises
211
+ handle_exception(exception, locale, key, options)
169
212
  end
170
213
  alias :t :translate
171
214
 
215
+ def translate!(key, options = {})
216
+ translate(key, options.merge( :raise => true ))
217
+ end
218
+ alias :t! :translate!
219
+
172
220
  # Localizes certain objects, such as dates and numbers to local formatting.
173
221
  def localize(object, options = {})
174
222
  locale = options[:locale] || I18n.locale
@@ -177,7 +225,10 @@ module I18n
177
225
  end
178
226
  alias :l :localize
179
227
 
180
- protected
228
+ # making these private until Ruby 1.9.2 can send to protected methods again
229
+ # see http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=24280
230
+ private
231
+
181
232
  # Handles exceptions raised in the backend. All exceptions except for
182
233
  # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
183
234
  # was caught and the option :raise is not set the handler returns an error
@@ -187,13 +238,40 @@ module I18n
187
238
  raise exception
188
239
  end
189
240
 
241
+ # Any exceptions thrown in translate will be sent to the @@exception_handler
242
+ # which can be a Symbol, a Proc or any other Object.
243
+ #
244
+ # If exception_handler is a Symbol then it will simply be sent to I18n as
245
+ # a method call. A Proc will simply be called. In any other case the
246
+ # method #call will be called on the exception_handler object.
247
+ #
248
+ # Examples:
249
+ #
250
+ # I18n.exception_handler = :default_exception_handler # this is the default
251
+ # I18n.default_exception_handler(exception, locale, key, options) # will be called like this
252
+ #
253
+ # I18n.exception_handler = lambda { |*args| ... } # a lambda
254
+ # I18n.exception_handler.call(exception, locale, key, options) # will be called like this
255
+ #
256
+ # I18n.exception_handler = I18nExceptionHandler.new # an object
257
+ # I18n.exception_handler.call(exception, locale, key, options) # will be called like this
258
+ def handle_exception(exception, locale, key, options)
259
+ case @@exception_handler
260
+ when Symbol
261
+ send(@@exception_handler, exception, locale, key, options)
262
+ else
263
+ @@exception_handler.call(exception, locale, key, options)
264
+ end
265
+ end
266
+
190
267
  # Merges the given locale, key and scope into a single array of keys.
191
268
  # Splits keys that contain dots into multiple keys. Makes sure all
192
269
  # keys are Symbols.
193
- def normalize_translation_keys(locale, key, scope)
194
- keys = [locale] + Array(scope) + [key]
195
- keys = keys.map { |k| k.to_s.split(/\./) }
196
- keys.flatten.map { |k| k.to_sym }
270
+ def normalize_translation_keys(locale, key, scope, separator = nil)
271
+ keys = [locale] + Array(scope) + Array(key)
272
+ keys = keys.map { |k| k.to_s.split(separator || I18n.default_separator) }
273
+ keys = keys.flatten - ['']
274
+ keys.map { |k| k.to_sym }
197
275
  end
198
276
  end
199
277
  end
@@ -0,0 +1,17 @@
1
+ module I18n
2
+ module Backend
3
+ autoload :ActiveRecord, 'i18n/backend/active_record'
4
+ autoload :Base, 'i18n/backend/base'
5
+ autoload :Cache, 'i18n/backend/cache'
6
+ autoload :Cascade, 'i18n/backend/cascade'
7
+ autoload :Chain, 'i18n/backend/chain'
8
+ autoload :Fallbacks, 'i18n/backend/fallbacks'
9
+ autoload :Fast, 'i18n/backend/fast'
10
+ autoload :Gettext, 'i18n/backend/gettext'
11
+ autoload :Helpers, 'i18n/backend/helpers'
12
+ autoload :InterpolationCompiler, 'i18n/backend/interpolation_compiler'
13
+ autoload :Metadata, 'i18n/backend/metadata'
14
+ autoload :Pluralization, 'i18n/backend/pluralization'
15
+ autoload :Simple, 'i18n/backend/simple'
16
+ end
17
+ end
@@ -0,0 +1,70 @@
1
+ require 'i18n/backend/base'
2
+ require 'i18n/backend/active_record/translation'
3
+
4
+ #
5
+ # This backend reads translations from a Translations table in environment database. Note that the database
6
+ # will not automatically be prepopulated with missing keys. You can achieve this effect with the ActiveRecordMissing backend,
7
+ # as the following example shows:
8
+ #
9
+ # I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18.backend, I18n::Backend::ActiveRecordMissing.new)
10
+ #
11
+ module I18n
12
+ module Backend
13
+ class ActiveRecord
14
+ autoload :Missing, 'i18n/backend/active_record/missing'
15
+ autoload :StoreProcs, 'i18n/backend/active_record/store_procs'
16
+ autoload :Translation, 'i18n/backend/active_record/translation'
17
+
18
+ include Base
19
+
20
+ def reload!
21
+ end
22
+
23
+ def store_translations(locale, data, options = {})
24
+ separator = options[:separator] || I18n.default_separator
25
+ wind_keys(data, separator).each do |key, v|
26
+ Translation.locale(locale).lookup(expand_keys(key, separator), separator).delete_all
27
+ Translation.create(:locale => locale.to_s, :key => key.to_s, :value => v)
28
+ end
29
+ end
30
+
31
+ def available_locales
32
+ begin
33
+ Translation.available_locales
34
+ rescue ::ActiveRecord::StatementInvalid
35
+ []
36
+ end
37
+ end
38
+
39
+ protected
40
+
41
+ def lookup(locale, key, scope = [], separator = nil)
42
+ return unless key
43
+
44
+ separator ||= I18n.default_separator
45
+ key = (Array(scope) + Array(key)).join(separator)
46
+
47
+ result = Translation.locale(locale).lookup(key, separator).all
48
+ if result.empty?
49
+ return nil
50
+ elsif result.first.key == key
51
+ return result.first.value
52
+ else
53
+ chop_range = (key.size + separator.size)..-1
54
+ result = result.inject({}) do |hash, r|
55
+ hash[r.key.slice(chop_range)] = r.value
56
+ hash
57
+ end
58
+ deep_symbolize_keys(unwind_keys(result, separator))
59
+ end
60
+ end
61
+
62
+ # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
63
+ def expand_keys(key, separator = I18n.default_separator)
64
+ key.to_s.split(separator).inject([]) do |keys, key|
65
+ keys << [keys.last, key].compact.join(separator)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end