Chris@0: # Redmine - project management software Chris@441: # Copyright (C) 2006-2011 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@0: @hook_module = Redmine::Hook 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@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@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@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@0: number_to_human_size(1024*1024*4) Chris@0: end Chris@0: end Chris@0: end Chris@441: 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@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@0: end