Chris@0: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@119: require File.expand_path('../../../../test_helper', __FILE__) Chris@0: Chris@0: class Redmine::I18nTest < ActiveSupport::TestCase Chris@0: include Redmine::I18n Chris@0: include ActionView::Helpers::NumberHelper Chris@441: Chris@0: def setup Chris@1115: User.current.language = nil Chris@1115: end Chris@1115: Chris@1115: def teardown Chris@1115: set_language_if_valid 'en' Chris@0: end Chris@441: Chris@0: def test_date_format_default Chris@0: set_language_if_valid 'en' Chris@0: today = Date.today Chris@441: Setting.date_format = '' Chris@119: assert_equal I18n.l(today), format_date(today) Chris@0: end Chris@441: Chris@0: def test_date_format Chris@0: set_language_if_valid 'en' Chris@0: today = Date.today Chris@0: Setting.date_format = '%d %m %Y' Chris@0: assert_equal today.strftime('%d %m %Y'), format_date(today) Chris@0: end Chris@441: Chris@1115: def test_date_format_default_with_user_locale Chris@1115: set_language_if_valid 'es' Chris@1115: today = now = Time.parse('2011-02-20 14:00:00') Chris@1115: Setting.date_format = '%d %B %Y' Chris@1115: User.current.language = 'fr' Chris@1115: s1 = "20 f\xc3\xa9vrier 2011" Chris@1115: s1.force_encoding("UTF-8") if s1.respond_to?(:force_encoding) Chris@1115: assert_equal s1, format_date(today) Chris@1115: User.current.language = nil Chris@1115: assert_equal '20 Febrero 2011', format_date(today) Chris@1115: end Chris@1115: Chris@0: def test_date_and_time_for_each_language Chris@0: Setting.date_format = '' Chris@0: valid_languages.each do |lang| Chris@0: set_language_if_valid lang Chris@0: assert_nothing_raised "#{lang} failure" do Chris@0: format_date(Date.today) Chris@0: format_time(Time.now) Chris@0: format_time(Time.now, false) Chris@441: assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), Chris@441: "date.formats.default missing in #{lang}" Chris@441: assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), Chris@441: "time.formats.time missing in #{lang}" Chris@0: end Chris@0: assert l('date.day_names').is_a?(Array) Chris@0: assert_equal 7, l('date.day_names').size Chris@441: Chris@0: assert l('date.month_names').is_a?(Array) Chris@0: assert_equal 13, l('date.month_names').size Chris@0: end Chris@0: end Chris@441: Chris@1115: def test_time_for_each_zone Chris@1115: ActiveSupport::TimeZone.all.each do |zone| Chris@1115: User.current.stubs(:time_zone).returns(zone.name) Chris@1115: assert_nothing_raised "#{zone} failure" do Chris@1115: format_time(Time.now) Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@245: def test_time_format Chris@245: set_language_if_valid 'en' Chris@245: now = Time.parse('2011-02-20 15:45:22') Chris@245: with_settings :time_format => '%H:%M' do Chris@245: with_settings :date_format => '' do Chris@245: assert_equal '02/20/2011 15:45', format_time(now) Chris@245: assert_equal '15:45', format_time(now, false) Chris@245: end Chris@245: with_settings :date_format => '%Y-%m-%d' do Chris@245: assert_equal '2011-02-20 15:45', format_time(now) Chris@245: assert_equal '15:45', format_time(now, false) Chris@245: end Chris@245: end Chris@245: end Chris@441: Chris@0: def test_time_format_default Chris@0: set_language_if_valid 'en' Chris@245: now = Time.parse('2011-02-20 15:45:22') Chris@245: with_settings :time_format => '' do Chris@245: with_settings :date_format => '' do Chris@245: assert_equal '02/20/2011 03:45 pm', format_time(now) Chris@245: assert_equal '03:45 pm', format_time(now, false) Chris@245: end Chris@245: with_settings :date_format => '%Y-%m-%d' do Chris@245: assert_equal '2011-02-20 03:45 pm', format_time(now) Chris@245: assert_equal '03:45 pm', format_time(now, false) Chris@245: end Chris@245: end Chris@0: end Chris@441: Chris@1115: def test_time_format_default_with_user_locale Chris@1115: set_language_if_valid 'en' Chris@1115: User.current.language = 'fr' Chris@1115: now = Time.parse('2011-02-20 15:45:22') Chris@1115: with_settings :time_format => '' do Chris@1115: with_settings :date_format => '' do Chris@1115: assert_equal '20/02/2011 15:45', format_time(now) Chris@1115: assert_equal '15:45', format_time(now, false) Chris@1115: end Chris@1115: with_settings :date_format => '%Y-%m-%d' do Chris@1115: assert_equal '2011-02-20 15:45', format_time(now) Chris@1115: assert_equal '15:45', format_time(now, false) Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@0: def test_time_format Chris@0: set_language_if_valid 'en' Chris@0: now = Time.now Chris@0: Setting.date_format = '%d %m %Y' Chris@0: Setting.time_format = '%H %M' Chris@0: assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) Chris@0: assert_equal now.strftime('%H %M'), format_time(now, false) Chris@0: end Chris@441: Chris@0: def test_utc_time_format Chris@0: set_language_if_valid 'en' Chris@441: now = Time.now Chris@0: Setting.date_format = '%d %m %Y' Chris@0: Setting.time_format = '%H %M' Chris@441: assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc) Chris@441: assert_equal now.strftime('%H %M'), format_time(now.utc, false) Chris@0: end Chris@441: Chris@0: def test_number_to_human_size_for_each_language Chris@0: valid_languages.each do |lang| Chris@0: set_language_if_valid lang Chris@0: assert_nothing_raised "#{lang} failure" do Chris@1115: size = number_to_human_size(257024) Chris@1115: assert_match /251/, size Chris@0: end Chris@0: end Chris@0: end Chris@441: Chris@1115: def test_day_name Chris@1115: set_language_if_valid 'fr' Chris@1115: assert_equal 'dimanche', day_name(0) Chris@1115: assert_equal 'jeudi', day_name(4) Chris@1115: end Chris@1115: Chris@1115: def test_day_letter Chris@1115: set_language_if_valid 'fr' Chris@1115: assert_equal 'd', day_letter(0) Chris@1115: assert_equal 'j', day_letter(4) Chris@1115: end Chris@1115: Chris@1115: def test_number_to_currency_for_each_language Chris@1115: valid_languages.each do |lang| Chris@1115: set_language_if_valid lang Chris@1115: assert_nothing_raised "#{lang} failure" do Chris@1115: number_to_currency(-1000.2) Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: def test_number_to_currency_default Chris@1115: set_language_if_valid 'bs' Chris@1115: assert_equal "KM -1000,20", number_to_currency(-1000.2) Chris@1115: set_language_if_valid 'de' Chris@1115: euro_sign = "\xe2\x82\xac" Chris@1115: euro_sign.force_encoding('UTF-8') if euro_sign.respond_to?(:force_encoding) Chris@1115: assert_equal "-1000,20 #{euro_sign}", number_to_currency(-1000.2) Chris@1115: end Chris@1115: Chris@0: def test_valid_languages Chris@0: assert valid_languages.is_a?(Array) Chris@0: assert valid_languages.first.is_a?(Symbol) Chris@0: end Chris@441: Chris@1115: def test_languages_options Chris@1115: options = languages_options Chris@1115: Chris@1115: assert options.is_a?(Array) Chris@1115: assert_equal valid_languages.size, options.size Chris@1115: assert_nil options.detect {|option| !option.is_a?(Array)} Chris@1115: assert_nil options.detect {|option| option.size != 2} Chris@1115: assert_nil options.detect {|option| !option.first.is_a?(String) || !option.last.is_a?(String)} Chris@1115: assert_include ["English", "en"], options Chris@1115: end Chris@1115: Chris@1115: def test_locales_validness Chris@1115: lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size Chris@1115: assert_equal lang_files_count, valid_languages.size Chris@1115: valid_languages.each do |lang| Chris@1115: assert set_language_if_valid(lang) Chris@1115: end Chris@1115: set_language_if_valid('en') Chris@1115: end Chris@1115: Chris@0: def test_valid_language Chris@0: to_test = {'fr' => :fr, Chris@0: 'Fr' => :fr, Chris@0: 'zh' => :zh, Chris@0: 'zh-tw' => :"zh-TW", Chris@0: 'zh-TW' => :"zh-TW", Chris@0: 'zh-ZZ' => nil } Chris@0: to_test.each {|lang, expected| assert_equal expected, find_language(lang)} Chris@0: end Chris@441: Chris@119: def test_fallback Chris@119: ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) Chris@119: ::I18n.locale = 'en' Chris@119: assert_equal "Untranslated string", l(:untranslated) Chris@119: ::I18n.locale = 'fr' Chris@119: assert_equal "Untranslated string", l(:untranslated) Chris@441: Chris@119: ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) Chris@119: ::I18n.locale = 'en' Chris@119: assert_equal "Untranslated string", l(:untranslated) Chris@119: ::I18n.locale = 'fr' Chris@119: assert_equal "Pas de traduction", l(:untranslated) Chris@119: end Chris@441: Chris@441: def test_utf8 Chris@441: set_language_if_valid 'ja' Chris@441: str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84" Chris@441: i18n_ja_yes = l(:general_text_Yes) Chris@441: if str_ja_yes.respond_to?(:force_encoding) Chris@441: str_ja_yes.force_encoding('UTF-8') Chris@441: assert_equal "UTF-8", i18n_ja_yes.encoding.to_s Chris@441: end Chris@441: assert_equal str_ja_yes, i18n_ja_yes Chris@441: end Chris@1115: Chris@1115: def test_traditional_chinese_locale Chris@1115: set_language_if_valid 'zh-TW' Chris@1115: str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)" Chris@1115: if str_tw.respond_to?(:force_encoding) Chris@1115: str_tw.force_encoding('UTF-8') Chris@1115: end Chris@1115: assert_equal str_tw, l(:general_lang_name) Chris@1115: end Chris@1115: Chris@1115: def test_french_locale Chris@1115: set_language_if_valid 'fr' Chris@1115: str_fr = "Fran\xc3\xa7ais" Chris@1115: if str_fr.respond_to?(:force_encoding) Chris@1115: str_fr.force_encoding('UTF-8') Chris@1115: end Chris@1115: assert_equal str_fr, l(:general_lang_name) Chris@1115: end Chris@0: end