grosser-fast_gettext 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
1
  FastGettext
2
2
  ===========
3
- GetText but 3.5 x faster, 20 x less memory, simple, clean namespace (7 vs 34) and threadsave!
3
+ GetText but 3.5 x faster, 560 x less memory, simple, clean namespace (7 vs 34) and threadsave!
4
+
5
+ It supports multiple backends (atm: .mo files, .po files, ActiveRecord, Chain) and can easily be extended.
4
6
 
5
7
  [Example Rails application](https://github.com/grosser/gettext_i18n_rails_example)
6
8
 
@@ -12,13 +14,6 @@ Or from source:
12
14
  git clone git://github.com/grosser/fast_gettext.git
13
15
  cd fast_gettext && rake install
14
16
 
15
- Generate .po or .mo files using GetText parser (example tasks at [gettext_i18n_rails](http://github.com/grosser/gettext_i18n_rails))
16
-
17
- Tell Gettext where your .mo or .po files lie:
18
- #e.g. for locale/de/my_app.po and locale/de/LC_MESSAGES/my_app.mo
19
- #add :type=>:po and it will read directly from po files (not recommended for production since po-parsing can crash!)
20
- FastGettext.add_text_domain('my_app',:path=>'locale')
21
-
22
17
  Choose text domain and locale for translation
23
18
  FastGettext.text_domain = 'my_app'
24
19
  FastGettext.available_locales = ['de','en','fr','en_US','en_UK'] # only allow these locales to be set (optional)
@@ -34,28 +29,50 @@ Start translating
34
29
  Disable translation errors(like no text domain setup) while doing e.g. console session / testing
35
30
  FastGettext.silence_errors
36
31
 
32
+ Translations
33
+ ============
34
+ ### Default: .mo-files
35
+ Generate .po or .mo files using GetText parser (example tasks at [gettext_i18n_rails](http://github.com/grosser/gettext_i18n_rails))
36
+
37
+ Tell Gettext where your .mo or .po files lie:
38
+ #e.g. for locale/de/my_app.po and locale/de/LC_MESSAGES/my_app.mo
39
+ #add :type=>:po and it will read directly from po files (not recommended for production since po-parsing can crash!)
40
+ FastGettext.add_text_domain('my_app',:path=>'locale')
41
+
42
+ ATM you have to use the [original GetText](http://github.com/mutoh/gettext) to create and manage your po/mo-files.
43
+ I already started work on a po/mo parser & reader that is easier to use, contributions welcome @ [pomo](http://github.com/grosser/pomo)
44
+
45
+ ###Database
46
+ !!!This is very new/alpha-ish atm and surely will change!!!
47
+ Eesy to maintain especially with many translations and multiple locales.
48
+ ATM the default implementation is a bit clumsy, it is build so that the model can be substituted by your own.
49
+ FastGettext.add_text_domain('my_app', :type=>:db, :model=>TranslationKey)
50
+ #the model should be the model that represents the keys, you can use FastGettext::TranslationRepository::DB::TranslationKey
51
+ #the model must have string:key and translations
52
+ #a Translation must have string:text and string:locale
53
+
54
+ To get started have a look at the tests in `spec/fast_gettext/translation_repository/db_spec.rb`(includes migrations) or the models
55
+ in `lib/fast_gettext/translation_repository/db/translation_key`.
56
+
37
57
  Performance
38
58
  ===========
39
- 50_000 translations speed / memory
40
- small translation file <-> large translation file
59
+ 50_000 translations speed / memory
60
+ small translation file <-> large translation file
61
+ (ruby enterprise 1.8.6, your results may vary, try `rake benchmark`)
41
62
  Baseline: (doing nothing in a loop)
42
- 0.240000s / 1196K <->
63
+ 0.250000s / 0K <->
43
64
 
44
65
  Ideal: (primitive Hash lookup)
45
- 0.790000s / 1200K <-> 0.760000s / 1200K
66
+ 0.820000s / 4K <-> 0.760000s / 4K
46
67
 
47
68
  FastGettext:
48
- 1.360000s / 1200K <-> 1.340000s / 1200K
69
+ 1.360000s / 8K <-> 1.320000s / 8K
49
70
 
50
71
  GetText 2.0.1:
51
- 4.810000s / 6.176K <-> 4.770000s / 6.176K
72
+ 4.880000s / 4480K <-> 4.810000s / 4480K
52
73
 
53
74
  ActiveSupport I18n::Backend::Simple :
54
- 21.800000s / 11296K <->
55
-
56
-
57
-
58
-
75
+ 21.770000s / 10100K <->
59
76
 
60
77
  Thread Safety and Rails
61
78
  =======================
@@ -88,11 +105,6 @@ then e.g. controllers, so set them inside your application_controller.
88
105
  include FastGettext::Translation
89
106
  ...
90
107
 
91
- Updating translations
92
- =====================
93
- ATM you have to use the [original GetText](http://github.com/mutoh/gettext) to create and manage your po/mo-files.
94
- I already started work on a po/mo parser & reader that is easier to use, contributions welcome @ [pomo](http://github.com/grosser/pomo)
95
-
96
108
  Advanced features
97
109
  =================
98
110
  ###Abnormal pluralisation
@@ -127,7 +139,7 @@ Unfound may not always mean missing, if you chose not to translate a word becaus
127
139
  A lambda or anything that responds to `call` will do as callback. A good starting point may be `examples/missing_translations_logger.rb`.
128
140
 
129
141
  ###Plugins
130
- Want a yml, xml, database version ?
142
+ Want a yml, xml version ?
131
143
  Write your own TranslationRepository!
132
144
  #fast_gettext/translation_repository/xxx.rb
133
145
  module FastGettext
@@ -145,6 +157,8 @@ FAQ
145
157
 
146
158
  TODO
147
159
  ====
160
+ - some cleanup required, repositories should not have locale
161
+ - DB::TranslationKey responds_to? :available_locales should be false when it is not defined, maybe testing bug
148
162
  - use `default_locale=(x)` internally, atm the default is available_locales.first || 'en'
149
163
  - use `default_text_domain=(x)` internally, atm default is nil...
150
164
 
data/Rakefile CHANGED
@@ -6,6 +6,7 @@ task :default do |t|
6
6
  end
7
7
 
8
8
  task :benchmark do
9
+ puts "Running on #{RUBY}"
9
10
  %w[baseline ideal fast_gettext original i18n_simple].each do |bench|
10
11
  puts `ruby benchmark/#{bench}.rb`
11
12
  puts ""
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 4
4
- :patch: 2
4
+ :patch: 3
@@ -0,0 +1,59 @@
1
+ require 'active_record'
2
+ module FastGettext
3
+ module TranslationRepository
4
+ # Responsibility:
5
+ # - provide access to translations in database through a database abstraction
6
+ #
7
+ # Options:
8
+ # :model => Model that represents your keys
9
+ # you can either use the models supplied under db/, extend them or build your own
10
+ # only constraints:
11
+ # key: find_by_key, translations
12
+ # translation: text, locale
13
+ class DB
14
+ def initialize(name,options={})
15
+ @model = options[:model]
16
+ end
17
+
18
+ @@seperator = '||||' # string that seperates multiple plurals
19
+ def self.seperator=(sep);@@seperator = sep;end
20
+ def self.seperator;@@seperator;end
21
+
22
+ def available_locales
23
+ if @model.respond_to? :available_locales
24
+ @model.available_locales
25
+ else
26
+ []
27
+ end
28
+ end
29
+
30
+ def pluralisation_rule
31
+ if @model.respond_to? :pluralsation_rule
32
+ @model.pluralsation_rule
33
+ else
34
+ nil
35
+ end
36
+ end
37
+
38
+ def [](key)
39
+ translation = translation(key) and translation.text
40
+ end
41
+
42
+ def plural(*args)
43
+ translation = translation(args*self.class.seperator)
44
+ if translation
45
+ translation.text.to_s.split(self.class.seperator)
46
+ else
47
+ []
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def translation(key)
54
+ return unless key = @model.find_by_key(key)
55
+ key.translations.find_by_locale(FastGettext.locale)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,12 @@
1
+ class FastGettext::TranslationRepository::DB::TranslationKey < ActiveRecord::Base
2
+ has_many :translations, :class_name=>'TranslationText'
3
+ validates_uniqueness_of :key
4
+ validates_presence_of :key
5
+
6
+ # TODO this should not be necessary, but
7
+ # TranslationKey.respond_to? :available_locales
8
+ # returns true even if this is not defined...
9
+ def self.available_locales
10
+ []
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ class FastGettext::TranslationRepository::DB::TranslationText < ActiveRecord::Base
2
+ belongs_to :key, :class_name=>'TranslationKey'
3
+ validates_presence_of :locale, :text
4
+ validates_uniqueness_of :locale, :scope=>:translation_key_id
5
+ end
@@ -0,0 +1,71 @@
1
+ current_folder = File.dirname(__FILE__)
2
+ require File.join(current_folder,'..','..','spec_helper')
3
+
4
+ require 'activerecord'
5
+ require 'fast_gettext/translation_repository/db'
6
+ require 'fast_gettext/translation_repository/db/translation_key'
7
+ require 'fast_gettext/translation_repository/db/translation_text'
8
+
9
+ include FastGettext::TranslationRepository
10
+
11
+ describe FastGettext::TranslationRepository::DB do
12
+ before :all do
13
+ ActiveRecord::Base.establish_connection({
14
+ :adapter => "sqlite3",
15
+ :database => ":memory:"
16
+ })
17
+
18
+ #create model table
19
+ #TODO surpress output ?
20
+ ActiveRecord::Schema.define(:version => 1) do
21
+ create_table :translation_keys do |t|
22
+ t.string :key, :unique=>true, :null=>false
23
+ t.timestamps
24
+ end
25
+ create_table :translation_texts do |t|
26
+ t.string :text, :locale
27
+ t.integer :translation_key_id, :null=>false
28
+ t.timestamps
29
+ end
30
+ end
31
+ end
32
+
33
+ before do
34
+ DB::TranslationKey.delete_all
35
+ DB::TranslationText.delete_all
36
+ FastGettext.locale = 'de'
37
+ @rep = FastGettext::TranslationRepository::DB.new('x', :model=>DB::TranslationKey)
38
+ end
39
+
40
+ def create_translation(key, text)
41
+ translation_key = DB::TranslationKey.create!(:key=>key)
42
+ DB::TranslationText.create!(:translation_key_id=>translation_key.id, :text=>text, :locale=>'de')
43
+ end
44
+
45
+ it "can be built" do
46
+ # pending #this is weird, it responds to :available_locales, but it is not defined...
47
+ @rep.available_locales.should == []
48
+ end
49
+
50
+ it "has no pluralisation_rule by default" do
51
+ @rep.pluralisation_rule.should == nil
52
+ end
53
+
54
+ it "cannot translate when no models are present" do
55
+ @rep['car'].should == nil
56
+ end
57
+
58
+ it "can translate" do
59
+ create_translation 'car', 'Auto'
60
+ @rep['car'].should == 'Auto'
61
+ end
62
+
63
+ it "cannot pluralize when no model is present" do
64
+ @rep.plural('Axis','Axis').should == []
65
+ end
66
+
67
+ it "can pluralize" do
68
+ create_translation 'Axis||||Axis', 'Achse||||Achsen'
69
+ @rep.plural('Axis','Axis').should == ['Achse','Achsen']
70
+ end
71
+ 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.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-22 00:00:00 -07:00
12
+ date: 2009-04-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -34,6 +34,9 @@ files:
34
34
  - lib/fast_gettext/translation_repository.rb
35
35
  - lib/fast_gettext/translation_repository/base.rb
36
36
  - lib/fast_gettext/translation_repository/chain.rb
37
+ - lib/fast_gettext/translation_repository/db.rb
38
+ - lib/fast_gettext/translation_repository/db/translation_key.rb
39
+ - lib/fast_gettext/translation_repository/db/translation_text.rb
37
40
  - lib/fast_gettext/translation_repository/logger.rb
38
41
  - lib/fast_gettext/translation_repository/mo.rb
39
42
  - lib/fast_gettext/translation_repository/po.rb
@@ -42,6 +45,7 @@ files:
42
45
  - spec/fast_gettext/storage_spec.rb
43
46
  - spec/fast_gettext/translation_repository/base_spec.rb
44
47
  - spec/fast_gettext/translation_repository/chain_spec.rb
48
+ - spec/fast_gettext/translation_repository/db_spec.rb
45
49
  - spec/fast_gettext/translation_repository/logger_spec.rb
46
50
  - spec/fast_gettext/translation_repository/mo_spec.rb
47
51
  - spec/fast_gettext/translation_repository/po_spec.rb
@@ -98,6 +102,7 @@ test_files:
98
102
  - spec/fast_gettext/translation_repository/logger_spec.rb
99
103
  - spec/fast_gettext/translation_repository/base_spec.rb
100
104
  - spec/fast_gettext/translation_repository/po_spec.rb
105
+ - spec/fast_gettext/translation_repository/db_spec.rb
101
106
  - spec/fast_gettext/translation_repository/mo_spec.rb
102
107
  - spec/fast_gettext/mo_file_spec.rb
103
108
  - spec/aa_unconfigued_spec.rb