Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require File.expand_path('../../../../test_helper', __FILE__) Chris@909: Chris@909: class Redmine::I18nTest < ActiveSupport::TestCase Chris@909: include Redmine::I18n Chris@909: include ActionView::Helpers::NumberHelper Chris@909: Chris@909: def setup Chris@909: @hook_module = Redmine::Hook Chris@909: end Chris@909: Chris@909: def test_date_format_default Chris@909: set_language_if_valid 'en' Chris@909: today = Date.today Chris@909: Setting.date_format = '' Chris@909: assert_equal I18n.l(today), format_date(today) Chris@909: end Chris@909: Chris@909: def test_date_format Chris@909: set_language_if_valid 'en' Chris@909: today = Date.today Chris@909: Setting.date_format = '%d %m %Y' Chris@909: assert_equal today.strftime('%d %m %Y'), format_date(today) Chris@909: end Chris@909: Chris@909: def test_date_and_time_for_each_language Chris@909: Setting.date_format = '' Chris@909: valid_languages.each do |lang| Chris@909: set_language_if_valid lang Chris@909: assert_nothing_raised "#{lang} failure" do Chris@909: format_date(Date.today) Chris@909: format_time(Time.now) Chris@909: format_time(Time.now, false) Chris@909: assert_not_equal 'default', ::I18n.l(Date.today, :format => :default), Chris@909: "date.formats.default missing in #{lang}" Chris@909: assert_not_equal 'time', ::I18n.l(Time.now, :format => :time), Chris@909: "time.formats.time missing in #{lang}" Chris@909: end Chris@909: assert l('date.day_names').is_a?(Array) Chris@909: assert_equal 7, l('date.day_names').size Chris@909: Chris@909: assert l('date.month_names').is_a?(Array) Chris@909: assert_equal 13, l('date.month_names').size Chris@909: end Chris@909: end Chris@909: Chris@909: def test_time_format Chris@909: set_language_if_valid 'en' Chris@909: now = Time.parse('2011-02-20 15:45:22') Chris@909: with_settings :time_format => '%H:%M' do Chris@909: with_settings :date_format => '' do Chris@909: assert_equal '02/20/2011 15:45', format_time(now) Chris@909: assert_equal '15:45', format_time(now, false) Chris@909: end Chris@909: with_settings :date_format => '%Y-%m-%d' do Chris@909: assert_equal '2011-02-20 15:45', format_time(now) Chris@909: assert_equal '15:45', format_time(now, false) Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def test_time_format_default Chris@909: set_language_if_valid 'en' Chris@909: now = Time.parse('2011-02-20 15:45:22') Chris@909: with_settings :time_format => '' do Chris@909: with_settings :date_format => '' do Chris@909: assert_equal '02/20/2011 03:45 pm', format_time(now) Chris@909: assert_equal '03:45 pm', format_time(now, false) Chris@909: end Chris@909: with_settings :date_format => '%Y-%m-%d' do Chris@909: assert_equal '2011-02-20 03:45 pm', format_time(now) Chris@909: assert_equal '03:45 pm', format_time(now, false) Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def test_time_format Chris@909: set_language_if_valid 'en' Chris@909: now = Time.now Chris@909: Setting.date_format = '%d %m %Y' Chris@909: Setting.time_format = '%H %M' Chris@909: assert_equal now.strftime('%d %m %Y %H %M'), format_time(now) Chris@909: assert_equal now.strftime('%H %M'), format_time(now, false) Chris@909: end Chris@909: Chris@909: def test_utc_time_format Chris@909: set_language_if_valid 'en' Chris@909: now = Time.now Chris@909: Setting.date_format = '%d %m %Y' Chris@909: Setting.time_format = '%H %M' Chris@909: assert_equal now.strftime('%d %m %Y %H %M'), format_time(now.utc) Chris@909: assert_equal now.strftime('%H %M'), format_time(now.utc, false) Chris@909: end Chris@909: Chris@909: def test_number_to_human_size_for_each_language Chris@909: valid_languages.each do |lang| Chris@909: set_language_if_valid lang Chris@909: assert_nothing_raised "#{lang} failure" do Chris@909: number_to_human_size(1024*1024*4) Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: def test_valid_languages Chris@909: assert valid_languages.is_a?(Array) Chris@909: assert valid_languages.first.is_a?(Symbol) Chris@909: end Chris@909: Chris@909: def test_valid_language Chris@909: to_test = {'fr' => :fr, Chris@909: 'Fr' => :fr, Chris@909: 'zh' => :zh, Chris@909: 'zh-tw' => :"zh-TW", Chris@909: 'zh-TW' => :"zh-TW", Chris@909: 'zh-ZZ' => nil } Chris@909: to_test.each {|lang, expected| assert_equal expected, find_language(lang)} Chris@909: end Chris@909: Chris@909: def test_fallback Chris@909: ::I18n.backend.store_translations(:en, {:untranslated => "Untranslated string"}) Chris@909: ::I18n.locale = 'en' Chris@909: assert_equal "Untranslated string", l(:untranslated) Chris@909: ::I18n.locale = 'fr' Chris@909: assert_equal "Untranslated string", l(:untranslated) Chris@909: Chris@909: ::I18n.backend.store_translations(:fr, {:untranslated => "Pas de traduction"}) Chris@909: ::I18n.locale = 'en' Chris@909: assert_equal "Untranslated string", l(:untranslated) Chris@909: ::I18n.locale = 'fr' Chris@909: assert_equal "Pas de traduction", l(:untranslated) Chris@909: end Chris@909: Chris@909: def test_utf8 Chris@909: set_language_if_valid 'ja' Chris@909: str_ja_yes = "\xe3\x81\xaf\xe3\x81\x84" Chris@909: i18n_ja_yes = l(:general_text_Yes) Chris@909: if str_ja_yes.respond_to?(:force_encoding) Chris@909: str_ja_yes.force_encoding('UTF-8') Chris@909: assert_equal "UTF-8", i18n_ja_yes.encoding.to_s Chris@909: end Chris@909: assert_equal str_ja_yes, i18n_ja_yes Chris@909: end Chris@909: end