Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding wlorentson's patch for nested hash lookups/returns

  • Loading branch information...
commit 949329f92208f3f0ca6aa86a30d097853f288221 1 parent 6a8bdee
Dylan Stamat authored
Showing with 47 additions and 38 deletions.
  1. +1 −1  README.textile
  2. +4 −0 lib/models/locale.rb
  3. +42 −37 spec/localize_spec.rb
2  README.textile
View
@@ -4,7 +4,7 @@ Stores your translations in the database, rather than yaml files. As you tag it
h2. DISCLAIMER!
-* In implementing this into another project, I realized that the currency support is currently broken. It basically boils down to the i18n gem accessing some of the Rails i18n yaml's in a different way than others. In this case, it's requesting a parent key, and expects a hash to be returned containing the children. This is not baked in the plugin at the moment, and any contributions toward this would be well received.
+* -In implementing this into another project, I realized that the currency support is currently broken. It basically boils down to the i18n gem accessing some of the Rails i18n yaml's in a different way than others. In this case, it's requesting a parent key, and expects a hash to be returned containing the children. This is not baked in the plugin at the moment, and any contributions toward this would be well received.- Thank you wlorentson!
* The translations_controller is unprotected, and you'll probably want to add some kind of authorization filter to it to make sure the outside world can't access it.
4 lib/models/locale.rb
View
@@ -5,6 +5,10 @@ class Locale < ActiveRecord::Base
has_many :translations, :dependent => :destroy
named_scope :non_defaults, :conditions => ["code != ?", I18n.default_locale.to_s]
+ #named_scope :english, lambda { |m| { return Hash.new if m.nil?; :conditions => "locales.locale = '#{m}'" } }
+# named_scope :in_city, lambda { |m| { return {} if m.nil?; :joins => [cities], :conditions => "cities.name = '#{m}' } }
+
+
@@default_locale = nil
def self.default_locale
79 spec/localize_spec.rb
View
@@ -9,54 +9,59 @@
@backend.cache_store.clear
end
- describe "with default locale en" do
- before(:each) do
- I18n.default_locale = "en"
- # load database with default active_support translations for english locale
- I18nUtil.load_from_yml 'vendor/rails/activesupport/lib/active_support/locale/en.yml'
- end
-
- describe "and locale en" do
+ # load database with default active_support translations for english locale
+ # FIXME: remove this dependency
+ unless File.exists?('vendor/rails/activesupport/lib/active_support/locale/en.yml')
+ puts "\n[warn] A few date tests require rails to be vendorized. See spec/localize_spec.rb. This needs to be fixed.\n"
+ else
+ describe "with default locale en" do
before(:each) do
- I18n.locale = "en"
+ I18n.default_locale = "en"
+ I18nUtil.load_from_yml 'vendor/rails/activesupport/lib/active_support/locale/en.yml'
end
- it "should localize dates in different formats" do
- dates = {
- Date.new(2009,1,1) => {:default => "2009-01-01", :short => "Jan 01", :long => "January 01, 2009"},
- Date.new(2009,1,31) => {:default => "2009-01-31", :short => "Jan 31", :long => "January 31, 2009"},
- Date.new(2009,2,2) => {:default => "2009-02-02", :short => "Feb 02", :long => "February 02, 2009"},
- Date.new(2009,2,28) => {:default => "2009-02-28", :short => "Feb 28", :long => "February 28, 2009"},
- Date.new(2008,2,29) => {:default => "2008-02-29", :short => "Feb 29", :long => "February 29, 2008"},
- Date.new(2009,3,11) => {:default => "2009-03-11", :short => "Mar 11", :long => "March 11, 2009"},
- Date.new(2010,4,30) => {:default => "2010-04-30", :short => "Apr 30", :long => "April 30, 2010"},
- Date.new(2020,12,31) => {:default => "2020-12-31", :short => "Dec 31", :long => "December 31, 2020"}
- }
-
- dates.each do |date, formats|
- formats.each do |format, expected_value|
- @backend.localize("en", date, format).should == expected_value
+ describe "and locale en" do
+ before(:each) do
+ I18n.locale = "en"
+ end
+
+ it "should localize dates in different formats" do
+ dates = {
+ Date.new(2009,1,1) => {:default => "2009-01-01", :short => "Jan 01", :long => "January 01, 2009"},
+ Date.new(2009,1,31) => {:default => "2009-01-31", :short => "Jan 31", :long => "January 31, 2009"},
+ Date.new(2009,2,2) => {:default => "2009-02-02", :short => "Feb 02", :long => "February 02, 2009"},
+ Date.new(2009,2,28) => {:default => "2009-02-28", :short => "Feb 28", :long => "February 28, 2009"},
+ Date.new(2008,2,29) => {:default => "2008-02-29", :short => "Feb 29", :long => "February 29, 2008"},
+ Date.new(2009,3,11) => {:default => "2009-03-11", :short => "Mar 11", :long => "March 11, 2009"},
+ Date.new(2010,4,30) => {:default => "2010-04-30", :short => "Apr 30", :long => "April 30, 2010"},
+ Date.new(2020,12,31) => {:default => "2020-12-31", :short => "Dec 31", :long => "December 31, 2020"}
+ }
+
+ dates.each do |date, formats|
+ formats.each do |format, expected_value|
+ @backend.localize("en", date, format).should == expected_value
+ end
end
end
- end
- it "should localize times in different formats" do
- time = DateTime.new(y=2008,m=3,d=22,h=16,min=30,s=12)
+ it "should localize times in different formats" do
+ time = DateTime.new(y=2008,m=3,d=22,h=16,min=30,s=12)
+
+ @backend.localize("en", time, :default).should == "Sat, 22 Mar 2008 16:30:12 +0000"
+ @backend.localize("en", time, :short).should == "22 Mar 16:30"
+ @backend.localize("en", time, :long).should == "March 22, 2008 16:30"
+ @backend.localize("en", time, '%B %d, %Y %H:%M %p').should == "March 22, 2008 16:30 pm"
+ end
- @backend.localize("en", time, :default).should == "Sat, 22 Mar 2008 16:30:12 +0000"
- @backend.localize("en", time, :short).should == "22 Mar 16:30"
- @backend.localize("en", time, :long).should == "March 22, 2008 16:30"
- @backend.localize("en", time, '%B %d, %Y %H:%M %p').should == "March 22, 2008 16:30 pm"
end
- end
+ describe "and locale es" do
+ before(:each) do
+ I18n.locale = "es"
+ @spanish_locale = Locale.create!(:code => 'es')
+ end
- describe "and locale es" do
- before(:each) do
- I18n.locale = "es"
- @spanish_locale = Locale.create!(:code => 'es')
end
-
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.