trendi18n 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,6 +46,12 @@ If you want to use trendi18n in your app you should do three steps:
46
46
 
47
47
  = Changelog
48
48
 
49
+ === 0.9.3
50
+
51
+ * Return all translations form scope, when scope given as a key
52
+ * Trendi18n::Backend::Trendi18n.available_locales use data both from localization files and from database
53
+ * Translation.get_locales return an array of symbols in place of an array of strings
54
+
49
55
  === 0.9.2
50
56
 
51
57
  * Backend reloading system (multi-threading supported)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.2
1
+ 0.9.3
@@ -12,6 +12,11 @@ class Translation < ActiveRecord::Base
12
12
 
13
13
  @@locales = []
14
14
 
15
+ # return true if there is no translation with key == name, and there is some translations with scope == name
16
+ def self.scope?(name, locale = I18n.locale.to_s)
17
+ !self.exists?(:key => name, :locale => locale, :scope => nil) && self.exists?(:scope => name, :locale => locale)
18
+ end
19
+
15
20
  # return locales which translations are stored in db
16
21
  def self.get_locales
17
22
  @@locales
@@ -19,7 +24,7 @@ class Translation < ActiveRecord::Base
19
24
 
20
25
  # read locales which translation are stored in db
21
26
  def self.set_locales
22
- @@locales = self.all(:select => "DISTINCT(locale)", :order => "locale ASC").map { |obj| obj.locale }
27
+ @@locales = self.all(:select => "DISTINCT(locale)", :order => "locale ASC").map { |obj| obj.locale.to_sym }
23
28
  end
24
29
 
25
30
  # Set @read_time, date and time of first database read on current update.
@@ -126,9 +131,21 @@ class Translation < ActiveRecord::Base
126
131
  end
127
132
 
128
133
  # return hash ready to by stored as translation
129
- def to_translation_hash
130
- path = self.scope.nil? ? [self.key] : self.scope.split(".") << self.key
134
+ def to_translation_hash()
135
+ path = []
136
+ path += self.scope.split(".") unless self.scope.nil?
137
+ path << self.key
131
138
  return path.reverse.inject(self) {|before, step| { step => before}}
132
139
  end
133
140
 
141
+ # return ready to by stored as translations hash of translations in scope
142
+ def self.scope_to_translation_hash(scope, locale = I18n.locale.to_s)
143
+ children = self.find_by_sql ["SELECT * FROM translations WHERE scope LIKE ? and locale=? ORDER BY id", scope + "%", locale]
144
+ hash = {}
145
+ for child in children
146
+ hash.deep_merge! child.to_translation_hash()
147
+ end
148
+ Trendi18n::ScopeTranslations.new(locale, hash)
149
+ end
150
+
134
151
  end
@@ -10,7 +10,7 @@ module Trendi18n
10
10
  # return available locales, based on informaton form Translation model
11
11
  def available_locales
12
12
  Translation.set_read_time
13
- Translation.get_locales
13
+ Translation.get_locales | nested.available_locales
14
14
  end
15
15
 
16
16
  # translate key in locale using options
@@ -27,6 +27,13 @@ module Trendi18n
27
27
  # read values of scope and default options and delete them form options
28
28
  values = options.reject {|name, value| [:scope, :default].include?(name)}
29
29
 
30
+
31
+ # cache and return all translation from scope if it's used as key
32
+ if Translation.scope?(key_with_scope = [scope, key].delete_if{|x| x.blank?}.join("."), loc = locale.to_s)
33
+ cache_translation(result = Translation.scope_to_translation_hash(key_with_scope, loc))
34
+ return result.to_translation_hash[key.to_s]
35
+ end
36
+
30
37
  if key.is_a?(Symbol) # if key is a Symbol
31
38
  begin
32
39
  return nested.translate(locale, key, options) # use nested translete
@@ -79,6 +86,13 @@ module Trendi18n
79
86
  store_translations(translation.locale, translation.to_translation_hash)
80
87
  end
81
88
 
89
+ # cache all translations in scope
90
+ def cache_translations_from_scope(scope_translation)
91
+ for hash in scope_translation.to_translation_hash
92
+ cache_translation(ScopeTranslations.new(scope_translation.locale, hash))
93
+ end
94
+ end
95
+
82
96
  end
83
97
  end
84
98
  end
@@ -0,0 +1,13 @@
1
+ module Trendi18n
2
+
3
+ # Simple class use to caching hash of translations in scope
4
+ class ScopeTranslations
5
+
6
+ attr_accessor :locale, :to_translation_hash
7
+
8
+ def initialize(locale, to_translation_hash)
9
+ @locale, @to_translation_hash = locale, to_translation_hash
10
+ end
11
+ end
12
+
13
+ end
@@ -1,6 +1,7 @@
1
1
  require 'trendi18n/backend/trendi18n'
2
2
  require 'trendi18n/generator/commands'
3
3
  require 'trendi18n/handler'
4
+ require 'trendi18n/scope_translations'
4
5
  require 'file'
5
6
 
6
7
  I18n.backend = Trendi18n::Backend::Trendi18n.new
@@ -39,6 +39,6 @@ Rails::Initializer.run do |config|
39
39
 
40
40
 
41
41
  # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
42
- # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
43
- # config.i18n.default_locale = :de
42
+ config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}')]
43
+ config.i18n.default_locale = :en
44
44
  end
@@ -0,0 +1,2 @@
1
+ fr:
2
+ hello: "Bonjour univers!"
@@ -10,9 +10,6 @@ Given /^I have relocalized "([^\"]*)" from "([^\"]*)" to "([^\"]*)"$/ do |key, o
10
10
  Translation.first(:conditions => {:key => key, :locale => old_locale}).update_attribute('locale', new_locale)
11
11
  end
12
12
 
13
- When /^I reloading locales$/ do
14
- Translation.set_locales
15
- end
16
13
 
17
14
 
18
15
 
@@ -39,6 +39,23 @@ describe Trendi18n::Backend::Trendi18n do
39
39
  I18n.reload!
40
40
  I18n.t("test_key")
41
41
  end
42
+
43
+ it "should cache all translations from scope, when scope is used as key" do
44
+ Translation.create!(:key => "key_down", :scope => "testscope", :locale => "en")
45
+ Translation.create!(:key => "key_up", :scope => "testscope", :locale => "en")
46
+ I18n.t(:testscope, :locale => "en")
47
+ Translation.should_not_receive(:lookup)
48
+ I18n.t(:key_down, :scope => "testscope", :locale => "en")
49
+ end
50
+
51
+ it "should cache all translations from scope and subscopes, when scope is used as key" do
52
+ Translation.create!(:key => "key_down", :scope => "testscope.subtest", :locale => "en")
53
+ Translation.create!(:key => "key_up", :scope => "testscope", :locale => "en")
54
+ I18n.t(:testscope, :locale => "en")
55
+ Translation.should_not_receive(:lookup)
56
+ I18n.t(:key_down, :scope => "testscope.subtest", :locale => "en")
57
+ end
58
+
42
59
  describe "up-to-date status" do
43
60
 
44
61
  it "should be up-to-date when there is no translations" do
@@ -142,10 +159,15 @@ describe Trendi18n::Backend::Trendi18n do
142
159
 
143
160
  end
144
161
 
145
- describe "while scope given as a key" do
146
162
 
147
- it "should translate to hash of translations"
163
+ end
148
164
 
149
- end
165
+ it "should return availables_locales merged form db and files" do
166
+ Translation.create!(:locale => "pl", :key => "al_test", :translation => "al_test_in_polish")
167
+ Translation.set_locales
168
+ I18n.backend.available_locales.include?(:pl).should == true
169
+ I18n.backend.available_locales.include?(:en).should == true # created by before(each)
170
+ I18n.backend.available_locales.include?(:fr).should == true # in localization file
150
171
  end
172
+
151
173
  end
@@ -99,7 +99,9 @@ describe Translation do
99
99
  Translation.create!(:key => "key", :locale => "en")
100
100
  Translation.create!(:key => "key", :locale => "nl")
101
101
  Translation.set_locales
102
- Translation.get_locales.should == ["en", "nl", "pl"]
102
+ Translation.get_locales.include?(:en).should == true
103
+ Translation.get_locales.include?(:nl).should == true
104
+ Translation.get_locales.include?(:pl).should == true
103
105
  end
104
106
 
105
107
  end
@@ -111,4 +113,31 @@ describe Translation do
111
113
  I18n.t("with_count", :count => 2).should == "many (2)"
112
114
  end
113
115
 
116
+ describe "scope recognizing" do
117
+
118
+ it "should recognize something is scope, not key" do
119
+ Translation.create!(:key => "key", :locale => "en", :scope => "scope")
120
+ Translation.scope?("scope", "en").should == true
121
+ end
122
+
123
+ it "should recognize something is key, when scope with the same name exists and key has no scope" do
124
+ Translation.create!(:key => "scope", :locale => "en")
125
+ Translation.create!(:key => "key", :scope => "scope", :locale => "en")
126
+ Translation.scope?("scope", "en").should == false
127
+ end
128
+
129
+ it "should recognize something is scope, when key with the same name exists and has some scope" do
130
+ Translation.create!(:key => "scope", :locale => "en", :scope => "real.scope")
131
+ Translation.create!(:key => "key", :locale => "en", :scope => "scope")
132
+ Translation.scope?("scope", "en").should == true
133
+ end
134
+
135
+ it "should recognize something is scope, when key with the same name exists, but has different locale" do
136
+ Translation.create!(:key => "scope", :locale => "pl")
137
+ Translation.create!(:key => "key", :scope => "scope", :locale => "en")
138
+ Translation.scope?("scope", "pl").should == false
139
+ Translation.scope?("scope", "en").should == true
140
+ end
141
+
142
+ end
114
143
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trendi18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Misiurek
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-12-30 00:00:00 +01:00
14
+ date: 2010-01-15 00:00:00 +01:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -46,6 +46,7 @@ files:
46
46
  - lib/trendi18n/backend/trendi18n.rb
47
47
  - lib/trendi18n/generator/commands.rb
48
48
  - lib/trendi18n/handler.rb
49
+ - lib/trendi18n/scope_translations.rb
49
50
  - rails/init.rb
50
51
  - spec/test_application/README
51
52
  - spec/test_application/Rakefile
@@ -68,6 +69,7 @@ files:
68
69
  - spec/test_application/config/initializers/new_rails_defaults.rb
69
70
  - spec/test_application/config/initializers/session_store.rb
70
71
  - spec/test_application/config/locales/en.yml
72
+ - spec/test_application/config/locales/fr.yml
71
73
  - spec/test_application/config/routes.rb
72
74
  - spec/test_application/db/migrate/20091208195455_create_translations.rb
73
75
  - spec/test_application/db/schema.rb