grosser-fast_gettext 0.3.4 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +5 -1
- data/VERSION.yml +1 -1
- data/lib/fast_gettext/storage.rb +16 -1
- data/spec/fast_gettext/storage_spec.rb +51 -3
- metadata +1 -1
data/README.markdown
CHANGED
@@ -64,7 +64,7 @@ and do not need to be readded for every thread (parsing takes time...).
|
|
64
64
|
Try the [gettext_i18n_rails plugin](http://github.com/grosser/gettext_i18n_rails), it simplifies the setup.
|
65
65
|
|
66
66
|
Setting `available_locales`,`text_domain` or `locale` will not work inside the `evironment.rb`, since it runs in a different thread
|
67
|
-
then e.g. controllers, so set them inside your application_controller.
|
67
|
+
then e.g. controllers, so set them inside your application_controller.
|
68
68
|
|
69
69
|
#environment.rb after initializers
|
70
70
|
FastGettext.add_text_domain('accounting',:path=>'locale')
|
@@ -96,6 +96,10 @@ Advanced features
|
|
96
96
|
Pluralisation rules can be set directly via a lambda (see code/specs), or by using the Gettext
|
97
97
|
plural definition (see spec/locale/en/test_plural.po or [Plural expressions for all languages](http://translate.sourceforge.net/wiki/l10n/pluralforms).
|
98
98
|
|
99
|
+
###default_text_domain
|
100
|
+
If you only use one text domain, setting `FastGettext.default_text_domain = 'app'`
|
101
|
+
is sufficient and no more `text_domain=` is needed
|
102
|
+
|
99
103
|
###Plugins
|
100
104
|
Want a yml, xml, database version ?
|
101
105
|
Write your own TranslationRepository!
|
data/VERSION.yml
CHANGED
data/lib/fast_gettext/storage.rb
CHANGED
@@ -10,7 +10,7 @@ module FastGettext
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
[:available_locales,:
|
13
|
+
[:available_locales,:_locale,:current_cache].each do |method_name|
|
14
14
|
key = "fast_gettext_#{method_name}".to_sym
|
15
15
|
define_method method_name do
|
16
16
|
Thread.current[key]
|
@@ -23,11 +23,26 @@ module FastGettext
|
|
23
23
|
#so initial translations does not crash
|
24
24
|
Thread.current[:fast_gettext_current_cache]={}
|
25
25
|
|
26
|
+
def text_domain
|
27
|
+
Thread.current[:fast_gettext_text_domain] || default_text_domain
|
28
|
+
end
|
29
|
+
|
26
30
|
def text_domain=(new_domain)
|
27
31
|
Thread.current[:fast_gettext_text_domain]=new_domain
|
28
32
|
update_current_cache
|
29
33
|
end
|
30
34
|
|
35
|
+
#-> cattr_accessor :default_text_domain
|
36
|
+
@@default_text_domain = nil
|
37
|
+
def default_text_domain=(domain)
|
38
|
+
@@default_text_domain = domain
|
39
|
+
update_current_cache
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_text_domain
|
43
|
+
@@default_text_domain
|
44
|
+
end
|
45
|
+
|
31
46
|
#global, since re-parsing whole folders takes too much time...
|
32
47
|
@@translation_repositories={}
|
33
48
|
def translation_repositories
|
@@ -4,11 +4,15 @@ require File.join(current_folder,'..','spec_helper')
|
|
4
4
|
include FastGettext::Storage
|
5
5
|
|
6
6
|
describe 'Storage' do
|
7
|
+
before do
|
8
|
+
self.default_text_domain = nil
|
9
|
+
end
|
10
|
+
|
7
11
|
def thread_save(method)
|
8
12
|
send("#{method}=",'de')
|
9
13
|
|
10
14
|
# mess around with other threads
|
11
|
-
100.times do
|
15
|
+
100.times do
|
12
16
|
Thread.new {FastGettext.send("#{method}=",'en')}
|
13
17
|
end
|
14
18
|
|
@@ -28,35 +32,62 @@ describe 'Storage' do
|
|
28
32
|
self.translation_repositories[:x].should == 2
|
29
33
|
end
|
30
34
|
|
35
|
+
describe :default_text_domain do
|
36
|
+
it "stores default_text_domain non-thread-safe" do
|
37
|
+
self.default_text_domain=1
|
38
|
+
t = Thread.new{self.default_text_domain=2}
|
39
|
+
t.join
|
40
|
+
self.default_text_domain.should == 2
|
41
|
+
end
|
42
|
+
|
43
|
+
it "uses default_text_domain when text_domain is not set" do
|
44
|
+
self.text_domain = nil
|
45
|
+
self.default_text_domain = 'x'
|
46
|
+
text_domain.should == 'x'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "does not use default when domain is set" do
|
50
|
+
self.text_domain = 'x'
|
51
|
+
self.default_text_domain = 'y'
|
52
|
+
text_domain.should == 'x'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
31
56
|
describe :locale do
|
32
57
|
it "stores everything as long as available_locales is not set" do
|
33
58
|
self.available_locales = nil
|
34
59
|
self.locale = 'XXX'
|
35
60
|
locale.should == 'XXX'
|
36
61
|
end
|
62
|
+
|
37
63
|
it "is en if no locale and no available_locale were set" do
|
38
64
|
FastGettext.send(:_locale=,nil)
|
39
65
|
self.available_locales = nil
|
40
66
|
locale.should == 'en'
|
41
67
|
end
|
68
|
+
|
42
69
|
it "does not change the locale if locales was called with nil" do
|
43
70
|
self.locale = nil
|
44
71
|
locale.should == 'en'
|
45
72
|
end
|
73
|
+
|
46
74
|
it "is the first available_locale if one was set" do
|
47
75
|
self.available_locales = ['de']
|
48
76
|
locale.should == 'de'
|
49
77
|
end
|
78
|
+
|
50
79
|
it "does not store a locale if it is not available" do
|
51
80
|
self.available_locales = ['de']
|
52
81
|
self.locale = 'en'
|
53
82
|
locale.should == 'de'
|
54
83
|
end
|
84
|
+
|
55
85
|
it "set_locale returns the old locale if the new could not be set" do
|
56
86
|
self.locale = 'de'
|
57
87
|
self.available_locales = ['de']
|
58
88
|
self.set_locale('en').should == 'de'
|
59
89
|
end
|
90
|
+
|
60
91
|
{
|
61
92
|
'Opera' => "de-DE,de;q=0.9,en;q=0.8",
|
62
93
|
'Firefox' => "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3",
|
@@ -68,11 +99,13 @@ describe 'Storage' do
|
|
68
99
|
FastGettext.locale.should == 'de_DE'
|
69
100
|
end
|
70
101
|
end
|
102
|
+
|
71
103
|
it "sets a unimportant locale if it is the only available" do
|
72
104
|
FastGettext.available_locales = ['en','xx']
|
73
105
|
FastGettext.locale = "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"
|
74
106
|
FastGettext.locale.should == 'en'
|
75
107
|
end
|
108
|
+
|
76
109
|
it "sets locale from comma seperated" do
|
77
110
|
FastGettext.available_locales = ['de_DE','en','xx']
|
78
111
|
FastGettext.locale = "de,de-de,en"
|
@@ -84,6 +117,7 @@ describe 'Storage' do
|
|
84
117
|
before do
|
85
118
|
FastGettext.text_domain = 'xxx'
|
86
119
|
end
|
120
|
+
|
87
121
|
it "raises when a textdomain was empty" do
|
88
122
|
begin
|
89
123
|
FastGettext._('x')
|
@@ -91,7 +125,7 @@ describe 'Storage' do
|
|
91
125
|
rescue FastGettext::Storage::NoTextDomainConfigured
|
92
126
|
end
|
93
127
|
end
|
94
|
-
|
128
|
+
|
95
129
|
it "can silence erros" do
|
96
130
|
FastGettext.silence_errors
|
97
131
|
FastGettext._('x').should == 'x'
|
@@ -100,16 +134,30 @@ describe 'Storage' do
|
|
100
134
|
|
101
135
|
describe :current_cache do
|
102
136
|
before do
|
137
|
+
FastGettext.text_domain = 'xxx'
|
103
138
|
FastGettext.available_locales = ['de','en']
|
104
139
|
FastGettext.locale = 'de'
|
105
140
|
FastGettext._('abc')
|
106
141
|
FastGettext.locale = 'en'
|
107
142
|
end
|
108
143
|
|
109
|
-
it "stores a translation
|
144
|
+
it "stores a translation seperate by locale" do
|
110
145
|
FastGettext.current_cache['abc'].should == nil
|
111
146
|
end
|
112
147
|
|
148
|
+
it "stores a translation seperate by domain" do
|
149
|
+
FastGettext.locale = 'de'
|
150
|
+
FastGettext.text_domain = nil
|
151
|
+
FastGettext.current_cache['abc'].should == nil
|
152
|
+
end
|
153
|
+
|
154
|
+
it "cache is restored through setting of default_text_domain" do
|
155
|
+
FastGettext.locale = 'de'
|
156
|
+
FastGettext.text_domain = nil
|
157
|
+
FastGettext.default_text_domain = 'xxx'
|
158
|
+
FastGettext.current_cache['abc'].should == 'abc'
|
159
|
+
end
|
160
|
+
|
113
161
|
it "stores a translation permanently" do
|
114
162
|
FastGettext.locale = 'de'
|
115
163
|
FastGettext.current_cache['abc'].should == 'abc'
|