Commit c8e1774a08b12ffab2c8ad035419051bfc12da07

Authored by Andrew Kane
2 parents 120de656 42a9b06e

Merge branch 'firewalker06-localize-format'

CHANGELOG.md
  1 +## 2.4.0 [unreleased]
  2 +
  3 +- Added localization
  4 +
1 5 ## 2.3.1
2 6  
3 7 - Added `series: false` option for arrays and hashes
... ...
lib/groupdate/magic.rb
  1 +require "i18n"
  2 +
1 3 module Groupdate
2 4 class Magic
3 5 attr_accessor :field, :options
... ... @@ -226,6 +228,7 @@ module Groupdate
226 228 series = series.to_a.reverse
227 229 end
228 230  
  231 + locale = options[:locale] || I18n.locale
229 232 key_format =
230 233 if options[:format]
231 234 if options[:format].respond_to?(:call)
... ... @@ -243,7 +246,7 @@ module Groupdate
243 246 when :month_of_year
244 247 key = Date.new(2014, key, 1).to_time
245 248 end
246   - key.strftime(options[:format].to_s)
  249 + I18n.localize(key, format: options[:format].to_s, locale: locale)
247 250 end
248 251 end
249 252 else
... ...
test/test_helper.rb
... ... @@ -26,6 +26,14 @@ end
26 26 class Post < ActiveRecord::Base
27 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 37 # migrations
30 38 %w(postgresql mysql2).each do |adapter|
31 39 ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil
... ... @@ -715,6 +723,19 @@ module TestGroupdate
715 723 assert_format :month_of_year, "Jan", "%b"
716 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 739 def test_format_multiple_groups
719 740 create_user "2014-03-01 00:00:00 UTC"
720 741 assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count
... ...