grosser-fast_gettext 0.1.0 → 0.2.0

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 CHANGED
@@ -12,6 +12,7 @@ Tell Gettext where your mo-files lie:
12
12
 
13
13
  Choose text domain, and locale for translation
14
14
  FastGettext.text_domain = 'my_app'
15
+ FastGettext.available_locales = ['de','en','fr','en_US','en_UK'] # only allow these locales to be set (optional)
15
16
  FastGettext.locale = 'de'
16
17
 
17
18
  Start translating
@@ -21,11 +22,6 @@ Start translating
21
22
  s_('Namespace|no-found') == 'not-found'
22
23
  n_('Axis','Axis',3) == 'Achsen' #German plural of Axis
23
24
 
24
- Thread-safety
25
- =============
26
- locale/text_domain/available_locales are not shared between threads.
27
- But text_domains is, so that found translations can be reused.
28
-
29
25
  Speed
30
26
  =====
31
27
  FastGettext
@@ -54,7 +50,7 @@ ATM you have to use the [original GetText](http://github.com/mutoh/gettext) to c
54
50
 
55
51
  Author
56
52
  ======
57
- Mofile parsing from Masao Mutoh, see vender/README
53
+ Mofile parsing from Masao Mutoh, see vendor/README
58
54
 
59
55
  Michael Grosser
60
56
  grosser.michael@gmail.com
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :minor: 1
2
+ :minor: 2
3
3
  :patch: 0
4
4
  :major: 0
@@ -1,24 +1,30 @@
1
1
  module FastGettext
2
2
  module Storage
3
- [:locale,:text_domain,:available_locales].each do |method|
4
- key = "FastGettext.#{method}"
3
+ [:text_domain,:available_locales,:text_domains].each do |method|
5
4
  define_method method do
6
- Thread.current[key]
5
+ thread_store(method)
7
6
  end
8
7
  define_method "#{method}=" do |value|
9
- Thread.current[key] = value
8
+ write_thread_store(method,value)
10
9
  end
11
10
  end
12
11
 
13
- #NOT THREADSAFE, for speed/caching
14
- @@text_domains = {}
15
-
16
- def text_domains
17
- @@text_domains
12
+ def locale
13
+ thread_store(:locale) || (available_locales||[]).first || 'en'
18
14
  end
19
15
 
20
- def text_domains=(value)
21
- @@text_domains=value
16
+ def locale=(value)
17
+ write_thread_store(:locale,value) if not available_locales or available_locales.include?(value)
18
+ end
19
+
20
+ private
21
+
22
+ def thread_store(key)
23
+ Thread.current["FastGettext.#{key}"]
24
+ end
25
+
26
+ def write_thread_store(key,value)
27
+ Thread.current["FastGettext.#{key}"]=value
22
28
  end
23
29
  end
24
30
  end
data/lib/fast_gettext.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'fast_gettext/mo_file'
2
2
  require 'fast_gettext/storage'
3
+ require File.join(File.dirname(__FILE__),'..','vendor','string')
3
4
 
4
5
  module FastGettext
5
6
  include FastGettext::Storage
@@ -35,6 +36,16 @@ module FastGettext
35
36
  end
36
37
  end
37
38
 
39
+ #tell gettext: this string need translation (will be found during parsing)
40
+ def N_(translate)
41
+ translate
42
+ end
43
+
44
+ #tell gettext: this string need translation (will be found during parsing)
45
+ def Nn_(singular,plural)
46
+ [singular,plural]
47
+ end
48
+
38
49
  def add_text_domain(name,options)
39
50
  self.text_domains ||= {}
40
51
  domain = self.text_domains[name] = {:path=>options.delete(:path),:mo_files=>{}}
@@ -5,25 +5,41 @@ include FastGettext::Storage
5
5
 
6
6
  describe Storage do
7
7
  def thread_save(method)
8
- send("#{method}=",1)
8
+ send("#{method}=",'de')
9
9
 
10
10
  # mess around with other threads
11
- threads = []
12
11
  100.times do |i|
13
- threads << Thread.new {send("#{method}=",i)}
12
+ Thread.new {FastGettext.send("#{method}=",'en')}
14
13
  end
15
- threads.each(&:join)
16
14
 
17
- send(method) == 1
15
+ send(method) == 'de'
18
16
  end
19
17
 
20
- [:locale, :available_locales, :text_domain].each do |method|
18
+ [:locale, :available_locales, :text_domain, :text_domains].each do |method|
21
19
  it "stores #{method} thread-save" do
22
- thread_save(:locale).should == true
20
+ thread_save(method).should == true
23
21
  end
24
22
  end
25
23
 
26
- it "does not store text_domains thread-save" do
27
- thread_save(:text_domains).should == false
24
+ describe :locale do
25
+ it "stores everything as long as available_locales is not set" do
26
+ self.available_locales = nil
27
+ self.locale = 'XXX'
28
+ locale.should == 'XXX'
29
+ end
30
+ it "is en if no locale and no available_locale were set" do
31
+ Thread.current['FastGettext.locale']=nil
32
+ self.available_locales = nil
33
+ locale.should == 'en'
34
+ end
35
+ it "is the first available_locale if one was set" do
36
+ self.available_locales = ['de']
37
+ locale.should == 'de'
38
+ end
39
+ it "does not store a locale if it is not available" do
40
+ self.available_locales = ['de']
41
+ self.locale = 'en'
42
+ locale.should == 'de'
43
+ end
28
44
  end
29
45
  end
@@ -40,4 +40,16 @@ describe FastGettext do
40
40
  s_("XXX/not found",'/').should == "not found"
41
41
  end
42
42
  end
43
+
44
+ describe :N_ do
45
+ it "returns the msgid" do
46
+ N_('XXXXX').should == 'XXXXX'
47
+ end
48
+ end
49
+
50
+ describe :Nn_ do
51
+ it "returns the msgids as array" do
52
+ Nn_('X','Y').should == ['X','Y']
53
+ end
54
+ end
43
55
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # ---- requirements
2
- $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift File.expand_path("../lib", File.dirname(__FILE__))
3
3
  require 'fast_gettext'
4
4
 
5
5
  # ---- bugfix
@@ -0,0 +1,12 @@
1
+ current_folder = File.dirname(__FILE__)
2
+ require File.join(current_folder,'..','spec_helper')
3
+
4
+ #just to make sure we did not mess up while copying...
5
+ describe String do
6
+ it "substitudes using % + Hash" do
7
+ "x%{name}y" %{:name=>'a'}.should == 'xay'
8
+ end
9
+ it "substitudes using % + Array" do
10
+ ("x%sy%s" % ['a','b']).should == 'xayb'
11
+ end
12
+ end
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -40,6 +40,8 @@ files:
40
40
  - spec/fast_gettext
41
41
  - spec/fast_gettext/storage_spec.rb
42
42
  - spec/fast_gettext/mo_file_spec.rb
43
+ - spec/vendor
44
+ - spec/vendor/string_spec.rb
43
45
  has_rdoc: true
44
46
  homepage: http://github.com/grosser/fast_gettext
45
47
  post_install_message: