1# -*- coding: utf-8 -*- 2from __future__ import with_statement 3 4import sys 5import os 6sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) 7 8import unittest 9from decimal import Decimal 10import flask 11from datetime import datetime 12import flask_babelex as babel 13from flask_babelex import gettext, ngettext, lazy_gettext 14from flask_babelex._compat import text_type 15 16 17class DateFormattingTestCase(unittest.TestCase): 18 19 def test_basics(self): 20 app = flask.Flask(__name__) 21 b = babel.Babel(app) 22 d = datetime(2010, 4, 12, 13, 46) 23 24 with app.test_request_context(): 25 assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' 26 assert babel.format_date(d) == 'Apr 12, 2010' 27 assert babel.format_time(d) == '1:46:00 PM' 28 29 with app.test_request_context(): 30 app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna' 31 assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM' 32 assert babel.format_date(d) == 'Apr 12, 2010' 33 assert babel.format_time(d) == '3:46:00 PM' 34 35 with app.test_request_context(): 36 app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE' 37 assert babel.format_datetime(d, 'long') == \ 38 '12. April 2010 um 15:46:00 MESZ' 39 40 def test_init_app(self): 41 b = babel.Babel() 42 app = flask.Flask(__name__) 43 b.init_app(app) 44 d = datetime(2010, 4, 12, 13, 46) 45 46 with app.test_request_context(): 47 assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' 48 assert babel.format_date(d) == 'Apr 12, 2010' 49 assert babel.format_time(d) == '1:46:00 PM' 50 51 with app.test_request_context(): 52 app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna' 53 assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM' 54 assert babel.format_date(d) == 'Apr 12, 2010' 55 assert babel.format_time(d) == '3:46:00 PM' 56 57 with app.test_request_context(): 58 app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE' 59 assert babel.format_datetime(d, 'long') == \ 60 '12. April 2010 um 15:46:00 MESZ' 61 62 def test_custom_formats(self): 63 app = flask.Flask(__name__) 64 app.config.update( 65 BABEL_DEFAULT_LOCALE='en_US', 66 BABEL_DEFAULT_TIMEZONE='Pacific/Johnston' 67 ) 68 b = babel.Babel(app) 69 b.date_formats['datetime'] = 'long' 70 b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a' 71 d = datetime(2010, 4, 12, 13, 46) 72 73 with app.test_request_context(): 74 assert babel.format_datetime(d) == 'April 12, 2010 3:46:00 AM' 75 76 def test_custom_locale_selector(self): 77 app = flask.Flask(__name__) 78 b = babel.Babel(app) 79 d = datetime(2010, 4, 12, 13, 46) 80 81 the_timezone = 'UTC' 82 the_locale = 'en_US' 83 84 @b.localeselector 85 def select_locale(): 86 return the_locale 87 @b.timezoneselector 88 def select_timezone(): 89 return the_timezone 90 91 with app.test_request_context(): 92 assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' 93 94 the_locale = 'de_DE' 95 the_timezone = 'Europe/Vienna' 96 97 with app.test_request_context(): 98 assert babel.format_datetime(d) == '12.04.2010, 15:46:00' 99 100 def test_refreshing(self): 101 app = flask.Flask(__name__) 102 b = babel.Babel(app) 103 d = datetime(2010, 4, 12, 13, 46) 104 with app.test_request_context(): 105 assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' 106 app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna' 107 babel.refresh() 108 assert babel.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM' 109 110 def test_non_initialized(self): 111 app = flask.Flask(__name__) 112 d = datetime(2010, 4, 12, 13, 46) 113 with app.test_request_context(): 114 assert babel.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' 115 116 117class NumberFormattingTestCase(unittest.TestCase): 118 119 def test_basics(self): 120 app = flask.Flask(__name__) 121 b = babel.Babel(app) 122 n = 1099 123 124 with app.test_request_context(): 125 assert babel.format_number(n) == u'1,099' 126 assert babel.format_decimal(Decimal('1010.99')) == u'1,010.99' 127 assert babel.format_currency(n, 'USD') == '$1,099.00' 128 assert babel.format_percent(0.19) == '19%' 129 assert babel.format_scientific(10000) == u'1E4' 130 131 132class GettextTestCase(unittest.TestCase): 133 134 def test_basics(self): 135 app = flask.Flask(__name__) 136 b = babel.Babel(app, default_locale='de_DE') 137 138 with app.test_request_context(): 139 assert gettext(u'Hello %(name)s!', name='Peter') == 'Hallo Peter!' 140 assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 3) == u'3 Äpfel' 141 assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 1) == u'1 Apfel' 142 143 def test_template_basics(self): 144 app = flask.Flask(__name__) 145 b = babel.Babel(app, default_locale='de_DE') 146 147 t = lambda x: flask.render_template_string('{{ %s }}' % x) 148 149 with app.test_request_context(): 150 assert t("gettext('Hello %(name)s!', name='Peter')") == 'Hallo Peter!' 151 assert t("ngettext('%(num)s Apple', '%(num)s Apples', 3)") == u'3 Äpfel' 152 assert t("ngettext('%(num)s Apple', '%(num)s Apples', 1)") == u'1 Apfel' 153 assert flask.render_template_string(''' 154 {% trans %}Hello {{ name }}!{% endtrans %} 155 ''', name='Peter').strip() == 'Hallo Peter!' 156 assert flask.render_template_string(''' 157 {% trans num=3 %}{{ num }} Apple 158 {%- pluralize %}{{ num }} Apples{% endtrans %} 159 ''', name='Peter').strip() == u'3 Äpfel' 160 161 def test_lazy_gettext(self): 162 app = flask.Flask(__name__) 163 b = babel.Babel(app, default_locale='de_DE') 164 yes = lazy_gettext(u'Yes') 165 with app.test_request_context(): 166 assert text_type(yes) == 'Ja' 167 app.config['BABEL_DEFAULT_LOCALE'] = 'en_US' 168 with app.test_request_context(): 169 assert text_type(yes) == 'Yes' 170 171 def test_lazy_gettext_defaultdomain(self): 172 app = flask.Flask(__name__) 173 domain = babel.Domain(domain='test') 174 b = babel.Babel(app, default_locale='de_DE', default_domain=domain) 175 first = lazy_gettext('first') 176 with app.test_request_context(): 177 assert text_type(first) == 'erste' 178 app.config['BABEL_DEFAULT_LOCALE'] = 'en_US' 179 with app.test_request_context(): 180 assert text_type(first) == 'first' 181 182 def test_list_translations(self): 183 app = flask.Flask(__name__) 184 b = babel.Babel(app, default_locale='de_DE') 185 translations = b.list_translations() 186 assert len(translations) == 1 187 assert str(translations[0]) == 'de' 188 189 def test_domain(self): 190 app = flask.Flask(__name__) 191 b = babel.Babel(app, default_locale='de_DE') 192 domain = babel.Domain(domain='test') 193 194 with app.test_request_context(): 195 assert domain.gettext('first') == 'erste' 196 assert babel.gettext('first') == 'first' 197 198 def test_as_default(self): 199 app = flask.Flask(__name__) 200 b = babel.Babel(app, default_locale='de_DE') 201 domain = babel.Domain(domain='test') 202 203 with app.test_request_context(): 204 assert babel.gettext('first') == 'first' 205 domain.as_default() 206 assert babel.gettext('first') == 'erste' 207 208 def test_default_domain(self): 209 app = flask.Flask(__name__) 210 domain = babel.Domain(domain='test') 211 b = babel.Babel(app, default_locale='de_DE', default_domain=domain) 212 213 with app.test_request_context(): 214 assert babel.gettext('first') == 'erste' 215 216 def test_non_initialized(self): 217 app = flask.Flask(__name__) 218 with app.test_request_context(): 219 assert babel.gettext('first') == 'first' 220 221 def test_multiple_apps(self): 222 app1 = flask.Flask(__name__) 223 b1 = babel.Babel(app1, default_locale='de_DE') 224 225 app2 = flask.Flask(__name__) 226 b2 = babel.Babel(app2, default_locale='de_DE') 227 228 with app1.test_request_context(): 229 assert babel.gettext('Yes') == 'Ja' 230 231 assert 'de_DE' in b1._default_domain.cache 232 233 with app2.test_request_context(): 234 assert 'de_DE' not in b2._default_domain.cache 235 236if __name__ == '__main__': 237 unittest.main() 238