fast_gettext 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +1 -0
- data/Readme.md +33 -15
- data/lib/fast_gettext/mo_file.rb +5 -5
- data/lib/fast_gettext/po_file.rb +1 -1
- data/lib/fast_gettext/translation_repository/merge.rb +73 -0
- data/lib/fast_gettext/translation_repository/mo.rb +5 -1
- data/lib/fast_gettext/vendor/poparser.rb +2 -2
- data/lib/fast_gettext/version.rb +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f47483365d8998f016a8c092875a4b92cc281264
|
4
|
+
data.tar.gz: e3cdfa1fd0ca9ffc4b7ba397c10f7de01e6099d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf39c69120683117225d63aed75e3d4e451d89ef863edebee55055155b16dc5ed37413d183cd98e5edca4c63fe758652664f3afa0574c40a58eb70bc859a5280
|
7
|
+
data.tar.gz: 14ddb2652d36ce7b9f9dc22bdfbcc36341446d5e623b1df41f33087cd75507194b567aabd96cab384573c94344084122297846f7b920b84746b8ac1a25a79b8d
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
1.1.0 -- translations are no longer eager loaded for improved startup performance, pass `eager_load: true` to preload for example in preforked web server
|
1
2
|
1.0.0 -- do not enforce attr_accessible unless ProtectedAttributes are loaded
|
2
3
|
0.9.0 -- reworked internals of caching to be plugable
|
3
4
|
0.7.0 -- set_locale resets to default locale if none of the available locales was tried to set
|
data/Readme.md
CHANGED
@@ -50,24 +50,24 @@ Setup
|
|
50
50
|
|
51
51
|
From mo files (traditional/default)
|
52
52
|
|
53
|
-
FastGettext.add_text_domain('my_app'
|
53
|
+
FastGettext.add_text_domain('my_app', path: 'locale')
|
54
54
|
|
55
55
|
Or po files (less maintenance than mo)
|
56
56
|
|
57
|
-
FastGettext.add_text_domain('my_app'
|
57
|
+
FastGettext.add_text_domain('my_app', path: 'locale', type: :po)
|
58
58
|
# :ignore_fuzzy => true to not use fuzzy translations
|
59
59
|
# :report_warning => false to hide warnings about obsolete/fuzzy translations
|
60
60
|
|
61
61
|
Or yaml files (use I18n syntax/indentation)
|
62
62
|
|
63
|
-
FastGettext.add_text_domain('my_app', :
|
63
|
+
FastGettext.add_text_domain('my_app', path: 'config/locales', type: :yaml)
|
64
64
|
|
65
65
|
Or database (scaleable, good for many locales/translators)
|
66
66
|
|
67
67
|
# db access is cached <-> only first lookup hits the db
|
68
68
|
require "fast_gettext/translation_repository/db"
|
69
69
|
FastGettext::TranslationRepository::Db.require_models #load and include default models
|
70
|
-
FastGettext.add_text_domain('my_app', :
|
70
|
+
FastGettext.add_text_domain('my_app', type: :db, model: TranslationKey)
|
71
71
|
|
72
72
|
### 3. Choose text domain and locale for translation
|
73
73
|
Do this once in every Thread. (e.g. Rails -> ApplicationController)
|
@@ -83,7 +83,7 @@ Do this once in every Thread. (e.g. Rails -> ApplicationController)
|
|
83
83
|
_('not-found') == 'not-found'
|
84
84
|
s_('Namespace|not-found') == 'not-found'
|
85
85
|
n_('Axis','Axis',3) == 'Achsen' #German plural of Axis
|
86
|
-
_('Hello %{name}!') % {:
|
86
|
+
_('Hello %{name}!') % {name: "Pete"} == 'Hello Pete!'
|
87
87
|
|
88
88
|
|
89
89
|
Managing translations
|
@@ -93,7 +93,7 @@ Generate .po or .mo files using GetText parser (example tasks at [gettext_i18n_r
|
|
93
93
|
|
94
94
|
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
95
|
|
96
|
-
FastGettext.add_text_domain('my_app'
|
96
|
+
FastGettext.add_text_domain('my_app', path: 'locale')
|
97
97
|
|
98
98
|
Use the [original GetText](http://github.com/mutoh/gettext) to create and manage po/mo-files.
|
99
99
|
(Work on a po/mo parser & reader that is easier to use has started, contributions welcome @ [get_pomo](http://github.com/grosser/get_pomo) )
|
@@ -116,9 +116,9 @@ Setting `available_locales`,`text_domain` or `locale` will not work inside the `
|
|
116
116
|
since it runs in a different thread then e.g. controllers, so set them inside your application_controller.
|
117
117
|
|
118
118
|
#environment.rb after initializers
|
119
|
-
Object.send(:include,FastGettext::Translation)
|
120
|
-
FastGettext.add_text_domain('accounting'
|
121
|
-
FastGettext.add_text_domain('frontend'
|
119
|
+
Object.send(:include, FastGettext::Translation)
|
120
|
+
FastGettext.add_text_domain('accounting', path: 'locale')
|
121
|
+
FastGettext.add_text_domain('frontend', path: 'locale')
|
122
122
|
...
|
123
123
|
|
124
124
|
#application_controller.rb
|
@@ -165,19 +165,35 @@ You can use any number of repositories to find a translation. Simply add them to
|
|
165
165
|
the first cannot translate a given key, the next is asked and so forth.
|
166
166
|
|
167
167
|
repos = [
|
168
|
-
FastGettext::TranslationRepository.build('new', :
|
169
|
-
FastGettext::TranslationRepository.build('old', :
|
168
|
+
FastGettext::TranslationRepository.build('new', path: '....'),
|
169
|
+
FastGettext::TranslationRepository.build('old', path: '....')
|
170
170
|
]
|
171
|
-
FastGettext.add_text_domain 'combined', :
|
171
|
+
FastGettext.add_text_domain 'combined', type: :chain, :chain: repos
|
172
|
+
|
173
|
+
###Merge
|
174
|
+
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
|
+
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
|
+
a subordinate repository is added. This puts the burden on the load phase and speeds up the translations.
|
177
|
+
|
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
|
183
|
+
|
184
|
+
Downside of this approach is that you have to reload the merge repo each time a language is changed.
|
185
|
+
|
186
|
+
FastGettext.locale = 'de'
|
187
|
+
domain.reload
|
172
188
|
|
173
189
|
###Logger
|
174
190
|
When you want to know which keys could not be translated or were used, add a Logger to a Chain:
|
175
191
|
|
176
192
|
repos = [
|
177
|
-
FastGettext::TranslationRepository.build('app', :
|
178
|
-
FastGettext::TranslationRepository.build('logger', :
|
193
|
+
FastGettext::TranslationRepository.build('app', path: '....')
|
194
|
+
FastGettext::TranslationRepository.build('logger', type: :logger, callback: lambda{|key_or_array_of_ids| ... }),
|
179
195
|
}
|
180
|
-
FastGettext.add_text_domain 'combined', :
|
196
|
+
FastGettext.add_text_domain 'combined', type: :chain, chain: repos
|
181
197
|
|
182
198
|
If the Logger is in position #1 it will see all translations, if it is in position #2 it will only see the unfound.
|
183
199
|
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.
|
@@ -223,6 +239,8 @@ order (depends on the Ruby hash implementation):
|
|
223
239
|
D_("string") # finds 'string' in any domain
|
224
240
|
# etc.
|
225
241
|
|
242
|
+
Alternatively you can use [merge repository](https://github.com/grosser/fast_gettext#merge) to achieve the same behaviour.
|
243
|
+
|
226
244
|
FAQ
|
227
245
|
===
|
228
246
|
- [Problems with ActiveRecord messages?](http://wiki.github.com/grosser/fast_gettext/activerecord)
|
data/lib/fast_gettext/mo_file.rb
CHANGED
@@ -30,17 +30,17 @@ module FastGettext
|
|
30
30
|
nil
|
31
31
|
end
|
32
32
|
|
33
|
+
def data
|
34
|
+
load_data if @data.nil?
|
35
|
+
@data
|
36
|
+
end
|
37
|
+
|
33
38
|
def self.empty
|
34
39
|
MoFile.new(File.join(File.dirname(__FILE__),'vendor','empty.mo'))
|
35
40
|
end
|
36
41
|
|
37
42
|
private
|
38
43
|
|
39
|
-
def data
|
40
|
-
load_data if @data.nil?
|
41
|
-
@data
|
42
|
-
end
|
43
|
-
|
44
44
|
def load_data
|
45
45
|
@data = if @filename.is_a? FastGettext::GetText::MOFile
|
46
46
|
@filename
|
data/lib/fast_gettext/po_file.rb
CHANGED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'fast_gettext/translation_repository/po'
|
2
|
+
|
3
|
+
module FastGettext
|
4
|
+
module TranslationRepository
|
5
|
+
# Responsibility:
|
6
|
+
# - merge data from multiple repositories into one hash structure
|
7
|
+
# - can be used instead of searching for translations in multiple domains
|
8
|
+
# - requires reload when current locale is changed
|
9
|
+
class Merge < Base
|
10
|
+
def initialize(name, options={})
|
11
|
+
clear
|
12
|
+
super(name, options)
|
13
|
+
options.fetch(:chain, []).each do |repo|
|
14
|
+
add_repo(repo)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def available_locales
|
19
|
+
@repositories.flat_map(&:available_locales).uniq
|
20
|
+
end
|
21
|
+
|
22
|
+
def pluralisation_rule
|
23
|
+
@repositories.each do |r|
|
24
|
+
result = r.pluralisation_rule and return result
|
25
|
+
end
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def plural(*keys)
|
30
|
+
@repositories.each do |r|
|
31
|
+
result = r.plural(*keys)
|
32
|
+
return result unless result.compact.empty?
|
33
|
+
end
|
34
|
+
[]
|
35
|
+
end
|
36
|
+
|
37
|
+
def reload
|
38
|
+
@data = {}
|
39
|
+
@repositories.each do |r|
|
40
|
+
r.reload
|
41
|
+
load_repo(r)
|
42
|
+
end
|
43
|
+
super
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_repo(repo)
|
47
|
+
raise "Unsupported repository" unless repo_supported?(repo)
|
48
|
+
@repositories << repo
|
49
|
+
load_repo(repo)
|
50
|
+
true
|
51
|
+
end
|
52
|
+
|
53
|
+
def [](key)
|
54
|
+
@data[key]
|
55
|
+
end
|
56
|
+
|
57
|
+
def clear
|
58
|
+
@repositories = []
|
59
|
+
@data = {}
|
60
|
+
end
|
61
|
+
|
62
|
+
protected
|
63
|
+
|
64
|
+
def repo_supported?(repo)
|
65
|
+
repo.respond_to?(:all_translations)
|
66
|
+
end
|
67
|
+
|
68
|
+
def load_repo(r)
|
69
|
+
@data = r.all_translations.merge(@data)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -24,12 +24,16 @@ module FastGettext
|
|
24
24
|
super
|
25
25
|
end
|
26
26
|
|
27
|
+
def all_translations
|
28
|
+
current_translations.data
|
29
|
+
end
|
30
|
+
|
27
31
|
protected
|
28
32
|
|
29
33
|
def find_and_store_files(name,options)
|
30
34
|
# parse all .mo files with the right name, that sit in locale/LC_MESSAGES folders
|
31
35
|
find_files_in_locale_folders(File.join('LC_MESSAGES',"#{name}.mo"), options[:path]) do |locale,file|
|
32
|
-
MoFile.new(file, :
|
36
|
+
MoFile.new(file, eager_load: @eager_load)
|
33
37
|
end
|
34
38
|
end
|
35
39
|
|
@@ -133,7 +133,7 @@ module GetText
|
|
133
133
|
|
134
134
|
def parse_file(po_file, data)
|
135
135
|
args = [ po_file ]
|
136
|
-
#
|
136
|
+
# In Ruby 1.9, we must detect proper encoding of a PO file.
|
137
137
|
if String.instance_methods.include?(:encode)
|
138
138
|
encoding = detect_file_encoding(po_file)
|
139
139
|
args << "r:#{encoding}"
|
@@ -144,7 +144,7 @@ module GetText
|
|
144
144
|
|
145
145
|
def detect_file_encoding(po_file)
|
146
146
|
open(po_file, :encoding => 'ASCII-8BIT') do |input|
|
147
|
-
input.
|
147
|
+
input.lines.each do |line|
|
148
148
|
return Encoding.find($1) if %r["Content-Type:.*\scharset=(.*)\\n"] =~ line
|
149
149
|
end
|
150
150
|
end
|
data/lib/fast_gettext/version.rb
CHANGED
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.
|
4
|
+
version: 1.2.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:
|
11
|
+
date: 2016-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/fast_gettext/translation_repository/db_models/translation_key.rb
|
130
130
|
- lib/fast_gettext/translation_repository/db_models/translation_text.rb
|
131
131
|
- lib/fast_gettext/translation_repository/logger.rb
|
132
|
+
- lib/fast_gettext/translation_repository/merge.rb
|
132
133
|
- lib/fast_gettext/translation_repository/mo.rb
|
133
134
|
- lib/fast_gettext/translation_repository/po.rb
|
134
135
|
- lib/fast_gettext/translation_repository/yaml.rb
|
@@ -139,7 +140,7 @@ files:
|
|
139
140
|
- lib/fast_gettext/vendor/poparser.rb
|
140
141
|
- lib/fast_gettext/vendor/string.rb
|
141
142
|
- lib/fast_gettext/version.rb
|
142
|
-
homepage:
|
143
|
+
homepage: https://github.com/grosser/fast_gettext
|
143
144
|
licenses:
|
144
145
|
- MIT
|
145
146
|
- Ruby
|
@@ -152,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
153
|
requirements:
|
153
154
|
- - ">="
|
154
155
|
- !ruby/object:Gem::Version
|
155
|
-
version:
|
156
|
+
version: 2.1.0
|
156
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
158
|
requirements:
|
158
159
|
- - ">="
|
@@ -160,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
161
|
version: '0'
|
161
162
|
requirements: []
|
162
163
|
rubyforge_project:
|
163
|
-
rubygems_version: 2.
|
164
|
+
rubygems_version: 2.5.1
|
164
165
|
signing_key:
|
165
166
|
specification_version: 4
|
166
167
|
summary: A simple, fast, memory-efficient and threadsafe implementation of GetText
|