Permalink
Browse files

initial import.

  • Loading branch information...
0 parents commit a66351212ab446a4007d682d5836f3d5e85102b5 @clemens committed Aug 3, 2008
10 README
@@ -0,0 +1,10 @@
+localized_dates
+=================
+
+TODO: Write some cool info. ;-)
+
+
+Copyright (c) 2008 Clemens Kofler <clemens@railway.at>, released under the MIT license
+
+http://www.railway.at
+http://github.com/clemens/localized_dates
@@ -0,0 +1,11 @@
+require 'rake'
+require 'spec/rake/spectask'
+
+desc 'Default: run specs.'
+task :default => :spec
+
+desc 'Run the specs'
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
@@ -0,0 +1 @@
+require 'localized_dates'
@@ -0,0 +1,9 @@
+require 'fileutils'
+
+locales_dir = File.join(RAILS_ROOT, 'config', 'locales')
+Dir.mkdir(locales_dir) unless File.directory?(locales_dir)
+
+locales_template_dir = File.join(File.dirname(__FILE__), 'lib', 'templates', 'locales')
+['en-US.rb', 'de-AT.rb'].each do |locale|
+ FileUtils.cp(File.join(locales_template_dir, locale), File.join(locales_dir, locale))
+end
@@ -0,0 +1,23 @@
+::Date.class_eval do
+ def to_formatted_s(format = :default)
+ formats = ::Date::DATE_FORMATS
+ formatter = formats[format]
+
+ unless formatter
+ formatters = I18n.translate(:'date.formats') || {}
+ formatter = formatters[format]
+ end
+
+ format_to_localize = formatter.respond_to?(:call) ? formatter.call(self) : formatter
+ I18n.localize(self, :format => format_to_localize)
+ end
+ alias_method :to_s, :to_formatted_s
+end
+
+::Date.const_set('DATE_FORMATS', {
+ :db => "%Y-%m-%d",
+ :number => "%Y%m%d",
+ :rfc822 => "%e %b %Y"
+})
+
+puts '... l10n Date hack loaded!'
@@ -0,0 +1,18 @@
+::DateTime.class_eval do
+ def to_formatted_s(format = :default)
+ formats = ::Time::DATE_FORMATS
+ formatter = formats[format]
+
+ unless formatter
+ default_formatters, datetime_formatters = I18n.translate([:'time.formats', :'time.datetime.formats']) || [{},{}]
+ formatters = default_formatters.merge(datetime_formatters)
+ formatter = formatters[format]
+ end
+
+ format_to_localize = formatter.respond_to?(:call) ? formatter.call(self) : formatter
+ I18n.localize(self, :format => format_to_localize)
+ end
+ alias_method :to_s, :to_formatted_s
+end
+
+puts '... l10n DateTime hack loaded!'
@@ -0,0 +1,23 @@
+::Time.class_eval do
+ def to_formatted_s(format = :default)
+ formats = ::Time::DATE_FORMATS
+ formatter = formats[format]
+
+ unless formatter
+ formatters = I18n.translate(:'time.formats') || {}
+ formatter = formatters[format]
+ end
+
+ format_to_localize = formatter.respond_to?(:call) ? formatter.call(self) : formatter
+ I18n.localize(self, :format => format_to_localize)
+ end
+ alias_method :to_s, :to_formatted_s
+end
+
+::Time.const_set('DATE_FORMATS', {
+ :db => "%Y-%m-%d %H:%M:%S",
+ :number => "%Y%m%d%H%M%S",
+ :rfc822 => "%a, %d %b %Y %H:%M:%S %z"
+})
+
+puts '... l10n Time hack loaded!'
@@ -0,0 +1,18 @@
+ActiveSupport::TimeWithZone.class_eval do
+ def to_s(format = :default)
+ return utc.to_s(format) if format == :db
+ formats = ::Time::DATE_FORMATS
+ formatter = formats[format]
+
+ unless formatter
+ default_formatters, twz_formatters = I18n.translate([:'time.formats', :'time.time_with_zone.formats']) || [{},{}]
+ formatters = default_formatters.merge(twz_formatters)
+ formatter = formatters[format]
+ end
+
+ format_to_localize = formatter.respond_to?(:call) ? formatter.call(self) : formatter
+ I18n.localize(self, :format => format_to_localize)
+ end
+end
+
+puts '... l10n TimeWithZone hack loaded!'
@@ -0,0 +1,6 @@
+require 'core_ext/date'
+require 'core_ext/datetime'
+require 'core_ext/time'
+require 'core_ext/time_with_zone'
+
+puts '... l10n dates and times loaded!'
@@ -0,0 +1,34 @@
+I18n.backend.store_translations :'de-AT', {
+ :date => {
+ :formats => {
+ :default => "%d.%m.%Y",
+ :short => lambda { |date| "#{date.day}. %b" },
+ :long => lambda { |date| "#{date.day}. %b %Y" }
+ },
+ :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
+ :abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
+ :month_names => [nil] + %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember),
+ :abbr_month_names => [nil] + %w(Jan Feb Mär Apr Mai Jun Jul Aug Sep Okt Nov Dez),
+ :order => [:day, :month, :year]
+ },
+ :time => {
+ :formats => {
+ :default => lambda { |time| "%A, #{time.day}. %B %Y, %H:%M Uhr" },
+ :time => "%H:%M Uhr",
+ :short => lambda { |time| "#{time.day}. %B, %H:%M Uhr" },
+ :long => lambda { |time| "%A, #{time.day}. %B %Y, %H:%M Uhr" }
+ },
+ :datetime => {
+ :formats => {
+ :default => "%Y-%m-%dT%H:%M:%S%Z"
+ }
+ },
+ :time_with_zone => {
+ :formats => {
+ :default => lambda { |time| "%d.%m.%Y, %H:%M:%S #{time.formatted_offset(false, 'UTC')}" }
+ }
+ },
+ :am => '',
+ :pm => ''
+ }
+}
@@ -0,0 +1,36 @@
+I18n.backend.store_translations :'en-US', {
+ :date => {
+ :formats => {
+ :default => "%Y-%m-%d",
+ :short => "%e %b",
+ :long => "%B %e, %Y",
+ :long_ordinal => lambda { |date| "%B #{date.day.ordinalize}, %Y" }
+ },
+ :day_names => Date::DAYNAMES,
+ :abbr_day_names => Date::ABBR_DAYNAMES,
+ :month_names => Date::MONTHNAMES,
+ :abbr_month_names => Date::ABBR_MONTHNAMES,
+ :order => [:year, :month, :day]
+ },
+ :time => {
+ :formats => {
+ :default => "%a %b %d %H:%M:%S %Z %Y",
+ :time => "%H:%M",
+ :short => "%d %b %H:%M",
+ :long => "%B %d, %Y %H:%M",
+ :long_ordinal => lambda { |time| "%B #{time.day.ordinalize}, %Y %H:%M" }
+ },
+ :datetime => {
+ :formats => {
+ :default => "%Y-%m-%dT%H:%M:%S%Z"
+ }
+ },
+ :time_with_zone => {
+ :formats => {
+ :default => lambda { |time| "%Y-%m-%d %H:%M:%S #{time.formatted_offset(false, 'UTC')}" }
+ }
+ },
+ :am => 'am',
+ :pm => 'pm'
+ }
+}
@@ -0,0 +1 @@
+# Logfile created on Sun Aug 03 20:31:41 +0200 2008 by /
@@ -0,0 +1,55 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "Date and Time localization" do
+ before(:each) do
+ @date_defaults = { :default => "%Y-%m-%d",
+ :short => "%e %b",
+ :long => "%B %e, %Y",
+ :long_ordinal => lambda { |date| "%B #{date.day.ordinalize}, %Y" } }
+ @time_defaults = { :default => "%a %b %d %H:%M:%S %Z %Y",
+ :time => "%H:%M",
+ :short => "%d %b %H:%M",
+ :long => "%B %d, %Y %H:%M",
+ :long_ordinal => lambda { |time| "%B #{time.day.ordinalize}, %Y %H:%M" } }
+ @datetime_defaults = { :default => "%Y-%m-%dT%H:%M:%S%Z" }
+ @time_with_zone_defaults = {
+ :default => lambda { |time| "%Y-%m-%d %H:%M:%S #{time.formatted_offset(false, 'UTC')}" }
+ }
+
+ I18n.backend.store_translations 'en-US', :date => { :formats => @date_defaults },
+ :time => { :formats => @time_defaults, :datetime => { :formats => @datetime_defaults },
+ :time_with_zone => { :formats => @time_with_zone_defaults } }
+ end
+
+ describe Date do
+ it "should translate format when to_s is called" do
+ I18n.should_receive(:translate).with(:'date.formats').and_return(@date_defaults)
+ Date.today.to_s(:short)
+ end
+ end
+
+ describe Time do
+ it "should translate format when to_s is called" do
+ I18n.should_receive(:translate).with(:'time.formats').and_return(@time_defaults)
+ Time.now.to_s(:short)
+ end
+ end
+
+ describe DateTime do
+ it "should translate format when to_s is called" do
+ I18n.should_receive(:translate).with([:'time.formats', :'time.datetime.formats']).
+ and_return([@time_defaults, @datetime_defaults])
+ DateTime.now.to_s(:short)
+ end
+ end
+
+ describe ActiveSupport::TimeWithZone do
+ it "should translate format when to_s is called" do
+ I18n.should_receive(:translate).with([:'time.formats', :'time.time_with_zone.formats']).
+ and_return([@time_defaults, @time_with_zone_defaults])
+
+ t, z = Time.utc(2000, 1, 1, 0), ActiveSupport::TimeZone['UTC']
+ ActiveSupport::TimeWithZone.new(t, z).to_s(:short)
+ end
+ end
+end
@@ -0,0 +1,10 @@
+begin
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
+rescue LoadError
+ puts "You need to install rspec in your base app"
+ exit
+end
+
+plugin_spec_dir = File.dirname(__FILE__)
+ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
+
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :l10n_dates_and_times do
+# # Task goes here
+# end

0 comments on commit a663512

Please sign in to comment.