rich_i18n 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/.gitignore +2 -0
  2. data/CHANGELOG +19 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.textile +269 -0
  5. data/Rakefile +42 -0
  6. data/VERSION +1 -0
  7. data/init.rb +1 -0
  8. data/install.rb +1 -0
  9. data/lib/rich/i18n.rb +11 -0
  10. data/lib/rich/i18n/actionpack.rb +4 -0
  11. data/lib/rich/i18n/actionpack/action_controller/dispatcher.rb +12 -0
  12. data/lib/rich/i18n/actionpack/action_view/base.rb +13 -0
  13. data/lib/rich/i18n/actionpack/action_view/sanitizor.rb +61 -0
  14. data/lib/rich/i18n/core.rb +8 -0
  15. data/lib/rich/i18n/core/array.rb +8 -0
  16. data/lib/rich/i18n/core/array/merging.rb +29 -0
  17. data/lib/rich/i18n/core/enriched_string.rb +28 -0
  18. data/lib/rich/i18n/core/enumerable/methods.rb +16 -0
  19. data/lib/rich/i18n/core/erb.rb +6 -0
  20. data/lib/rich/i18n/core/erb/output.rb +22 -0
  21. data/lib/rich/i18n/core/hash.rb +6 -0
  22. data/lib/rich/i18n/core/nil_class.rb +8 -0
  23. data/lib/rich/i18n/core/object.rb +6 -0
  24. data/lib/rich/i18n/core/object/output.rb +20 -0
  25. data/lib/rich/i18n/core/string.rb +10 -0
  26. data/lib/rich/i18n/core/string/enrichments.rb +68 -0
  27. data/lib/rich/i18n/core/string/inflections.rb +42 -0
  28. data/lib/rich/i18n/core/string/internationalization.rb +82 -0
  29. data/lib/rich/i18n/core/symbol.rb +6 -0
  30. data/lib/rich/i18n/core/symbol/internationalization.rb +16 -0
  31. data/lib/rich/i18n/engine.rb +31 -0
  32. data/lib/rich/i18n/formtastic.rb +51 -0
  33. data/lib/rich_i18n.rb +5 -0
  34. data/locales/nl.yml +14 -0
  35. data/rails/init.rb +1 -0
  36. data/tasks/rich_i18n_tasks.rake +11 -0
  37. data/test/core/string/inflections_test.rb +42 -0
  38. data/test/core/string/internationalization_test.rb +23 -0
  39. data/test/engine_test.rb +20 -0
  40. data/test/locales/nl/internationalization_test.rb +47 -0
  41. data/test/setup.rb +14 -0
  42. data/test/test_helper.rb +5 -0
  43. data/uninstall.rb +1 -0
  44. metadata +136 -0
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ pkg/*
@@ -0,0 +1,19 @@
1
+ = Rich-i18n CHANGELOG
2
+
3
+ == Version 1.0.3 (July 22, 2010)
4
+
5
+ * Specified the locales with relative paths
6
+ * Fixed error during gem initialization
7
+
8
+ == Version 1.0.2 (July 21, 2010)
9
+
10
+ * Added 'filled?' to NilClass
11
+ * Moved 'meta_data' from EnrichedString to String
12
+
13
+ == Version 1.0.1 (July 21, 2010)
14
+
15
+ * Fixed internationalization bug (caused by pluralization assumptions)
16
+
17
+ == Version 1.0.0 (July 20, 2010)
18
+
19
+ * Initial release (extracted Rich-i18n from E9s)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Paul Engel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,269 @@
1
+
2
+ h1. Rich-i18n
3
+
4
+ Enrichments (e9s) module for internationalization (i18n)
5
+
6
+ h2. Introduction
7
+
8
+ Rich-i18n is a module of E9s ("http://github.com/archan937/e9s":http://github.com/archan937/e9s) which enriches I18n, Formtastic, the String and Symbol classes. This simplifies internationalization of your Rails application making a Rails developers life much easier. A list of features:
9
+
10
+ h3. I18n
11
+
12
+ * @Default values@ - Use the translation key (or a portion) as default value: @"continue".t@ returns @"continue"@ and @"text.Welcome_to_our_site".t@ returns @"Welcome to our site"@
13
+ * @An easy interface@ - Just call the @t@ method on string or symbols to translate
14
+ * @Combine translations@ - Joining keys with spaces combines translations: @"Male / Female".t@ returns @"Man / Vrouw"@ in Dutch
15
+ * @Preserve i18n meta data@ - Rich-i18n preserves the key, the actual used I18n key and the actual translation which you can enquire (this can come in handy when implementing a CMS)
16
+
17
+ h3. Formtastic
18
+
19
+ * @Labels, seatholders and default values@ - Not only translate labels, but also hint text (so called @seatholders@) and even translate default values
20
+ * @Unobtrusive implementation@ - Translate labels and seatholders unobtrusively, in other words: leave your @semantic_form_for@ (view) code completely untouched
21
+ * @Specific translations@ - Not only specify general translations for labels and seatholders, but make them model or even form specific
22
+
23
+ h3. Inflections
24
+
25
+ * @Preserve character casing@ - Rich-i18n preserves the casing in your translations: @"save".t@ returns @"bewaar"@, @"Save".t@ returns @"Bewaar"@ and @"SAVE".t@ returns @"BEWAAR"@ in Dutch
26
+
27
+ h3. More available features when using E9s (enrichments)
28
+
29
+ * @Localized pluralization@ - Translations only in singular form are sufficient enough as E9s can pluralize in foreign languages
30
+ * @An easy interface for localized pluralizations@ - Just call the @pl@ method on string or symbols to pluralize
31
+ * @Preserve pluralization@ - E9s singularizes or pluralizes your translations depending on the key: @"house".t@ returns @"huis"@ and @"Houses".t@ returns @"Huizen"@ in Dutch
32
+
33
+ *Note*: keep in mind that you will have to use E9s to do this, please visit "http://github.com/archan937/e9s":http://github.com/archan937/e9s for more information.
34
+
35
+ h2. Installation
36
+
37
+ h3. Using Rich-i18n as gem
38
+
39
+ Install the Rich-i18n gem:
40
+
41
+ <pre>
42
+ sudo gem install rich_i18n
43
+ </pre>
44
+
45
+ Add rich_i18n in environment.rb as a gem dependency:
46
+
47
+ <pre>
48
+ config.gem "rich_i18n"
49
+ </pre>
50
+
51
+ h3. Using Rich-i18n as plugin
52
+
53
+ Install the Rich-i18n plugin:
54
+
55
+ <pre>
56
+ ./script/plugin install git://github.com/archan937/rich_i18n.git
57
+ </pre>
58
+
59
+ Install i18n if you haven't already:
60
+
61
+ <pre>
62
+ sudo gem install i18n
63
+ </pre>
64
+
65
+ h3. Optional
66
+
67
+ You will have to install the @Formtastic@ plugin when translating @labels@ and @seatholders@ with *Rich-i18n as plugin*:
68
+
69
+ <pre>
70
+ ./script/plugin install git://github.com/justinfrench/formtastic.git
71
+ </pre>
72
+
73
+ When wanting to use @seatholders@, please include @seat_holder.js@ in your template:
74
+
75
+ <pre>
76
+ <script src="path/to/seat_holder.js" type="text/javascript"></script>
77
+ </pre>
78
+
79
+ *Note*: please visit "http://github.com/archan937/seat_holder":http://github.com/archan937/seat_holder for more information about SeatHolder.
80
+
81
+ h3. Testing Rich-i18n out-of-the-box
82
+
83
+ Set the default locale to @:nl@ in @environment.rb@:
84
+
85
+ <pre>
86
+ config.i18n.default_locale = :nl
87
+ </pre>
88
+
89
+ Run the Rails console:
90
+
91
+ <pre>
92
+ ./script/console
93
+ </pre>
94
+
95
+ Start translating in Dutch:
96
+
97
+ <pre>
98
+ >> "Male / Female".t
99
+ => "Man / Vrouw"
100
+ </pre>
101
+
102
+ h2. Usage
103
+
104
+ Just call the @t@ method on string or symbols to translate using Rich-i18n.
105
+
106
+ h3. Populating config/locales
107
+
108
+ At default, I18n uses @I18n::Backend::Simple@ of which translations are stored within YAML files located in @config/locales@. When adding a new language, it is adviced to copy a YAML file from "http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale in which you can add your translations. Of course, you can also use other I18n backends like @I18n::Backend::ActiveRecord@ for translations stored in the database.
109
+
110
+ *Note*: specified in @config/locales/nl.yml@
111
+
112
+ <pre>
113
+ ---
114
+ nl:
115
+
116
+ word:
117
+ "yes": ja
118
+ "no": nee
119
+ house: huis
120
+ letter: brief
121
+ sign: teken
122
+ users: gebruikers
123
+ more: meer
124
+ </pre>
125
+
126
+ h3. String methods
127
+
128
+ Rich-i18n has enriched the String class with several inflection methods such as @upcase_first@, @cp_case@, @upcase_first!@ and @pluralize!@. Please visit "http://github.com/archan937/rich_i18n/blob/master/lib/rich/i18n/core/string/inflections.rb":http://github.com/archan937/rich_i18n/blob/master/lib/rich/i18n/core/string/inflections.rb to see all the methods.
129
+
130
+ h3. Default values and case preservation
131
+
132
+ When not specified, Rich-i18n returns a default value based on the passed key: it splits the key on @"."@ and (sort of) humanizes the last part. Sort of, because it actually replaces @"_"@ with @" "@ and it copies the casing of the key with the @cp_case@ method of the @String@ class.
133
+
134
+ h3. Combined keys
135
+
136
+ You can combine translations by using passed string containing translation keys joined with spaces.
137
+
138
+ h3. Translation meta data with EnrichedString
139
+
140
+ When translating text, you possibly want to know the @used key@, the @actual used I18n key@ and its @actual translation@. Rich-i18n preserves just that in an @EnrichedString@ which is a subclass of @String@. Calling @.meta_data@ returns a hash with the meta data:
141
+
142
+ <pre>
143
+ >> "MORE".t.class
144
+ => Rich::I18n::Core::EnrichedString
145
+ >> "MORE".t.meta_data
146
+ => {:actual_key=>"word.more", :key=>"MORE", :actual_value=>"MEER"}
147
+ </pre>
148
+
149
+ Keep in mind that combined translations are possible and fortunately EnrichedString is able to cope with that. A concatenated translation has @merged_strings@ which contains every segments:
150
+
151
+ <pre>
152
+ >> "More users".t
153
+ => "Meer gebruikers"
154
+ >> "More users".t.merged_strings
155
+ => ["Meer", "gebruikers"]
156
+ >> "More users".t.meta_data
157
+ => nil
158
+ >> "More users".t.merged_strings.first.meta_data
159
+ => {:actual_key=>"word.more", :key=>"More", :actual_value=>"Meer"}
160
+ >> "More users".t.merged_strings.last.meta_data
161
+ => {:actual_key=>"word.user", :key=>"users", :actual_value=>"gebruiker"}
162
+ >> "One".t + " " + "question".t
163
+ => "één vraag"
164
+ >> ("One".t + " " + "question".t).merged_strings
165
+ => ["één", " ", "vraag"]
166
+ </pre>
167
+
168
+ h3. String.to_output
169
+
170
+ E9s adds the @to_output@ method to the String class. This returns the an @i18n tag@ with @HTML 5 attributes@ in which the translation meta data is provided:
171
+
172
+ <pre>
173
+ >> "More users".t.to_output
174
+ => "<i18n data-actual_key=\"word.more\" data-key=\"More\" data-actual_value=\"Meer\">Meer</i18n><i18n data-actual_key=\"word.user\" data-key=\"users\" data-actual_value=\"gebruiker\">gebruikers</i18n>"
175
+ </pre>
176
+
177
+ This can be very handy when implementing a CMS in which users change translations. Please note that "http://github.com/archan937/e9s-demo":http://github.com/archan937/e9s-demo uses this feature to highlight translations. Later on this will also be used in "Rich-CMS":http://github.com/archan937/rich_cms, a gem / plugin that makes inplace translating possible (please be patient for this to be released).
178
+
179
+ h3. I18n examples
180
+
181
+ As a result of the YAML file specified above, you will get the following translations in your Rails console:
182
+
183
+ <pre>
184
+ >> "house".t
185
+ => "huis"
186
+ >> "LETTER".t
187
+ => "BRIEF"
188
+ >> "application.index.Welcome_to_our_site".t
189
+ => "Welcome to our site"
190
+ >> "Sign".t
191
+ => "Teken"
192
+ >> "MORE USERS".t
193
+ => "MEER GEBRUIKERS"
194
+ >> "Yes / No".t
195
+ => "Ja / Nee"
196
+ </pre>
197
+
198
+ h3. Labels and seatholders
199
+
200
+ You can translate @labels@ and @seatholders@ (placeholders :D) within Formtastic forms without altering its code.
201
+
202
+ *Note*: specified in @config/locales/nl.yml@
203
+
204
+ <pre>
205
+ ---
206
+ nl:
207
+
208
+ word:
209
+ password: wachtwoord
210
+
211
+ label:
212
+ user_name: gebruikersnaam
213
+ content: bericht
214
+
215
+ Question:
216
+ content: jouw vraag
217
+
218
+ Answer:
219
+ content: jouw antwoord
220
+
221
+ (search_form)
222
+ criteria: uw zoekcriteria
223
+
224
+ seatholder:
225
+ email_address: uw.naam@een.website.nl
226
+
227
+ Question:
228
+ content: Hoeveel uren zitten in een dag?
229
+
230
+ Answer:
231
+ content: 24 uur
232
+
233
+ (search_form)
234
+ criteria: '&Voorbeeld'
235
+ </pre>
236
+
237
+ h2. Contact me
238
+
239
+ For support, remarks and requests please mail me at "paul.engel@holder.nl":mailto:paul.engel@holder.nl.
240
+
241
+ h2. Credit
242
+
243
+ This Rails gem / plugin depends on:
244
+
245
+ i18n<br>
246
+ "http://github.com/svenfuchs/i18n":http://github.com/svenfuchs/i18n
247
+
248
+ Formtastic (optional)<br>
249
+ "http://github.com/justinfrench/formtastic":http://github.com/justinfrench/formtastic
250
+
251
+ SeatHolder (optional)<br>
252
+ "http://github.com/archan937/seat_holder":http://github.com/archan937/seat_holder
253
+
254
+ h2. ToDo's
255
+
256
+ * Override String interpolation (e.g. "foo #{"bar".t}") for preserving metadata (please note: by using String interpolation all translation meta data will be lost!)
257
+ * Most String inflection methods are also defined in rich_pluralization (keep it DRY)
258
+
259
+ h2. License
260
+
261
+ Copyright (c) 2010 Paul Engel, released under the MIT license
262
+
263
+ "http://holder.nl":http://holder.nl - "http://codehero.es":http://codehero.es - "http://gettopup.com":http://gettopup.com - "http://twitter.com/archan937":http://twitter.com/archan937 - "paul.engel@holder.nl":mailto:paul.engel@holder.nl
264
+
265
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
266
+
267
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
268
+
269
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ require "rake"
2
+ require "rake/testtask"
3
+ require "rake/rdoctask"
4
+
5
+ begin
6
+ require "jeweler"
7
+ Jeweler::Tasks.new do |gemspec|
8
+ gemspec.name = "rich_i18n"
9
+ gemspec.summary = "Enrichments (e9s) module for internationalization (i18n)"
10
+ gemspec.description = "Rich-i18n is a module of E9s (http://github.com/archan937/e9s) which enriches I18n, Formtastic, the String and Symbol classes. This simplifies internationalization of your Rails application making a Rails developers life much easier."
11
+ gemspec.email = "paul.engel@holder.nl"
12
+ gemspec.homepage = "http://github.com/archan937/rich_i18n"
13
+ gemspec.author = "Paul Engel"
14
+
15
+ gemspec.add_dependency "i18n"
16
+ gemspec.add_dependency "formtastic"
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ desc "Default: run unit tests."
24
+ task :default => :test
25
+
26
+ desc "Test the rich_i18n plugin."
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << "lib"
29
+ t.libs << "test"
30
+ t.pattern = "test/**/*_test.rb"
31
+ t.verbose = true
32
+ end
33
+
34
+ desc "Generate documentation for the rich_i18n plugin."
35
+ Rake::RDocTask.new(:rdoc) do |rdoc|
36
+ rdoc.rdoc_dir = "rdoc"
37
+ rdoc.title = "Rich-i18n"
38
+ rdoc.options << "--line-numbers" << "--inline-source"
39
+ rdoc.rdoc_files.include "README"
40
+ rdoc.rdoc_files.include "MIT-LICENSE"
41
+ rdoc.rdoc_files.include "lib/**/*.rb"
42
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.3
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "rails", "init")
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,11 @@
1
+
2
+ module Rich
3
+ module I18n
4
+ extend self
5
+
6
+ def method_missing(method, *args)
7
+ ::I18n.send method, *args
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+
2
+ require File.join(File.dirname(__FILE__), "actionpack", "action_controller", "dispatcher.rb")
3
+ require File.join(File.dirname(__FILE__), "actionpack", "action_view" , "base.rb" )
4
+ require File.join(File.dirname(__FILE__), "actionpack", "action_view" , "sanitizor.rb" )
@@ -0,0 +1,12 @@
1
+
2
+ unless defined?(Rich::I18n::CONTROLLER_HOOKED)
3
+
4
+ ActionController::Dispatcher.to_prepare :rich_i18n_formtastic do
5
+ if defined? Formtastic::SemanticFormBuilder
6
+ Formtastic::SemanticFormBuilder.send :include, Rich::I18n::Formtastic unless Formtastic::SemanticFormBuilder.included_modules.include? Rich::I18n::Formtastic
7
+ end
8
+ end
9
+
10
+ Rich::I18n::CONTROLLER_HOOKED = true
11
+
12
+ end
@@ -0,0 +1,13 @@
1
+
2
+ module ActionView
3
+ class Base
4
+
5
+ def render_with_rich_i18n(*args, &block)
6
+ (html = render_without_rich_i18n(*args, &block)).include?("<i18n ") ?
7
+ ::Rich::I18n::Actionpack::ActionView::Sanitizor.sanitize_html(html) :
8
+ html
9
+ end
10
+ alias_method_chain :render, :rich_i18n
11
+
12
+ end
13
+ end
@@ -0,0 +1,61 @@
1
+
2
+ module Rich
3
+ module I18n
4
+ module Actionpack
5
+ module ActionView
6
+ module Sanitizor
7
+ extend self
8
+
9
+ require "hpricot"
10
+
11
+ def sanitize_html(html)
12
+ return html unless (html || "").include?("<i18n ")
13
+
14
+ doc = Hpricot html
15
+
16
+ (doc/"head i18n").each do |i18n|
17
+ i18n.swap i18n.inner_html
18
+ end
19
+
20
+ (doc/"i18n").each do |i18n|
21
+ elem = Hpricot::Elem.new "span", i18n.raw_attributes.merge({:class => "i18n"})
22
+ elem.inner_html = i18n.inner_html
23
+ i18n.swap elem.to_html
24
+ end
25
+
26
+ (doc/"input").each do |input|
27
+ sanitize_input(input)
28
+ end
29
+
30
+ (doc/"textarea").each do |input|
31
+ sanitize_input(input)
32
+ end
33
+
34
+ doc.to_html
35
+ rescue
36
+ html.gsub(/(\<i18n[^\>]+\>)|(\<\/i18n\>)/, "")
37
+ end
38
+
39
+ private
40
+
41
+ def sanitize_input(input)
42
+ %w(value seatholder).each do |input_attr|
43
+ next unless input.attributes[input_attr].include?("<i18n ")
44
+
45
+ i18n = Hpricot(input.attributes[input_attr]).children.first
46
+ i18n.raw_attributes.each do |key, value|
47
+ input.attributes[key] = value
48
+ end
49
+
50
+ input.attributes[input_attr] = i18n.inner_html
51
+ input.attributes["class"] = ["i18n", input.attributes["class"]].uniq.join(" ").strip
52
+ end
53
+
54
+ input
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end