<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -59,16 +59,22 @@ module Globalize
         default = options.delete(:default)
         result = backends.inject({}) do |namespace, backend|
           begin
-            translation = backend.translate(locale.to_sym, key, options) 
+            translation = backend.translate(locale, key, options) 
             if namespace_lookup?(translation, options)
               namespace.merge! translation
             elsif translation
               return translation 
             end
           rescue I18n::MissingTranslationData
+            namespace
           end
         end
-        result || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
+        
+        if result.blank?
+          default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
+        else
+          result
+        end
       end
       
       def localize(locale, object, format = :default)</diff>
      <filename>lib/globalize/backend/chain.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,19 +1,50 @@
-require 'globalize/backend/static'
-require 'globalize/locale/fallbacks'
-require 'globalize/translation'
 require 'globalize/backend/db/db_translation'
 
 module Globalize
   module Backend
-    class Db &lt; Static
+    class Db &lt; I18n::Backend::Simple
       def init_translations
-        @translations = Hash.new
-        Globalize::Db::TranslationLocale.all.each do |locale|
-          @translations[locale.language_tag.to_sym] = locale.translation_hash
-          I18n.fallbacks.map locale.language_tag.to_sym =&gt; locale.parent.language_tag.to_sym unless locale.parent.nil?
-        end
+        @translations ||= Hash.new
+        @translation_timestamps ||= Hash.new
         @initialized = true
       end
+      
+      def translate(locale, key, options = {})
+        begin
+          super(locale.to_sym, key, options)
+        rescue I18n::MissingTranslationData =&gt; e
+          if locale.parent
+            translate(locale.parent, key, options)
+          else
+            raise e
+          end
+        end
+      end
+      
+      def translate_with_caching(locale, key, options = {})
+        if locale.is_a?(Globalize::Db::TranslationLocale)
+          init_translations unless initialized?
+          if @translation_timestamps[locale.id] != locale.updated_at
+            reload_translation(locale)
+          end
+          translate_without_caching(locale, key, options)
+        else
+          raise(I18n::MissingTranslationData.new(locale, key, options))
+        end
+      end
+      alias_method_chain :translate, :caching
+      
+      private
+      
+      def reload_translation(locale)
+        begin
+          Rails.logger.info(&quot;Reloading translation #{locale.language_tag}&quot;)
+        rescue NameError
+        end
+        
+        @translations[locale.language_tag.to_sym] = locale.translation_hash(true)
+        @translation_timestamps[locale.id] = locale.updated_at
+      end
     end
   end
 end
\ No newline at end of file</diff>
      <filename>lib/globalize/backend/db.rb</filename>
    </modified>
    <modified>
      <diff>@@ -12,7 +12,7 @@ module Globalize
         record.locale.updated_at_will_change!
         record.locale.save(false)
       end
-
+      
       private
 
       def must_have_a_value
@@ -22,22 +22,23 @@ module Globalize
     
     class TranslationLocale &lt; ActiveRecord::Base
       belongs_to :parent, :class_name =&gt; 'Globalize::Db::TranslationLocale', :foreign_key =&gt; 'parent_id'
-      has_many :children, :class_name =&gt; 'Db::TranslationLocale', :foreign_key =&gt; 'parent_id', :dependent =&gt; :nullify
+      has_many :children, :class_name =&gt; 'Globalize::Db::TranslationLocale', :foreign_key =&gt; 'parent_id', :dependent =&gt; :nullify
 
       belongs_to :creator, :class_name =&gt; &quot;User&quot;, :foreign_key =&gt; &quot;user_id&quot;
       has_many :translations, :class_name =&gt; &quot;Globalize::Db::Translation&quot;, :foreign_key =&gt; &quot;locale_id&quot;, :dependent =&gt; :destroy
 
       validates_presence_of :locale
+      validates_presence_of :name
       validates_uniqueness_of :name
 
       before_create :copy_translations_from_parent
 
-      def translation_hash
-        return @hash unless @hash.nil?
+      def translation_hash(reload = false)
+        return @hash unless (reload || @hash.nil?)
 
         @hash = Hash.new
 
-        translations.each do |translation|
+        translations(reload).each do |translation|
           branch = translation.key.split('.')
           leaf = branch.pop
 
@@ -53,6 +54,11 @@ module Globalize
       def language_tag
         new_record? ? locale : &quot;#{locale}-x-#{id}&quot;
       end
+      alias_method :to_s, :language_tag
+      
+      def to_sym
+        language_tag.to_sym
+      end
 
       def copy_translations_from(other_locale)
         other_locale.translations.each do |other_translation|</diff>
      <filename>lib/globalize/backend/db/db_translation.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,6 @@
 namespace :i18n do
   namespace :db do
-    desc &quot;Load translation yaml file into database&quot;
+    desc &quot;Load translation yaml file into database: rake i18n:db:load locale=da-x-2&quot;
     task :load =&gt; :environment do
       file = File.open(File.expand_path(&quot;config/locales/#{ENV['locale']}.yml&quot;, Rails.root))
       data = YAML::load(file)</diff>
      <filename>tasks/i18n_db.rake</filename>
    </modified>
    <modified>
      <diff>@@ -86,6 +86,18 @@ class TranslateChainedTest &lt; ActiveSupport::TestCase
   end
 
   test &quot;looks up a namespace from all backends and merges them (if a result is a hash and no count option is present)&quot; do
+    assert_raise( I18n::MissingTranslationData ) { I18n.translate :foo, :raise =&gt; true }
+    
+    @first_backend.store_translations :en, {:foo =&gt; {:bar =&gt; 'bar from first backend'}}
+    result = I18n.translate :foo
+    assert_equal( {:bar =&gt; 'bar from first backend'}, result )
+    
+    I18n.reload!
+    @last_backend.store_translations :en, {:foo =&gt; {:baz =&gt; 'baz from last backend'}}
+    result = I18n.translate :foo
+    assert_equal( {:baz =&gt; 'baz from last backend'}, result )
+
+    I18n.reload!
     @first_backend.store_translations :en, {:foo =&gt; {:bar =&gt; 'bar from first backend'}}
     @last_backend.store_translations :en, {:foo =&gt; {:baz =&gt; 'baz from last backend'}}
     result = I18n.translate :foo</diff>
      <filename>test/backends/chained_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -28,20 +28,20 @@ class DbTest &lt; ActiveSupport::TestCase
     @da_DK_locale.save
     
     I18n.backend = Globalize::Backend::Db.new
-    I18n.locale = @en_US_locale.language_tag
+    I18n.locale = @en_US_locale
   end
   
-  test &quot;returns an instance of Translation:Static&quot; do
+  test &quot;returns an instance of String&quot; do
     translation = I18n.translate :foo
-    assert_instance_of Globalize::Translation::Static, translation
+    assert_instance_of String, translation
   end
 
   test &quot;returns the translation in en-US if present&quot; do
-    assert_equal &quot;foo in en-US&quot;, I18n.translate(:foo, :locale =&gt; @en_US_locale.language_tag)
+    assert_equal &quot;foo in en-US&quot;, I18n.translate(:foo, :locale =&gt; @en_US_locale)
   end
 
   test &quot;returns the translation in parent if not present&quot; do
-    assert_equal &quot;foo in en-US&quot;, I18n.translate(:foo, :locale =&gt; @da_DK_locale.language_tag)
+    assert_equal &quot;foo in en-US&quot;, I18n.translate(:foo, :locale =&gt; @da_DK_locale)
   end
   
   test &quot;copies translation from parent when created&quot; do
@@ -51,13 +51,13 @@ class DbTest &lt; ActiveSupport::TestCase
   
   test &quot;reloads translations when asked to&quot; do
     de_locale = Globalize::Db::TranslationLocale.create(:name =&gt; 'German', :locale =&gt; 'de', :parent_id =&gt; @en_US_locale.id)
-    assert_equal &quot;foo in en-US&quot;, I18n.translate(:foo, :locale =&gt; de_locale.language_tag)
+    I18n.locale = de_locale
+    assert_equal &quot;foo in en-US&quot;, I18n.translate(:foo)
     translation = de_locale.translations.find_by_key('foo')
     translation.value = 'foo in de'
     translation.save
-    assert_equal &quot;foo in en-US&quot;, I18n.translate(:foo, :locale =&gt; de_locale.language_tag)
-    I18n.reload!
-    assert_equal &quot;foo in de&quot;, I18n.translate(:foo, :locale =&gt; de_locale.language_tag)
+    I18n.locale.reload
+    assert_equal &quot;foo in de&quot;, I18n.translate(:foo)
     
   end
 end
\ No newline at end of file</diff>
      <filename>test/backends/db_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>ba479302fc415278147a5a5a6a3f00a78fcefc8a</id>
    </parent>
  </parents>
  <author>
    <name>Mick Staugaard</name>
    <email>spam@staugaard.com</email>
  </author>
  <url>http://github.com/staugaard/globalize2/commit/77b679061dce1b00492631549659fcd42fc6bb7c</url>
  <id>77b679061dce1b00492631549659fcd42fc6bb7c</id>
  <committed-date>2009-02-18T03:08:25-08:00</committed-date>
  <authored-date>2009-01-28T07:38:29-08:00</authored-date>
  <message>fixing chain and adding tests for fixes
added caching to the db backend</message>
  <tree>a9af544fc1f27bf7c7a72a95c0fc917d12f9d16a</tree>
  <committer>
    <name>Mick Staugaard</name>
    <email>spam@staugaard.com</email>
  </committer>
</commit>
