grosser-fast_gettext 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -44,15 +44,15 @@ I already started work on a po/mo parser & reader that is easier to use, contrib
44
44
 
45
45
  ###Database
46
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.
47
+ Easy to maintain especially with many translations and multiple locales.
48
+ An example migration for ActiveRecord can be found in `exaples/db/migration`.
49
+ This is usable with any model DataMapper/Sequel or any other(non-database) backend, the only thing you need to do is respond to the self.translation(key, locale) call.
50
+ The default plural seperator is `||||` but you may overwrite it (or suggest a better one..).
51
+ include FastGettext::TranslationRepository::DB.require_models
49
52
  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
+ #the model should be the model that represents the keys, you can use FastGettext::TranslationRepository::DBModel::TranslationKey
53
54
 
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`.
55
+ To get started have a look at the default models in `lib/fast_gettext/translation_repository/db/translation_key`.
56
56
 
57
57
  Performance
58
58
  ===========
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 4
4
- :patch: 3
4
+ :patch: 4
@@ -0,0 +1,22 @@
1
+ class CreateTranslationTables < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :translation_keys do |t|
4
+ t.string :key, :unique=>true, :null=>false
5
+ t.add_index :key
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :translation_texts do |t|
10
+ t.text :text
11
+ t.string :locale
12
+ t.integer :translation_key_id, :null=>false
13
+ t.add_index :translation_key_id
14
+ t.timestamps
15
+ end
16
+ end
17
+
18
+ def self.down
19
+ drop_table :translation_keys
20
+ drop_table :translation_texts
21
+ end
22
+ end
@@ -21,7 +21,7 @@ module FastGettext
21
21
 
22
22
  def available_locales
23
23
  if @model.respond_to? :available_locales
24
- @model.available_locales
24
+ @model.available_locales || []
25
25
  else
26
26
  []
27
27
  end
@@ -36,23 +36,21 @@ module FastGettext
36
36
  end
37
37
 
38
38
  def [](key)
39
- translation = translation(key) and translation.text
39
+ @model.translation(key, FastGettext.locale)
40
40
  end
41
41
 
42
42
  def plural(*args)
43
- translation = translation(args*self.class.seperator)
44
- if translation
45
- translation.text.to_s.split(self.class.seperator)
43
+ if translation = @model.translation(args*self.class.seperator, FastGettext.locale)
44
+ translation.to_s.split(self.class.seperator)
46
45
  else
47
46
  []
48
47
  end
49
48
  end
50
49
 
51
- protected
52
-
53
- def translation(key)
54
- return unless key = @model.find_by_key(key)
55
- key.translations.find_by_locale(FastGettext.locale)
50
+ def self.require_models
51
+ require 'fast_gettext/translation_repository/db_models/translation_key'
52
+ require 'fast_gettext/translation_repository/db_models/translation_text'
53
+ FastGettext::TranslationRepository::DBModels
56
54
  end
57
55
  end
58
56
  end
@@ -0,0 +1,15 @@
1
+ module FastGettext::TranslationRepository
2
+ module DBModels
3
+ class TranslationKey < ActiveRecord::Base
4
+ has_many :translations, :class_name=>'TranslationText'
5
+ validates_uniqueness_of :key
6
+ validates_presence_of :key
7
+
8
+ def self.translation(key, locale)
9
+ return unless translation_key = find_by_key(key)
10
+ return unless translation_text = translation_key.translations.find_by_locale(locale)
11
+ translation_text.text
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module FastGettext::TranslationRepository
2
+ module DBModels
3
+ class TranslationText < ActiveRecord::Base
4
+ belongs_to :key, :class_name=>'TranslationKey'
5
+ validates_presence_of :locale, :text
6
+ validates_uniqueness_of :locale, :scope=>:translation_key_id
7
+ end
8
+ end
9
+ end
@@ -3,10 +3,7 @@ require File.join(current_folder,'..','..','spec_helper')
3
3
 
4
4
  require 'activerecord'
5
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
6
+ include FastGettext::TranslationRepository::DB.require_models
10
7
 
11
8
  describe FastGettext::TranslationRepository::DB do
12
9
  before :all do
@@ -31,15 +28,15 @@ describe FastGettext::TranslationRepository::DB do
31
28
  end
32
29
 
33
30
  before do
34
- DB::TranslationKey.delete_all
35
- DB::TranslationText.delete_all
31
+ TranslationKey.delete_all
32
+ TranslationText.delete_all
36
33
  FastGettext.locale = 'de'
37
- @rep = FastGettext::TranslationRepository::DB.new('x', :model=>DB::TranslationKey)
34
+ @rep = FastGettext::TranslationRepository::DB.new('x', :model=>TranslationKey)
38
35
  end
39
36
 
40
37
  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')
38
+ translation_key = TranslationKey.create!(:key=>key)
39
+ TranslationText.create!(:translation_key_id=>translation_key.id, :text=>text, :locale=>'de')
43
40
  end
44
41
 
45
42
  it "can be built" do
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.3
4
+ version: 0.4.4
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-24 00:00:00 -07:00
12
+ date: 2009-04-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,7 @@ files:
25
25
  - README.markdown
26
26
  - Rakefile
27
27
  - VERSION.yml
28
+ - examples/db/migration.rb
28
29
  - examples/missing_translation_logger.rb
29
30
  - lib/fast_gettext.rb
30
31
  - lib/fast_gettext/mo_file.rb
@@ -35,8 +36,8 @@ files:
35
36
  - lib/fast_gettext/translation_repository/base.rb
36
37
  - lib/fast_gettext/translation_repository/chain.rb
37
38
  - 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
39
+ - lib/fast_gettext/translation_repository/db_models/translation_key.rb
40
+ - lib/fast_gettext/translation_repository/db_models/translation_text.rb
40
41
  - lib/fast_gettext/translation_repository/logger.rb
41
42
  - lib/fast_gettext/translation_repository/mo.rb
42
43
  - lib/fast_gettext/translation_repository/po.rb
@@ -109,4 +110,5 @@ test_files:
109
110
  - spec/vendor/iconv_spec.rb
110
111
  - spec/vendor/fake_load_path/iconv.rb
111
112
  - spec/vendor/string_spec.rb
113
+ - examples/db/migration.rb
112
114
  - examples/missing_translation_logger.rb
@@ -1,12 +0,0 @@
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
@@ -1,5 +0,0 @@
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