grosser-fast_gettext 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +10 -12
- data/VERSION.yml +1 -1
- data/lib/fast_gettext.rb +0 -4
- data/lib/fast_gettext/mo_file.rb +4 -0
- data/lib/fast_gettext/storage.rb +26 -6
- data/lib/fast_gettext/translation.rb +1 -1
- data/spec/aa_unconfigued_spec.rb +23 -0
- data/spec/fast_gettext/storage_spec.rb +5 -0
- data/spec/fast_gettext/translation_spec.rb +21 -1
- data/spec/fast_gettext_spec.rb +1 -0
- data/vendor/empty.mo +0 -0
- metadata +3 -1
data/README.markdown
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
FastGettext
|
2
2
|
===========
|
3
|
-
GetText but
|
3
|
+
GetText but 8.21 times faster, simple, clean namespace (7 vs 34) and threadsave!
|
4
4
|
|
5
5
|
Setup
|
6
6
|
=====
|
@@ -27,32 +27,30 @@ Speed
|
|
27
27
|
50_000 translations:
|
28
28
|
Ideal: (primitive Hash lookup)
|
29
29
|
small translation file:
|
30
|
-
1.
|
30
|
+
1.080000 0.190000 1.270000 ( 1.274699)
|
31
31
|
mapped: 5832K writeable/private: 3016K shared: 28K
|
32
32
|
|
33
33
|
large translation file:
|
34
|
-
1.
|
34
|
+
1.110000 0.200000 1.310000 ( 1.305616)
|
35
35
|
mapped: 5832K writeable/private: 3016K shared: 28K
|
36
36
|
|
37
37
|
FastGettext:
|
38
38
|
small translation file:
|
39
|
-
|
39
|
+
1.980000 0.310000 2.290000 ( 2.285980)
|
40
40
|
mapped: 5852K writeable/private: 3036K shared: 28K
|
41
41
|
|
42
42
|
large translation file:
|
43
|
-
|
44
|
-
mapped:
|
43
|
+
1.990000 0.320000 2.310000 ( 2.318801)
|
44
|
+
mapped: 5852K writeable/private: 3036K shared: 28K
|
45
45
|
|
46
46
|
GetText:
|
47
47
|
small translation file:
|
48
|
-
16.
|
49
|
-
mapped:
|
48
|
+
16.210000 1.290000 17.500000 ( 17.511050)
|
49
|
+
mapped: 8908K writeable/private: 5876K shared: 28K
|
50
50
|
|
51
51
|
large translation file:
|
52
|
-
16.
|
53
|
-
mapped:
|
54
|
-
|
55
|
-
|
52
|
+
16.340000 1.330000 17.670000 ( 17.679807)
|
53
|
+
mapped: 9028K writeable/private: 5996K shared: 28K
|
56
54
|
|
57
55
|
Thread Safety and Rails
|
58
56
|
=======================
|
data/VERSION.yml
CHANGED
data/lib/fast_gettext.rb
CHANGED
@@ -5,11 +5,7 @@ require File.join(File.dirname(__FILE__),'..','vendor','string')
|
|
5
5
|
|
6
6
|
module FastGettext
|
7
7
|
include FastGettext::Storage
|
8
|
-
|
9
8
|
extend self
|
10
|
-
def self.included(mod) #:nodoc:
|
11
|
-
mod.extend self
|
12
|
-
end
|
13
9
|
|
14
10
|
LOCALE_REX = /^[a-z]{2}$|^[a-z]{2}_[A-Z]{2}$/
|
15
11
|
NAMESPACE_SEPERATOR = '|'
|
data/lib/fast_gettext/mo_file.rb
CHANGED
data/lib/fast_gettext/storage.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
module FastGettext
|
2
2
|
module Storage
|
3
|
-
|
3
|
+
class NoTextDomainConfigured < Exception;end
|
4
|
+
|
5
|
+
[:available_locales,:text_domain].each do |method|
|
4
6
|
define_method method do
|
5
7
|
thread_store(method)
|
6
8
|
end
|
@@ -9,16 +11,22 @@ module FastGettext
|
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
14
|
+
# speed hack, twice as fast as
|
15
|
+
# Thread.current['FastGettext.'<<'current_translations']
|
16
|
+
Thread.current[:fast_gettext_current_translations] = NoTextDomainConfigured
|
17
|
+
def current_translations
|
18
|
+
Thread.current[:fast_gettext_current_translations]
|
19
|
+
end
|
20
|
+
def current_translations=x
|
21
|
+
Thread.current[:fast_gettext_current_translations]=x
|
22
|
+
end
|
23
|
+
|
12
24
|
#global, since re-parsing whole folders takes too much time...
|
13
25
|
@@text_domains={}
|
14
26
|
def text_domains
|
15
27
|
@@text_domains
|
16
28
|
end
|
17
29
|
|
18
|
-
def text_domain
|
19
|
-
thread_store(:text_domain)
|
20
|
-
end
|
21
|
-
|
22
30
|
def text_domain=(new_text_domain)
|
23
31
|
write_thread_store(:text_domain,new_text_domain)
|
24
32
|
update_current_translations
|
@@ -36,10 +44,22 @@ module FastGettext
|
|
36
44
|
end
|
37
45
|
end
|
38
46
|
|
47
|
+
# for chaining: puts set_locale('xx') == 'xx' ? 'applied' : 'rejected'
|
48
|
+
# returns the current locale, not the one that was supplied
|
49
|
+
# like locale=(), whoes behavior cannot be changed
|
50
|
+
def set_locale(new_locale)
|
51
|
+
self.locale = new_locale
|
52
|
+
locale
|
53
|
+
end
|
54
|
+
|
39
55
|
private
|
40
56
|
|
41
57
|
def update_current_translations
|
42
|
-
|
58
|
+
if text_domains[text_domain]
|
59
|
+
self.current_translations = text_domains[text_domain][:mo_files][locale] || MoFile.empty
|
60
|
+
else
|
61
|
+
self.current_translations = NoTextDomainConfigured
|
62
|
+
end
|
43
63
|
end
|
44
64
|
|
45
65
|
def thread_store(key)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),'spec_helper')
|
2
|
+
|
3
|
+
describe 'unconfigured' do
|
4
|
+
it "gives a useful error message when trying to just translate" do
|
5
|
+
x=1
|
6
|
+
begin
|
7
|
+
FastGettext._('x')
|
8
|
+
rescue
|
9
|
+
x=$!
|
10
|
+
end
|
11
|
+
x.to_s.should =~ /NoTextDomainConfigured/
|
12
|
+
end
|
13
|
+
it "gives a useful error message when only locale was set" do
|
14
|
+
FastGettext.locale = 'de'
|
15
|
+
x=1
|
16
|
+
begin
|
17
|
+
FastGettext._('x')
|
18
|
+
rescue
|
19
|
+
x=$!
|
20
|
+
end
|
21
|
+
x.to_s.should =~ /NoTextDomainConfigured/
|
22
|
+
end
|
23
|
+
end
|
@@ -48,5 +48,10 @@ describe Storage do
|
|
48
48
|
self.locale = 'en'
|
49
49
|
locale.should == 'de'
|
50
50
|
end
|
51
|
+
it "set_locale returns the old locale if the new could not be set" do
|
52
|
+
self.locale = 'de'
|
53
|
+
self.available_locales = ['de']
|
54
|
+
self.set_locale('en').should == 'de'
|
55
|
+
end
|
51
56
|
end
|
52
57
|
end
|
@@ -4,11 +4,30 @@ require File.join(current_folder,'..','spec_helper')
|
|
4
4
|
FastGettext.add_text_domain('test',:path=>File.join(File.dirname(__FILE__),'..','locale'))
|
5
5
|
FastGettext.text_domain = 'test'
|
6
6
|
FastGettext.available_locales = ['en','de']
|
7
|
-
FastGettext.locale = 'de'
|
8
7
|
|
9
8
|
include FastGettext::Translation
|
10
9
|
|
11
10
|
describe FastGettext::Translation do
|
11
|
+
before do
|
12
|
+
FastGettext.available_locales = ['en','de']
|
13
|
+
FastGettext.locale = 'de'
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "unknown locale" do
|
17
|
+
before do
|
18
|
+
FastGettext.available_locales = nil
|
19
|
+
FastGettext.locale = 'xx'
|
20
|
+
end
|
21
|
+
|
22
|
+
it "does not translate" do
|
23
|
+
_('car').should == 'car'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "does not translate plurals" do
|
27
|
+
n_('car','cars',2).should == 'cars'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
12
31
|
describe :_ do
|
13
32
|
it "translates simple text" do
|
14
33
|
_('car').should == 'Auto'
|
@@ -22,6 +41,7 @@ describe FastGettext::Translation do
|
|
22
41
|
it "translates pluralized" do
|
23
42
|
n_('Axis','Axis',1).should == 'Achse'
|
24
43
|
n_('Axis','Axis',2).should == 'Achsen'
|
44
|
+
n_('Axis','Axis',0).should == 'Achse'
|
25
45
|
end
|
26
46
|
it "returns the appropriate msgid if no translation was found" do
|
27
47
|
n_('NOTFOUND','NOTFOUNDs',1).should == 'NOTFOUND'
|
data/spec/fast_gettext_spec.rb
CHANGED
@@ -9,6 +9,7 @@ include FastGettext
|
|
9
9
|
|
10
10
|
describe FastGettext do
|
11
11
|
it "provides access to FastGettext::Translations methods" do
|
12
|
+
FastGettext._('car').should == 'Auto'
|
12
13
|
_('car').should == 'Auto'
|
13
14
|
s_("XXX|not found").should == "not found"
|
14
15
|
n_('Axis','Axis',1).should == 'Achse'
|
data/vendor/empty.mo
ADDED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grosser-fast_gettext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Grosser
|
@@ -25,6 +25,7 @@ files:
|
|
25
25
|
- vendor/iconv.rb
|
26
26
|
- vendor/string.rb
|
27
27
|
- vendor/mofile.rb
|
28
|
+
- vendor/empty.mo
|
28
29
|
- vendor/README.rdoc
|
29
30
|
- lib/fast_gettext.rb
|
30
31
|
- lib/fast_gettext
|
@@ -44,6 +45,7 @@ files:
|
|
44
45
|
- spec/fast_gettext/translation_spec.rb
|
45
46
|
- spec/fast_gettext/storage_spec.rb
|
46
47
|
- spec/fast_gettext/mo_file_spec.rb
|
48
|
+
- spec/aa_unconfigued_spec.rb
|
47
49
|
- spec/vendor
|
48
50
|
- spec/vendor/iconv_spec.rb
|
49
51
|
- spec/vendor/fake_load_path
|