nulogy-gettext_i18n_rails 0.4.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/Gemfile +16 -0
  2. data/Gemfile.lock +126 -0
  3. data/Rakefile +24 -0
  4. data/Readme.md +226 -0
  5. data/VERSION +1 -0
  6. data/gettext_i18n_rails.gemspec +65 -0
  7. data/init.rb +14 -0
  8. data/lib/gettext_i18n_rails.rb +33 -0
  9. data/lib/gettext_i18n_rails/action_controller.rb +8 -0
  10. data/lib/gettext_i18n_rails/active_record.rb +19 -0
  11. data/lib/gettext_i18n_rails/backend.rb +67 -0
  12. data/lib/gettext_i18n_rails/base_parser.rb +41 -0
  13. data/lib/gettext_i18n_rails/haml_parser.rb +15 -0
  14. data/lib/gettext_i18n_rails/hamlet_parser.rb +16 -0
  15. data/lib/gettext_i18n_rails/html_safe_translations.rb +29 -0
  16. data/lib/gettext_i18n_rails/i18n_hacks.rb +25 -0
  17. data/lib/gettext_i18n_rails/model_attributes_finder.rb +108 -0
  18. data/lib/gettext_i18n_rails/railtie.rb +22 -0
  19. data/lib/gettext_i18n_rails/ruby_gettext_extractor.rb +144 -0
  20. data/lib/gettext_i18n_rails/slim_parser.rb +15 -0
  21. data/lib/gettext_i18n_rails/string_interpolate_fix.rb +20 -0
  22. data/lib/gettext_i18n_rails/tasks.rb +127 -0
  23. data/lib/tasks/gettext_rails_i18n.rake +1 -0
  24. data/spec/gettext_i18n_rails/action_controller_spec.rb +54 -0
  25. data/spec/gettext_i18n_rails/active_record_spec.rb +85 -0
  26. data/spec/gettext_i18n_rails/backend_spec.rb +56 -0
  27. data/spec/gettext_i18n_rails/haml_parser_spec.rb +39 -0
  28. data/spec/gettext_i18n_rails/hamlet_parser_spec.rb +33 -0
  29. data/spec/gettext_i18n_rails/slim_parser_spec.rb +40 -0
  30. data/spec/gettext_i18n_rails/string_interpolate_fix_spec.rb +32 -0
  31. data/spec/gettext_i18n_rails_spec.rb +84 -0
  32. data/spec/spec_helper.rb +39 -0
  33. metadata +110 -0
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source :rubygems
2
+
3
+ gem 'fast_gettext'
4
+
5
+ group :dev do
6
+ gem 'haml'
7
+ gem 'slim'
8
+ gem 'hamlet'
9
+ gem 'ruby_parser'
10
+ gem 'gettext'
11
+ gem 'sqlite3', '~>1.3.4'
12
+ gem 'rails', ENV['RAILS'] || '~>3'
13
+ gem 'rake'
14
+ gem 'rspec', '~>2'
15
+ gem 'jeweler'
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,126 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.1.0)
5
+ actionpack (= 3.1.0)
6
+ mail (~> 2.3.0)
7
+ actionpack (3.1.0)
8
+ activemodel (= 3.1.0)
9
+ activesupport (= 3.1.0)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ i18n (~> 0.6)
13
+ rack (~> 1.3.2)
14
+ rack-cache (~> 1.0.3)
15
+ rack-mount (~> 0.8.2)
16
+ rack-test (~> 0.6.1)
17
+ sprockets (~> 2.0.0)
18
+ activemodel (3.1.0)
19
+ activesupport (= 3.1.0)
20
+ bcrypt-ruby (~> 3.0.0)
21
+ builder (~> 3.0.0)
22
+ i18n (~> 0.6)
23
+ activerecord (3.1.0)
24
+ activemodel (= 3.1.0)
25
+ activesupport (= 3.1.0)
26
+ arel (~> 2.2.1)
27
+ tzinfo (~> 0.3.29)
28
+ activeresource (3.1.0)
29
+ activemodel (= 3.1.0)
30
+ activesupport (= 3.1.0)
31
+ activesupport (3.1.0)
32
+ multi_json (~> 1.0)
33
+ arel (2.2.1)
34
+ bcrypt-ruby (3.0.1)
35
+ builder (3.0.0)
36
+ diff-lcs (1.1.2)
37
+ erubis (2.7.0)
38
+ fast_gettext (0.5.13)
39
+ gettext (2.1.0)
40
+ locale (>= 2.0.5)
41
+ git (1.2.5)
42
+ haml (3.1.4)
43
+ hamlet (0.1)
44
+ slim (~> 1.0)
45
+ hike (1.2.1)
46
+ i18n (0.6.0)
47
+ jeweler (1.6.4)
48
+ bundler (~> 1.0)
49
+ git (>= 1.2.5)
50
+ rake
51
+ locale (2.0.5)
52
+ mail (2.3.0)
53
+ i18n (>= 0.4.0)
54
+ mime-types (~> 1.16)
55
+ treetop (~> 1.4.8)
56
+ mime-types (1.16)
57
+ multi_json (1.0.3)
58
+ polyglot (0.3.2)
59
+ rack (1.3.2)
60
+ rack-cache (1.0.3)
61
+ rack (>= 0.4)
62
+ rack-mount (0.8.3)
63
+ rack (>= 1.0.0)
64
+ rack-ssl (1.3.2)
65
+ rack
66
+ rack-test (0.6.1)
67
+ rack (>= 1.0)
68
+ rails (3.1.0)
69
+ actionmailer (= 3.1.0)
70
+ actionpack (= 3.1.0)
71
+ activerecord (= 3.1.0)
72
+ activeresource (= 3.1.0)
73
+ activesupport (= 3.1.0)
74
+ bundler (~> 1.0)
75
+ railties (= 3.1.0)
76
+ railties (3.1.0)
77
+ actionpack (= 3.1.0)
78
+ activesupport (= 3.1.0)
79
+ rack-ssl (~> 1.3.2)
80
+ rake (>= 0.8.7)
81
+ rdoc (~> 3.4)
82
+ thor (~> 0.14.6)
83
+ rake (0.9.2)
84
+ rdoc (3.9.4)
85
+ rspec (2.6.0)
86
+ rspec-core (~> 2.6.0)
87
+ rspec-expectations (~> 2.6.0)
88
+ rspec-mocks (~> 2.6.0)
89
+ rspec-core (2.6.4)
90
+ rspec-expectations (2.6.0)
91
+ diff-lcs (~> 1.1.2)
92
+ rspec-mocks (2.6.0)
93
+ ruby_parser (2.3.1)
94
+ sexp_processor (~> 3.0)
95
+ sexp_processor (3.0.10)
96
+ slim (1.1.0)
97
+ temple (~> 0.3.5)
98
+ tilt (~> 1.3.2)
99
+ sprockets (2.0.0)
100
+ hike (~> 1.2)
101
+ rack (~> 1.0)
102
+ tilt (~> 1.1, != 1.3.0)
103
+ sqlite3 (1.3.4)
104
+ temple (0.3.5)
105
+ thor (0.14.6)
106
+ tilt (1.3.3)
107
+ treetop (1.4.10)
108
+ polyglot
109
+ polyglot (>= 0.3.1)
110
+ tzinfo (0.3.29)
111
+
112
+ PLATFORMS
113
+ ruby
114
+
115
+ DEPENDENCIES
116
+ fast_gettext
117
+ gettext
118
+ haml
119
+ hamlet
120
+ jeweler
121
+ rails (~> 3)
122
+ rake
123
+ rspec (~> 2)
124
+ ruby_parser
125
+ slim
126
+ sqlite3 (~> 1.3.4)
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ task :spec do
2
+ sh "rspec spec"
3
+ end
4
+
5
+ task :default do
6
+ sh "RAILS=2.3.14 && (bundle || bundle install) && bundle exec rake spec"
7
+ sh "RAILS=3.0.9 && (bundle || bundle install) && bundle exec rake spec"
8
+ sh "RAILS=3.1.0 && (bundle || bundle install) && bundle exec rake spec"
9
+ end
10
+
11
+ begin
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = 'gettext_i18n_rails'
15
+ gem.summary = "Simple FastGettext Rails integration."
16
+ gem.email = "grosser.michael@gmail.com"
17
+ gem.homepage = "http://github.com/grosser/#{gem.name}"
18
+ gem.authors = ["Michael Grosser"]
19
+ end
20
+
21
+ Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
24
+ end
data/Readme.md ADDED
@@ -0,0 +1,226 @@
1
+ [FastGettext](http://github.com/grosser/fast_gettext) / Rails integration.
2
+
3
+ Translate via FastGettext, use any other I18n backend as extension/fallback.
4
+
5
+ Rails does: `I18n.t('syntax.with.lots.of.dots')` with nested yml files
6
+ We do: `_('Just translate my damn text!')` with simple, flat mo/po/yml files or directly from db
7
+ To use I18n calls add a `syntax.with.lots.of.dots` translation.
8
+
9
+ [See it working in the example application.](https://github.com/grosser/gettext_i18n_rails_example)
10
+
11
+ Setup
12
+ =====
13
+ ### Installation
14
+
15
+ #### Rails 3
16
+
17
+ ##### As plugin:
18
+
19
+ rails plugin install git://github.com/grosser/gettext_i18n_rails.git
20
+
21
+ # Gemfile
22
+ gem 'fast_gettext', '>=0.4.8'
23
+
24
+ ##### As gem:
25
+
26
+ # Gemfile
27
+ gem 'gettext_i18n_rails'
28
+
29
+ ##### Optional:
30
+ Add `gettext` if you want to find translations or build .mo files<br/>
31
+ Add `ruby_parser` if you want to find translations inside haml/slim/hamlet files (does not support ruby 1.9 syntax)
32
+
33
+
34
+ # Gemfile
35
+ gem 'gettext', '>=1.9.3', :require => false, :group => :development
36
+ gem 'ruby_parser', :require => false, :group => :development
37
+
38
+ #### Rails 2
39
+
40
+ ##### As plugin:
41
+
42
+ script/plugin install git://github.com/grosser/gettext_i18n_rails.git
43
+ sudo gem install fast_gettext
44
+
45
+ # config/environment.rb
46
+ config.gem "fast_gettext", :version => '>=0.4.8'
47
+
48
+ ##### As gem:
49
+
50
+ gem install gettext_i18n_rails
51
+
52
+ # config/environment.rb
53
+ config.gem 'gettext_i18n_rails'
54
+
55
+ #Rakefile
56
+ begin
57
+ require "gettext_i18n_rails/tasks"
58
+ rescue LoadError
59
+ puts "gettext_i18n_rails is not installed, you probably should run 'rake gems:install' or 'bundle install'."
60
+ end
61
+
62
+ ##### Optional:
63
+ If you want to find translations or build .mo files
64
+ # config/environments/development.rb
65
+ config.gem "gettext", :version => '>=1.9.3', :lib => false
66
+
67
+ ### Locales & initialisation
68
+ Copy default locales with dates/sentence-connectors/AR-errors you want from e.g.
69
+ [rails i18n](http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/) into 'config/locales'
70
+
71
+ To initialize:
72
+
73
+ # config/initializers/fast_gettext.rb
74
+ FastGettext.add_text_domain 'app', :path => 'locale', :type => :po
75
+ FastGettext.default_available_locales = ['en','de'] #all you want to allow
76
+ FastGettext.default_text_domain = 'app'
77
+
78
+ And in your application:
79
+
80
+ # app/controllers/application_controller.rb
81
+ class ApplicationController < ...
82
+ before_filter :set_gettext_locale
83
+
84
+ Translating
85
+ ===========
86
+ Performance is almost the same for all backends since translations are cached after first use.
87
+
88
+ ### Option A: .po files
89
+
90
+ FastGettext.add_text_domain 'app', :path => 'locale', :type => :po
91
+
92
+ - use some _('translations')
93
+ - run `rake gettext:find`, to let GetText find all translations used
94
+ - (optional) run `rake gettext:store_model_attributes`, to parse the database for columns that can be translated
95
+ - if this is your first translation: `cp locale/app.pot locale/de/app.po` for every locale you want to use
96
+ - translate messages in 'locale/de/app.po' (leave msgstr blank and msgstr == msgid)
97
+
98
+ New translations will be marked "fuzzy", search for this and remove it, so that they will be used.
99
+ Obsolete translations are marked with ~#, they usually can be removed since they are no longer needed
100
+
101
+ #### Unfound translations with rake gettext:find
102
+ Dynamic translations like `_("x"+"u")` cannot be fond. You have 4 options:
103
+
104
+ - add `N_('xu')` somewhere else in the code, so the parser sees it
105
+ - add `N_('xu')` in a totally separate file like `locale/unfound_translations.rb`, so the parser sees it
106
+ - use the [gettext_test_log rails plugin ](http://github.com/grosser/gettext_test_log) to find all translations that where used while testing
107
+ - add a Logger to a translation Chain, so every unfound translations is logged ([example]((http://github.com/grosser/fast_gettext)))
108
+
109
+ ### Option B: Traditional .po/.mo files
110
+
111
+ FastGettext.add_text_domain 'app', :path => 'locale'
112
+
113
+ - follow Option A
114
+ - run `rake gettext:pack` to write binary GetText .mo files
115
+
116
+ ### Option C: Database
117
+ Most scalable method, all translators can work simultaneously and online.
118
+
119
+ Easiest to use with the [translation database Rails engine](http://github.com/grosser/translation_db_engine).
120
+ Translations can be edited under `/translation_keys`
121
+
122
+ FastGettext::TranslationRepository::Db.require_models
123
+ FastGettext.add_text_domain 'app', :type => :db, :model => TranslationKey
124
+
125
+ I18n
126
+ ====
127
+ I18n.locale <==> FastGettext.locale.to_sym
128
+ I18n.locale = :de <==> FastGettext.locale = 'de'
129
+
130
+ Any call to I18n that matches a gettext key will be translated through FastGettext.
131
+
132
+ Namespaces
133
+ ==========
134
+ Car|Model means Model in namespace Car.
135
+ You do not have to translate this into english "Model", if you use the
136
+ namespace-aware translation
137
+ s_('Car|Model') == 'Model' #when no translation was found
138
+
139
+ XSS / html_safe
140
+ ===============
141
+ If you trust your translators and all your usages of % on translations:<br/>
142
+ # config/environment.rb
143
+ GettextI18nRails.translations_are_html_safe = true
144
+
145
+ String % vs html_safe is buggy (can be used for XSS on 1.8 and is always non-safe in 1.9)<br/>
146
+ My recommended fix is: `require 'gettext_i18n_rails/string_interpolate_fix'`
147
+
148
+ - safe stays safe (escape added strings)
149
+ - unsafe stays unsafe (do not escape added strings)
150
+
151
+ ActiveRecord - error messages
152
+ =============================
153
+ ActiveRecord error messages are translated through Rails::I18n, but
154
+ model names and model attributes are translated through FastGettext.
155
+ Therefore a validation error on a BigCar's wheels_size needs `_('big car')` and `_('BigCar|Wheels size')`
156
+ to display localized.
157
+
158
+ The model/attribute translations can be found through `rake gettext:store_model_attributes`,
159
+ (which ignores some commonly untranslated columns like id,type,xxx_count,...).
160
+
161
+ Error messages can be translated through FastGettext, if the ':message' is a translation-id or the matching Rails I18n key is translated.
162
+
163
+ ####Option A:
164
+ Define a translation for "I need my rating!" and use it as message.
165
+ validates_inclusion_of :rating, :in=>1..5, :message=>N_('I need my rating!')
166
+
167
+ ####Option B:
168
+ validates_inclusion_of :rating, :in=>1..5
169
+ Make a translation for the I18n key: `activerecord.errors.models.rating.attributes.rating.inclusion`
170
+
171
+ ####Option C:
172
+ Add a translation to each config/locales/*.yml files
173
+ en:
174
+ activerecord:
175
+ errors:
176
+ models:
177
+ rating:
178
+ attributes:
179
+ rating:
180
+ inclusion: " -- please choose!"
181
+ The [rails I18n guide](http://guides.rubyonrails.org/i18n.html) can help with Option B and C.
182
+
183
+ Plurals
184
+ =======
185
+ FastGettext supports pluralization
186
+ n_('Apple','Apples',3) == 'Apples'
187
+
188
+ Abnormal plurals like e.g. Polish that has 4 different can also be addressed, see [FastGettext Readme](http://github.com/grosser/fast_gettext)
189
+
190
+ Customizing list of translatable files
191
+ ======================================
192
+ When you run
193
+
194
+ rake gettext:find
195
+
196
+ by default the following files are going to be scanned for translations: {app,lib,config,locale}/**/*.{rb,erb,haml,slim}. If
197
+ you want to specify a different list, you can redefine files_to_translate in the gettext namespace in a file like
198
+ lib/tasks/gettext.rake:
199
+
200
+ namespace :gettext do
201
+ def files_to_translate
202
+ Dir.glob("{app,lib,config,locale}/**/*.{rb,erb,haml,slim,rhtml}")
203
+ end
204
+ end
205
+
206
+ [Contributors](http://github.com/grosser/gettext_i18n_rails/contributors)
207
+ ======
208
+ - [ruby gettext extractor](http://github.com/retoo/ruby_gettext_extractor/tree/master) from [retoo](http://github.com/retoo)
209
+ - [Paul McMahon](http://github.com/pwim)
210
+ - [Duncan Mac-Vicar P](http://duncan.mac-vicar.com/blog)
211
+ - [Ramihajamalala Hery](http://my.rails-royce.org)
212
+ - [J. Pablo Fernández](http://pupeno.com)
213
+ - [Anh Hai Trinh](http://blog.onideas.ws)
214
+ - [ed0h](http://github.com/ed0h)
215
+ - [Nikos Dimitrakopoulos](http://blog.nikosd.com)
216
+ - [Ben Tucker](http://btucker.net/)
217
+ - [Kamil Śliwak](https://github.com/cameel)
218
+ - [Paul McMahon](https://github.com/pwim)
219
+ - [Rainux Luo](https://github.com/rainux)
220
+ - [Lucas Hills](https://github.com/2potatocakes)
221
+ - [Ladislav Slezák](https://github.com/lslezak)
222
+ - [Greg Weber](https://github.com/gregwebs)
223
+
224
+ [Michael Grosser](http://grosser.it)<br/>
225
+ grosser.michael@gmail.com<br/>
226
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.6
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gettext_i18n_rails"
8
+ s.version = "0.4.6"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = "2012-04-05"
13
+ s.email = "grosser.michael@gmail.com"
14
+ s.files = [
15
+ "Gemfile",
16
+ "Gemfile.lock",
17
+ "Rakefile",
18
+ "Readme.md",
19
+ "VERSION",
20
+ "gettext_i18n_rails.gemspec",
21
+ "init.rb",
22
+ "lib/gettext_i18n_rails.rb",
23
+ "lib/gettext_i18n_rails/action_controller.rb",
24
+ "lib/gettext_i18n_rails/active_record.rb",
25
+ "lib/gettext_i18n_rails/backend.rb",
26
+ "lib/gettext_i18n_rails/base_parser.rb",
27
+ "lib/gettext_i18n_rails/haml_parser.rb",
28
+ "lib/gettext_i18n_rails/hamlet_parser.rb",
29
+ "lib/gettext_i18n_rails/html_safe_translations.rb",
30
+ "lib/gettext_i18n_rails/i18n_hacks.rb",
31
+ "lib/gettext_i18n_rails/model_attributes_finder.rb",
32
+ "lib/gettext_i18n_rails/railtie.rb",
33
+ "lib/gettext_i18n_rails/ruby_gettext_extractor.rb",
34
+ "lib/gettext_i18n_rails/slim_parser.rb",
35
+ "lib/gettext_i18n_rails/string_interpolate_fix.rb",
36
+ "lib/gettext_i18n_rails/tasks.rb",
37
+ "lib/tasks/gettext_rails_i18n.rake",
38
+ "spec/gettext_i18n_rails/action_controller_spec.rb",
39
+ "spec/gettext_i18n_rails/active_record_spec.rb",
40
+ "spec/gettext_i18n_rails/backend_spec.rb",
41
+ "spec/gettext_i18n_rails/haml_parser_spec.rb",
42
+ "spec/gettext_i18n_rails/hamlet_parser_spec.rb",
43
+ "spec/gettext_i18n_rails/slim_parser_spec.rb",
44
+ "spec/gettext_i18n_rails/string_interpolate_fix_spec.rb",
45
+ "spec/gettext_i18n_rails_spec.rb",
46
+ "spec/spec_helper.rb"
47
+ ]
48
+ s.homepage = "http://github.com/grosser/gettext_i18n_rails"
49
+ s.require_paths = ["lib"]
50
+ s.rubygems_version = "1.8.15"
51
+ s.summary = "Simple FastGettext Rails integration."
52
+
53
+ if s.respond_to? :specification_version then
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
+ s.add_runtime_dependency(%q<fast_gettext>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<fast_gettext>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<fast_gettext>, [">= 0"])
63
+ end
64
+ end
65
+