def test_refreshing(self): app = flask.Flask(__name__) babel_ext.Babel(app) d = datetime(2010, 4, 12, 13, 46) babel_ext.refresh() # nothing should be refreshed (see case below) with app.test_request_context(): assert babel_ext.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna' babel_ext.refresh() assert babel_ext.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM'
def get(cls): # greetings = 'Hi there!' greetings = _('Hi there!') # message = 'I plan to do something from this project.' message = _('I plan to do something from this project.') # date = 'Date now is ' + str(datetime.now()) date = _('Date now is ') + format_datetime(datetime.now(), "dd LLL Y") # time = 'Time is ' + datetime.now() time = _('Time is ') + format_datetime(datetime.now(), 'HH:mm:ss Z') amount = format_currency(2256.22, 'RUR') return make_response( render_template('index.html', greetings=greetings, message=message, date=date, time=time, amount=amount))
def test_init_app(self): b = babel_ext.Babel() app = flask.Flask(__name__) b.init_app(app) d = datetime(2010, 4, 12, 13, 46) with app.test_request_context(): assert babel_ext.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' assert babel_ext.format_date(d) == 'Apr 12, 2010' assert babel_ext.format_time(d) == '1:46:00 PM' with app.test_request_context(): app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna' assert babel_ext.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM' assert babel_ext.format_date(d) == 'Apr 12, 2010' assert babel_ext.format_time(d) == '3:46:00 PM' with app.test_request_context(): app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE' assert babel_ext.format_datetime(d, 'long') == \ '12. April 2010 um 15:46:00 MESZ'
def post(cls): test_json = request.get_json() fbp.set_lng(request.headers.get('Accept-Language')) if test_json['what'] == 'date': payload = str( _('Date now is ') + format_datetime(datetime.now(), "dd LLL Y")) message = str(_('You asked about date')) elif test_json['what'] == 'time': payload = str( _('Time is ') + format_datetime(datetime.now(), 'HH:mm:ss Z')) message = str(_('You asked about time')) else: message = str( _('Not clear what do you want. ' 'I know date and time only. Please specify.')) payload = None output = {"message": message, "payload": payload} return output
def test_basics(self): app = flask.Flask(__name__) babel_ext.Babel(app) d = datetime(2010, 4, 12, 13, 46) delta = timedelta(days=6) with app.test_request_context(): assert babel_ext.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' assert babel_ext.format_date(d) == 'Apr 12, 2010' assert babel_ext.format_time(d) == '1:46:00 PM' assert babel_ext.format_timedelta(delta) == '1 week' assert babel_ext.format_timedelta(delta, threshold=1) == '6 days' with app.test_request_context(): app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna' assert babel_ext.format_datetime(d) == 'Apr 12, 2010, 3:46:00 PM' assert babel_ext.format_date(d) == 'Apr 12, 2010' assert babel_ext.format_time(d) == '3:46:00 PM' with app.test_request_context(): app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE' assert babel_ext.format_datetime(d, 'long') == \ '12. April 2010 um 15:46:00 MESZ'
def test_custom_locale_selector(self): app = flask.Flask(__name__) b = babel_ext.Babel(app) d = datetime(2010, 4, 12, 13, 46) the_timezone = 'UTC' the_locale = 'en_US' @b.localeselector def select_locale(): return the_locale @b.timezoneselector def select_timezone(): return the_timezone with app.test_request_context(): assert babel_ext.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' the_locale = 'de_DE' the_timezone = 'Europe/Vienna' with app.test_request_context(): assert babel_ext.format_datetime(d) == '12.04.2010, 15:46:00'
def test_custom_formats(self): app = flask.Flask(__name__) app.config.update(BABEL_DEFAULT_LOCALE='en_US', BABEL_DEFAULT_TIMEZONE='Pacific/Johnston') b = babel_ext.Babel(app) b.date_formats['datetime'] = 'long' b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a' b.date_formats['date'] = 'long' b.date_formats['date.short'] = 'MM d' d = datetime(2010, 4, 12, 13, 46) with app.test_request_context(): assert babel_ext.format_datetime(d) == 'April 12, 2010 3:46:00 AM' assert _get_format('datetime') == 'MMMM d, yyyy h:mm:ss a' # none; returns the format assert _get_format('datetime', 'medium') == 'medium' assert _get_format('date', 'short') == 'MM d'