Commit c8e1774a08b12ffab2c8ad035419051bfc12da07

Authored by Andrew Kane
2 parents 120de656 42a9b06e

Merge branch 'firewalker06-localize-format'

  1 +## 2.4.0 [unreleased]
  2 +
  3 +- Added localization
  4 +
1 ## 2.3.1 5 ## 2.3.1
2 6
3 - Added `series: false` option for arrays and hashes 7 - Added `series: false` option for arrays and hashes
lib/groupdate/magic.rb
  1 +require "i18n"
  2 +
1 module Groupdate 3 module Groupdate
2 class Magic 4 class Magic
3 attr_accessor :field, :options 5 attr_accessor :field, :options
@@ -226,6 +228,7 @@ module Groupdate @@ -226,6 +228,7 @@ module Groupdate
226 series = series.to_a.reverse 228 series = series.to_a.reverse
227 end 229 end
228 230
  231 + locale = options[:locale] || I18n.locale
229 key_format = 232 key_format =
230 if options[:format] 233 if options[:format]
231 if options[:format].respond_to?(:call) 234 if options[:format].respond_to?(:call)
@@ -243,7 +246,7 @@ module Groupdate @@ -243,7 +246,7 @@ module Groupdate
243 when :month_of_year 246 when :month_of_year
244 key = Date.new(2014, key, 1).to_time 247 key = Date.new(2014, key, 1).to_time
245 end 248 end
246 - key.strftime(options[:format].to_s) 249 + I18n.localize(key, format: options[:format].to_s, locale: locale)
247 end 250 end
248 end 251 end
249 else 252 else
test/test_helper.rb
@@ -26,6 +26,14 @@ end @@ -26,6 +26,14 @@ end
26 class Post < ActiveRecord::Base 26 class Post < ActiveRecord::Base
27 end 27 end
28 28
  29 +# i18n
  30 +I18n.enforce_available_locales = true
  31 +I18n.backend.store_translations :de, {
  32 + :date => {
  33 + :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil)
  34 + }
  35 +}
  36 +
29 # migrations 37 # migrations
30 %w(postgresql mysql2).each do |adapter| 38 %w(postgresql mysql2).each do |adapter|
31 ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil 39 ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil
@@ -715,6 +723,19 @@ module TestGroupdate @@ -715,6 +723,19 @@ module TestGroupdate
715 assert_format :month_of_year, "Jan", "%b" 723 assert_format :month_of_year, "Jan", "%b"
716 end 724 end
717 725
  726 + def test_format_locale
  727 + create_user "2014-10-01 00:00:00 UTC"
  728 + assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b", locale: :de).count
  729 + end
  730 +
  731 + def test_format_locale_global
  732 + create_user "2014-10-01 00:00:00 UTC"
  733 + I18n.locale = :de
  734 + assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b").count
  735 + ensure
  736 + I18n.locale = :en
  737 + end
  738 +
718 def test_format_multiple_groups 739 def test_format_multiple_groups
719 create_user "2014-03-01 00:00:00 UTC" 740 create_user "2014-03-01 00:00:00 UTC"
720 assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count 741 assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count