2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/time"
|
|
|
|
require "active_support/core_ext/array/conversions"
|
2008-07-02 13:21:07 -04:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class I18nTest < ActiveSupport::TestCase
|
2008-07-02 13:21:07 -04:00
|
|
|
def setup
|
|
|
|
@date = Date.parse("2008-7-2")
|
|
|
|
@time = Time.utc(2008, 7, 2, 16, 47, 1)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_time_zone_localization_with_default_format
|
2009-11-14 05:14:43 -05:00
|
|
|
now = Time.local(2000)
|
|
|
|
assert_equal now.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(now)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_date_localization_should_use_default_format
|
2008-07-16 13:51:48 -04:00
|
|
|
assert_equal @date.strftime("%Y-%m-%d"), I18n.localize(@date)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_date_localization_with_default_format
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal @date.strftime("%Y-%m-%d"), I18n.localize(@date, format: :default)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_date_localization_with_short_format
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal @date.strftime("%b %d"), I18n.localize(@date, format: :short)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_date_localization_with_long_format
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal @date.strftime("%B %d, %Y"), I18n.localize(@date, format: :long)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
|
|
|
def test_time_localization_should_use_default_format
|
2008-07-16 13:51:48 -04:00
|
|
|
assert_equal @time.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(@time)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_time_localization_with_default_format
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal @time.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(@time, format: :default)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_time_localization_with_short_format
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal @time.strftime("%d %b %H:%M"), I18n.localize(@time, format: :short)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_time_localization_with_long_format
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal @time.strftime("%B %d, %Y %H:%M"), I18n.localize(@time, format: :long)
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_day_names
|
|
|
|
assert_equal Date::DAYNAMES, I18n.translate(:'date.day_names')
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_abbr_day_names
|
|
|
|
assert_equal Date::ABBR_DAYNAMES, I18n.translate(:'date.abbr_day_names')
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_month_names
|
|
|
|
assert_equal Date::MONTHNAMES, I18n.translate(:'date.month_names')
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_abbr_month_names
|
|
|
|
assert_equal Date::ABBR_MONTHNAMES, I18n.translate(:'date.abbr_month_names')
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_date_order
|
2013-01-19 16:32:21 -05:00
|
|
|
assert_equal %w(year month day), I18n.translate(:'date.order')
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_time_am
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "am", I18n.translate(:'time.am')
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-07-02 13:21:07 -04:00
|
|
|
def test_time_pm
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "pm", I18n.translate(:'time.pm')
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|
2008-10-07 07:23:05 -04:00
|
|
|
|
2008-11-17 12:16:08 -05:00
|
|
|
def test_words_connector
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal ", ", I18n.translate(:'support.array.words_connector')
|
2008-10-07 07:23:05 -04:00
|
|
|
end
|
|
|
|
|
2008-11-17 12:16:08 -05:00
|
|
|
def test_two_words_connector
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal " and ", I18n.translate(:'support.array.two_words_connector')
|
2008-11-17 12:16:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_last_word_connector
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal ", and ", I18n.translate(:'support.array.last_word_connector')
|
2008-10-07 07:23:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_sentence
|
2008-11-17 12:16:08 -05:00
|
|
|
default_two_words_connector = I18n.translate(:'support.array.two_words_connector')
|
|
|
|
default_last_word_connector = I18n.translate(:'support.array.last_word_connector')
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "a, b, and c", %w[a b c].to_sentence
|
2016-08-06 13:38:33 -04:00
|
|
|
I18n.backend.store_translations "en", support: { array: { two_words_connector: " & " } }
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "a & b", %w[a b].to_sentence
|
2016-08-06 13:38:33 -04:00
|
|
|
I18n.backend.store_translations "en", support: { array: { last_word_connector: " and " } }
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "a, b and c", %w[a b c].to_sentence
|
2008-10-07 07:23:05 -04:00
|
|
|
ensure
|
2016-08-06 13:38:33 -04:00
|
|
|
I18n.backend.store_translations "en", support: { array: { two_words_connector: default_two_words_connector } }
|
|
|
|
I18n.backend.store_translations "en", support: { array: { last_word_connector: default_last_word_connector } }
|
2008-10-07 07:23:05 -04:00
|
|
|
end
|
2012-06-26 11:54:22 -04:00
|
|
|
|
|
|
|
def test_to_sentence_with_empty_i18n_store
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "a, b, and c", %w[a b c].to_sentence(locale: "empty")
|
2012-06-26 11:54:22 -04:00
|
|
|
end
|
2008-07-02 13:21:07 -04:00
|
|
|
end
|