fast_gettext 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc6a0e04654cd2e6d0d6657d37f564a1472ec43e
4
- data.tar.gz: 9843f87ded12215c6e1f3ae9a588170af7e01f97
3
+ metadata.gz: 8f4c80626231f0361be0e173421c4a1010c0a8d8
4
+ data.tar.gz: cbeb05b0da1df2944d4f7a7b48fc8bc85b540a27
5
5
  SHA512:
6
- metadata.gz: b535e9e77d6e2237bfcafcf44612d1794748f749fb5d12bde658533116d75d4208f5f9621957d0f33dfdf93b9b9eadcb9bf6d6fda87389e5880167683cd9ebde
7
- data.tar.gz: c3f7e9345eb928d017baa6c10041a23fb0811eb5e93d150dac786735cc219b32a3ad12e7d0e2641594a844e76afdc09ac63e116168e6213577b7f0075f8bac22
6
+ metadata.gz: 3f35ac017e58f1b0bdea6f3f3a0b32a4dc281806d4d6bfef0280bc96f953d19857038a70557d67e81146778a42996cfdfc6c9d7b40b92614e3b3a608e4e0f541
7
+ data.tar.gz: 11c9da7cf61ebe44f2be2e3e8da72318859c860a7edcc28ec5f624011bd0a79190ac4b21f0efb826a3adef8635e9485fcc39f23fabdb2b02b12681eb31e8860b
data/Readme.md CHANGED
@@ -44,46 +44,60 @@ Setup
44
44
  =====
45
45
  ### 1. Install
46
46
 
47
- sudo gem install fast_gettext
47
+ ```Bash
48
+ gem install fast_gettext
49
+ ```
48
50
 
49
51
  ### 2. Add a translation repository
50
52
 
51
53
  From mo files (traditional/default)
52
54
 
53
- FastGettext.add_text_domain('my_app', path: 'locale')
55
+ ```Ruby
56
+ FastGettext.add_text_domain('my_app', path: 'locale')
57
+ ```
54
58
 
55
59
  Or po files (less maintenance than mo)
56
60
 
57
- FastGettext.add_text_domain('my_app', path: 'locale', type: :po)
58
- # :ignore_fuzzy => true to not use fuzzy translations
59
- # :report_warning => false to hide warnings about obsolete/fuzzy translations
61
+ ```Ruby
62
+ FastGettext.add_text_domain('my_app', path: 'locale', type: :po)
63
+ # :ignore_fuzzy => true to not use fuzzy translations
64
+ # :report_warning => false to hide warnings about obsolete/fuzzy translations
65
+ ```
60
66
 
61
67
  Or yaml files (use I18n syntax/indentation)
62
68
 
63
- FastGettext.add_text_domain('my_app', path: 'config/locales', type: :yaml)
69
+ ```Ruby
70
+ FastGettext.add_text_domain('my_app', path: 'config/locales', type: :yaml)
71
+ ```
64
72
 
65
73
  Or database (scaleable, good for many locales/translators)
66
74
 
67
- # db access is cached <-> only first lookup hits the db
68
- require "fast_gettext/translation_repository/db"
69
- FastGettext::TranslationRepository::Db.require_models #load and include default models
70
- FastGettext.add_text_domain('my_app', type: :db, model: TranslationKey)
75
+ ```Ruby
76
+ # db access is cached <-> only first lookup hits the db
77
+ require "fast_gettext/translation_repository/db"
78
+ FastGettext::TranslationRepository::Db.require_models #load and include default models
79
+ FastGettext.add_text_domain('my_app', type: :db, model: TranslationKey)
80
+ ```
71
81
 
72
82
  ### 3. Choose text domain and locale for translation
73
83
  Do this once in every Thread. (e.g. Rails -> ApplicationController)
74
84
 
75
- FastGettext.text_domain = 'my_app'
76
- FastGettext.available_locales = ['de','en','fr','en_US','en_UK'] # only allow these locales to be set (optional)
77
- FastGettext.locale = 'de'
85
+ ```Ruby
86
+ FastGettext.text_domain = 'my_app'
87
+ FastGettext.available_locales = ['de','en','fr','en_US','en_UK'] # only allow these locales to be set (optional)
88
+ FastGettext.locale = 'de'
89
+ ```
78
90
 
79
91
  ### 4. Start translating
80
92
 
81
- include FastGettext::Translation
82
- _('Car') == 'Auto'
83
- _('not-found') == 'not-found'
84
- s_('Namespace|not-found') == 'not-found'
85
- n_('Axis','Axis',3) == 'Achsen' #German plural of Axis
86
- _('Hello %{name}!') % {name: "Pete"} == 'Hello Pete!'
93
+ ```Ruby
94
+ include FastGettext::Translation
95
+ _('Car') == 'Auto'
96
+ _('not-found') == 'not-found'
97
+ s_('Namespace|not-found') == 'not-found'
98
+ n_('Axis','Axis',3) == 'Achsen' #German plural of Axis
99
+ _('Hello %{name}!') % {name: "Pete"} == 'Hello Pete!'
100
+ ```
87
101
 
88
102
 
89
103
  Managing translations
@@ -93,12 +107,14 @@ Generate .po or .mo files using GetText parser (example tasks at [gettext_i18n_r
93
107
 
94
108
  Tell Gettext where your .mo or .po files lie, e.g. for locale/de/my_app.po and locale/de/LC_MESSAGES/my_app.mo
95
109
 
96
- FastGettext.add_text_domain('my_app', path: 'locale')
110
+ ```Ruby
111
+ FastGettext.add_text_domain('my_app', path: 'locale')
112
+ ```
97
113
 
98
114
  Use the [original GetText](http://github.com/mutoh/gettext) to create and manage po/mo-files.
99
115
  (Work on a po/mo parser & reader that is easier to use has started, contributions welcome @ [get_pomo](http://github.com/grosser/get_pomo) )
100
116
 
101
- ###Database
117
+ ### Database
102
118
  [Example migration for ActiveRecord](http://github.com/grosser/fast_gettext/blob/master/examples/db/migration.rb)<br/>
103
119
  The default plural seperator is `||||` but you may overwrite it (or suggest a better one..).
104
120
 
@@ -115,22 +131,24 @@ Try the [translation_db_engine](http://github.com/grosser/translation_db_engine)
115
131
  Setting `available_locales`,`text_domain` or `locale` will not work inside the `evironment.rb`,
116
132
  since it runs in a different thread then e.g. controllers, so set them inside your application_controller.
117
133
 
118
- #environment.rb after initializers
119
- Object.send(:include, FastGettext::Translation)
120
- FastGettext.add_text_domain('accounting', path: 'locale')
121
- FastGettext.add_text_domain('frontend', path: 'locale')
122
- ...
123
-
124
- #application_controller.rb
125
- class ApplicationController ...
126
- include FastGettext::Translation
127
- before_filter :set_locale
128
- def set_locale
129
- FastGettext.available_locales = ['de','en',...]
130
- FastGettext.text_domain = 'frontend'
131
- FastGettext.set_locale(params[:locale] || session[:locale] || request.env['HTTP_ACCEPT_LANGUAGE'])
132
- session[:locale] = I18n.locale = FastGettext.locale
133
- end
134
+ ```Ruby
135
+ # config/environment.rb after initializers
136
+ Object.send(:include, FastGettext::Translation)
137
+ FastGettext.add_text_domain('accounting', path: 'locale')
138
+ FastGettext.add_text_domain('frontend', path: 'locale')
139
+ ...
140
+
141
+ # app/controllers/application_controller.rb
142
+ class ApplicationController ...
143
+ include FastGettext::Translation
144
+ before_filter :set_locale
145
+ def set_locale
146
+ FastGettext.available_locales = ['de','en',...]
147
+ FastGettext.text_domain = 'frontend'
148
+ FastGettext.set_locale(params[:locale] || session[:locale] || request.env['HTTP_ACCEPT_LANGUAGE'])
149
+ session[:locale] = I18n.locale = FastGettext.locale
150
+ end
151
+ ```
134
152
 
135
153
 
136
154
  Advanced features
@@ -142,102 +160,132 @@ If you have any languages that do not fit this rule, you have to add a custom pl
142
160
 
143
161
  Via Ruby:
144
162
 
145
- FastGettext.pluralisation_rule = lambda{|count| count > 5 ? 1 : (count > 2 ? 0 : 2)}
163
+ ```Ruby
164
+ FastGettext.pluralisation_rule = lambda{|count| count > 5 ? 1 : (count > 2 ? 0 : 2)}
165
+ ```
146
166
 
147
167
  Via mo/pofile:
148
168
 
149
- Plural-Forms: nplurals=2; plural=n==2?3:4;
169
+ ```
170
+ Plural-Forms: nplurals=2; plural=n==2?3:4;
171
+ ```
150
172
 
151
173
  [Plural expressions for all languages](http://translate.sourceforge.net/wiki/l10n/pluralforms).
152
174
 
153
- ###default_text_domain
175
+ ### default_text_domain
154
176
  If you only use one text domain, setting `FastGettext.default_text_domain = 'app'`
155
177
  is sufficient and no more `text_domain=` is needed
156
178
 
157
- ###default_locale
179
+ ### default_locale
158
180
  If the simple rule of "first `availble_locale` or 'en'" is not suficcient for you, set `FastGettext.default_locale = 'de'`.
159
181
 
160
- ###default_available_locales
182
+ ### default_available_locales
161
183
  Fallback when no available_locales are set
162
184
 
163
- ###Chains
185
+ ### with_locale
186
+ If there is content from different locales that you wish to display, you should use the with_locale option as below:
187
+
188
+ ```Ruby
189
+ FastGettext.with_locale 'gsw_CH' do
190
+ FastGettext._('Car was successfully created.')
191
+ end
192
+ # => "Z auto isch erfolgriich gspeicharat worda."
193
+ ```
194
+
195
+ ### Chains
164
196
  You can use any number of repositories to find a translation. Simply add them to a chain and when
165
197
  the first cannot translate a given key, the next is asked and so forth.
166
198
 
167
- repos = [
168
- FastGettext::TranslationRepository.build('new', path: '....'),
169
- FastGettext::TranslationRepository.build('old', path: '....')
170
- ]
171
- FastGettext.add_text_domain 'combined', type: :chain, :chain: repos
199
+ ```Ruby
200
+ repos = [
201
+ FastGettext::TranslationRepository.build('new', path: '....'),
202
+ FastGettext::TranslationRepository.build('old', path: '....')
203
+ ]
204
+ FastGettext.add_text_domain 'combined', type: :chain, :chain: repos
205
+ ```
172
206
 
173
- ###Merge
207
+ ### Merge
174
208
  In some cases you can benefit from using merge repositories as an alternative to chains. They behave nearly the same. The difference is in the internal
175
209
  data structure. While chain repos iterate over the whole chain for each translation, merge repositories select and store the first translation at the time
176
210
  a subordinate repository is added. This puts the burden on the load phase and speeds up the translations.
177
211
 
178
- repos = [
179
- FastGettext::TranslationRepository.build('new', :path: '....'),
180
- FastGettext::TranslationRepository.build('old', :path: '....')
181
- ]
182
- domain = FastGettext.add_text_domain 'combined', type: :merge, chain: repos
212
+ ```Ruby
213
+ repos = [
214
+ FastGettext::TranslationRepository.build('new', :path: '....'),
215
+ FastGettext::TranslationRepository.build('old', :path: '....')
216
+ ]
217
+ domain = FastGettext.add_text_domain 'combined', type: :merge, chain: repos
218
+ ```
183
219
 
184
220
  Downside of this approach is that you have to reload the merge repo each time a language is changed.
185
221
 
186
- FastGettext.locale = 'de'
187
- domain.reload
222
+ ```Ruby
223
+ FastGettext.locale = 'de'
224
+ domain.reload
225
+ ```
188
226
 
189
- ###Logger
227
+ ### Logger
190
228
  When you want to know which keys could not be translated or were used, add a Logger to a Chain:
191
229
 
192
- repos = [
193
- FastGettext::TranslationRepository.build('app', path: '....')
194
- FastGettext::TranslationRepository.build('logger', type: :logger, callback: lambda{|key_or_array_of_ids| ... }),
195
- }
196
- FastGettext.add_text_domain 'combined', type: :chain, chain: repos
230
+ ```Ruby
231
+ repos = [
232
+ FastGettext::TranslationRepository.build('app', path: '....')
233
+ FastGettext::TranslationRepository.build('logger', type: :logger, callback: lambda{|key_or_array_of_ids| ... }),
234
+ }
235
+ FastGettext.add_text_domain 'combined', type: :chain, chain: repos
236
+ ```
197
237
 
198
238
  If the Logger is in position #1 it will see all translations, if it is in position #2 it will only see the unfound.
199
239
  Unfound may not always mean missing, if you choose not to translate a word because the key is a good translation, it will appear nevertheless.
200
240
  A lambda or anything that responds to `call` will do as callback. A good starting point may be `examples/missing_translations_logger.rb`.
201
241
 
202
- ###Plugins
242
+ ### Plugins
203
243
  Want a xml version ?
204
244
  Write your own TranslationRepository!
205
245
 
206
- #fast_gettext/translation_repository/xxx.rb
207
- module FastGettext
208
- module TranslationRepository
209
- class Wtf
210
- define initialize(name,options), [key], plural(*keys) and
211
- either inherit from TranslationRepository::Base or define available_locales and pluralisation_rule
212
- end
213
- end
246
+ ```Ruby
247
+ # fast_gettext/translation_repository/xxx.rb
248
+ module FastGettext
249
+ module TranslationRepository
250
+ class Wtf
251
+ define initialize(name,options), [key], plural(*keys) and
252
+ either inherit from TranslationRepository::Base or define available_locales and pluralisation_rule
214
253
  end
254
+ end
255
+ end
256
+ ```
215
257
 
216
- ###Multi domain support
258
+ ### Multi domain support
217
259
 
218
260
  If you have more than one gettext domain, there are two sets of functions
219
261
  available:
220
262
 
221
- include FastGettext::TranslationMultidomain
263
+ ```Ruby
264
+ include FastGettext::TranslationMultidomain
222
265
 
223
- d_("domainname", "string") # finds 'string' in domain domainname
224
- dn_("domainname", "string", "strings", 1) # ditto
225
- # etc.
266
+ d_("domainname", "string") # finds 'string' in domain domainname
267
+ dn_("domainname", "string", "strings", 1) # ditto
268
+ # etc.
269
+ ```
226
270
 
227
271
  These are helper methods so you don't need to write:
228
272
 
229
- FastGettext.text_domain = "domainname"
230
- _("string")
273
+ ```Ruby
274
+ FastGettext.text_domain = "domainname"
275
+ _("string")
276
+ ```
231
277
 
232
278
  It is useful in Rails plugins in the views for example. The second set of
233
279
  functions are D functions which search for string in _all_ domains. If there
234
280
  are multiple translations in different domains, it returns them in random
235
281
  order (depends on the Ruby hash implementation):
236
282
 
237
- include FastGettext::TranslationMultidomain
283
+ ```Ruby
284
+ include FastGettext::TranslationMultidomain
238
285
 
239
- D_("string") # finds 'string' in any domain
240
- # etc.
286
+ D_("string") # finds 'string' in any domain
287
+ # etc.
288
+ ```
241
289
 
242
290
  Alternatively you can use [merge repository](https://github.com/grosser/fast_gettext#merge) to achieve the same behaviour.
243
291
 
@@ -274,6 +322,7 @@ Mo/Po-file parsing from Masao Mutoh, see vendor/README
274
322
  - [Lukáš Zapletal](https://github.com/lzap)
275
323
  - [Dominic Cleal](https://github.com/domcleal)
276
324
  - [Tomas Strachota](https://github.com/tstrachota)
325
+ - [Martin Meier](https://github.com/mameier)
277
326
 
278
327
  [Michael Grosser](http://grosser.it)<br/>
279
328
  michael@grosser.it<br/>
@@ -4,6 +4,9 @@ module FastGettext
4
4
  # - base for all repositories
5
5
  # - fallback as empty repository, that cannot translate anything but does not crash
6
6
  class Base
7
+
8
+ attr_reader :name, :options
9
+
7
10
  def initialize(name,options={})
8
11
  @name = name
9
12
  @options = options
@@ -1,3 +1,3 @@
1
1
  module FastGettext
2
- VERSION = Version = '1.3.0'
2
+ VERSION = Version = '1.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_gettext
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-19 00:00:00.000000000 Z
11
+ date: 2017-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake