def get_locale(self): if self.data.language == 'fr': locale = i18n.Locale('fr', 'FR') else: locale = i18n.Locale('en', 'US') # At this point, the locale object only knows about builtin Nagare translation directories # We need to register Kansha's translation directories too (get them from the current locale) locale.translation_directories.update(i18n.get_locale().translation_directories) return locale
def test_context_manager(): locale1 = i18n.Locale('fr', 'FR', domain='domain1') locale2 = i18n.Locale('fr', 'FR', domain='domain2') i18n.set_locale(locale1) assert i18n.get_locale().domain == 'domain1' with locale2: assert i18n.get_locale().domain == 'domain2' with locale2: assert i18n.get_locale().domain == 'domain2' assert i18n.get_locale().domain == 'domain1'
def test_context_manager(self): locale1 = i18n.Locale('fr', 'FR', domain='domain1') locale2 = i18n.Locale('fr', 'FR', domain='domain2') i18n.set_locale(locale1) self.assertEqual(i18n.get_locale().domain, 'domain1') with locale2: self.assertEqual(i18n.get_locale().domain, 'domain2') with locale2: self.assertEqual(i18n.get_locale().domain, 'domain2') self.assertEqual(i18n.get_locale().domain, 'domain1')
def setup_module(module): local.request = local.Process() locale = i18n.Locale('fr', 'FR', dirname=os.path.join(os.path.dirname(__file__), 'locale')) i18n.set_locale(locale)
def test_get_era_names(): locale = i18n.Locale('en', 'US') assert locale.get_era_names() == {0: u'Before Christ', 1: u'Anno Domini'} assert locale.get_era_names(width='wide') == {0: u'Before Christ', 1: u'Anno Domini'} assert locale.get_era_names(width='abbreviated') == {0: u'BC', 1: u'AD'} assert locale.get_era_names(width='narrow') == {0: u'B', 1: u'A'}
def configure(): """Configure the batch environment""" global apps # we suppose that the batch addresses only one application app_name, app = apps.items()[0] local.request = local.Process() local.worker = local.Process() log.set_logger('nagare.application.' + app.name) security.set_manager(app.security) # initialize the application context app.set_base_url('http://localhost/') # dummy server host app.set_locale(i18n.Locale('en', 'US')) # Set the default Locale # install the configuration registry.configure(app.configuration) # log the start of the script logger = log.get_logger('.' + __name__) logger.debug('----') logger.debug('Running %s\n' % sys.argv[0])
def setUp(self): # call the base implementation super(ConfigurationEnabledTestCase, self).setUp() # configure the local service local.request = local.Process() local.worker = local.Process() # read the configuration conf_path = test_resource_path('conf', 'eureka.cfg.template') defaults = dict(here='string(default="%s")' % os.path.abspath(os.path.dirname(conf_path))) conf = read_application_options(conf_path, self.fail, defaults) # initialize the application context # no root component since we only want to perform the configuration app = BaseApplication(None) app.PLATFORM = "Eureka base" app.set_config(conf_path, conf, self.fail) app.set_base_url('http://localhost/') # dummy server host app.set_locale(i18n.Locale('en', 'US')) # Set the default Locale # install the configuration registry.configure(app.configuration) # configure the security manager security.set_manager(app.security)
def test_format_time_time_en(): i18n.set_locale(i18n.Locale('en', 'US', timezone='Pacific/Pitcairn')) t = datetime.time(15, 30) assert i18n.format_time(t, format='full') == '3:30:00 PM Pitcairn Time' assert i18n.format_time(t, format='long') == '3:30:00 PM -0800' assert i18n.format_time(t, format='medium') == '3:30:00 PM' assert i18n.format_time(t) == '3:30:00 PM' assert i18n.format_time(t, format='short') == '3:30 PM'
def test_format_date_date(): i18n.set_locale(i18n.Locale('fr', 'FR')) d = datetime.date(2007, 4, 1) assert i18n.format_date(d, format='full') == 'dimanche 1 avril 2007' assert i18n.format_date(d, format='long') == '1 avril 2007' assert i18n.format_date(d, format='medium') == '1 avr. 2007' assert i18n.format_date(d) == '1 avr. 2007' assert i18n.format_date(d, format='short') == '01/04/2007'
def test_parse_date_en(self): i18n.set_locale(i18n.Locale('en', 'US')) d = i18n.parse_date('4/1/04') self.assertIsInstance(d, datetime.date) self.assertEqual((d.year, d.month, d.day), (2004, 4, 1)) d = i18n.parse_date('4/1/2004') self.assertIsInstance(d, datetime.date) self.assertEqual((d.year, d.month, d.day), (2004, 4, 1))
def test_parse_date_en(): i18n.set_locale(i18n.Locale('en', 'US')) d = i18n.parse_date('4/1/04') assert isinstance(d, datetime.date) assert (d.year, d.month, d.day) == (2004, 4, 1) d = i18n.parse_date('4/1/2004') assert isinstance(d, datetime.date) assert (d.year, d.month, d.day) == (2004, 4, 1)
def test_format_time_time_with_format(self): i18n.set_locale(i18n.Locale('en', 'US', timezone='Pacific/Pitcairn')) t = datetime.time(15, 30) self.assertEqual(i18n.format_time(t, format="hh 'o''clock' a, zzzz"), "03 o'clock PM, Pitcairn Time") t = datetime.time(15, 30, tzinfo=pytz.timezone('Africa/Niamey')) self.assertEqual(i18n.format_time(t, format="hh 'o''clock' a, zzzz"), "03 o'clock PM, Pitcairn Time")
def test_parse_date_fr(): i18n.set_locale(i18n.Locale('fr', 'FR')) d = i18n.parse_date('4/1/04') assert isinstance(d, datetime.date) assert (d.year, d.month, d.day) == (2004, 1, 4) d = i18n.parse_date('4/1/2004') assert isinstance(d, datetime.date) assert (d.year, d.month, d.day) == (2004, 1, 4)
def test_format_time_time_fr3(self): i18n.set_locale(i18n.Locale('fr', 'FR')) t = datetime.time(15, 30, tzinfo=pytz.timezone('Pacific/Pitcairn')) self.assertEqual(i18n.format_time(t, format='full'), '15:30:00 UTC+00:00') self.assertEqual(i18n.format_time(t, format='long'), '15:30:00 +0000') self.assertEqual(i18n.format_time(t, format='medium'), '15:30:00') self.assertEqual(i18n.format_time(t), '15:30:00') self.assertEqual(i18n.format_time(t, format='short'), '15:30')
def test_format_time_datetime_with_format(self): i18n.set_locale(i18n.Locale('en', 'US', timezone='Pacific/Pitcairn')) d = datetime.datetime(2007, 4, 1, 15, 30) self.assertEqual(i18n.format_time(d, format="hh 'o''clock' a, zzzz"), u"03 o'clock PM, Pitcairn Time") tz = pytz.timezone('Africa/Niamey') d = tz.localize(datetime.datetime(2007, 4, 1, 15, 30)) self.assertEqual(i18n.format_time(d, format="hh 'o''clock' a, zzzz"), u"06 o'clock AM, Pitcairn Time")
def test_format_datetime_with_format(): i18n.set_locale(i18n.Locale('en', 'US', timezone='Pacific/Pitcairn')) d = datetime.datetime(2007, 4, 1, 15, 30) assert i18n.format_datetime(d, format="yyyy.MM.dd G 'at' HH:mm:ss zzz" ) == '2007.04.01 AD at 15:30:00 -0800' tz = pytz.timezone('Africa/Niamey') d = tz.localize(datetime.datetime(2007, 4, 1, 15, 30)) assert i18n.format_datetime(d, format="yyyy.MM.dd G 'at' HH:mm:ss zzz" ) == '2007.04.01 AD at 06:30:00 -0800'
def test_format_time_datetime_fr2(self): i18n.set_locale(i18n.Locale('fr', 'FR', timezone='Africa/Niamey')) d = datetime.datetime(2007, 4, 1, 15, 30) self.assertEqual(i18n.format_time(d, format='full'), u'15:30:00 heure normale d’Afrique de l’Ouest') self.assertEqual(i18n.format_time(d, format='long'), '15:30:00 +0100') self.assertEqual(i18n.format_time(d, format='medium'), '15:30:00') self.assertEqual(i18n.format_time(d), '15:30:00') self.assertEqual(i18n.format_time(d, format='short'), '15:30')
def test_format_time_time_fr3(): i18n.set_locale(i18n.Locale('fr', 'FR')) t = datetime.time(15, 30, tzinfo=pytz.timezone('Pacific/Pitcairn')) assert i18n.format_time( t, format='full') == u'15:30:00 Temps universel coordonné' assert i18n.format_time(t, format='long') == '15:30:00 TU' assert i18n.format_time(t, format='medium') == '15:30:00' assert i18n.format_time(t) == '15:30:00' assert i18n.format_time(t, format='short') == '15:30'
def test_format_date_datetime(): i18n.set_locale(i18n.Locale('fr', 'FR')) tz = pytz.timezone('Pacific/Pitcairn') d = tz.localize(datetime.datetime(2007, 4, 1, 15, 30)) assert i18n.format_date(d, format='full') == 'dimanche 1 avril 2007' assert i18n.format_date(d, format='long') == '1 avril 2007' assert i18n.format_date(d, format='medium') == '1 avr. 2007' assert i18n.format_date(d) == '1 avr. 2007' assert i18n.format_date(d, format='short') == '01/04/2007'
def test_format_time_time_fr4(self): i18n.set_locale(i18n.Locale('fr', 'FR', timezone='Africa/Niamey')) t = datetime.time(15, 30, tzinfo=pytz.timezone('Pacific/Pitcairn')) self.assertEqual(i18n.format_time(t, format='full'), u'15:30:00 heure normale d’Afrique de l’Ouest') self.assertEqual(i18n.format_time(t, format='long'), '15:30:00 +0100') self.assertEqual(i18n.format_time(t, format='medium'), '15:30:00') self.assertEqual(i18n.format_time(t), '15:30:00') self.assertEqual(i18n.format_time(t, format='short'), '15:30')
def test_format_time_time_fr1(self): i18n.set_locale(i18n.Locale('fr', 'FR')) t = datetime.time(15, 30) self.assertEqual(i18n.format_time(t, format='full'), '15:30:00 UTC+00:00') self.assertEqual(i18n.format_time(t, format='long'), '15:30:00 +0000') self.assertEqual(i18n.format_time(t, format='medium'), '15:30:00') self.assertEqual(i18n.format_time(t), '15:30:00') self.assertEqual(i18n.format_time(t, format='short'), '15:30')
def test_format_time_datetime_fr1(): i18n.set_locale(i18n.Locale('fr', 'FR')) d = datetime.datetime(2007, 4, 1, 15, 30) assert i18n.format_time( d, format='full') == u'15:30:00 Temps universel coordonné' assert i18n.format_time(d, format='long') == '15:30:00 TU' assert i18n.format_time(d, format='medium') == '15:30:00' assert i18n.format_time(d) == '15:30:00' assert i18n.format_time(d, format='short') == '15:30'
def test_format_time_datetime_fr1(self): i18n.set_locale(i18n.Locale('fr', 'FR')) d = datetime.datetime(2007, 4, 1, 15, 30) self.assertEqual(i18n.format_time(d, format='full'), '15:30:00 UTC+00:00') self.assertEqual(i18n.format_time(d, format='long'), '15:30:00 +0000') self.assertEqual(i18n.format_time(d, format='medium'), '15:30:00') self.assertEqual(i18n.format_time(d), '15:30:00') self.assertEqual(i18n.format_time(d, format='short'), '15:30')
def test_format_time_time_fr2(): i18n.set_locale(i18n.Locale('fr', 'FR', timezone='Africa/Niamey')) t = datetime.time(15, 30) assert i18n.format_time( t, format='full') == u'15:30:00 heure normale d’Afrique de l’Ouest' assert i18n.format_time(t, format='long') == '15:30:00 +0100' assert i18n.format_time(t, format='medium') == '15:30:00' assert i18n.format_time(t) == '15:30:00' assert i18n.format_time(t, format='short') == '15:30'
def test_to_timezone_utc_datetime(self): d1 = datetime.datetime(2007, 4, 1, 15, 30, tzinfo=pytz.UTC) i18n.set_locale(i18n.Locale('fr', 'FR')) d2 = i18n.to_timezone(d1) self.assertEqual(str(d2.tzinfo), 'UTC') self.assertEqual(d2.strftime('%H:%M'), '15:30') i18n.set_locale(i18n.Locale('fr', 'FR', timezone='Pacific/Pitcairn')) d2 = i18n.to_timezone(d1) self.assertEqual(str(d2.tzinfo), 'Pacific/Pitcairn') self.assertEqual(d2.strftime('%H:%M'), '07:30') i18n.set_locale( i18n.Locale('fr', 'FR', timezone='Pacific/Pitcairn', default_timezone=pytz.UTC)) d2 = i18n.to_timezone(d1) self.assertEqual(str(d2.tzinfo), 'Pacific/Pitcairn') self.assertEqual(d2.strftime('%H:%M'), '07:30')
def test_to_utc_utc_datetime(self): d1 = datetime.datetime(2007, 4, 1, 15, 30, tzinfo=pytz.UTC) i18n.set_locale(i18n.Locale('fr', 'FR')) d2 = i18n.to_utc(d1) self.assertEqual(str(d2.tzinfo), 'UTC') self.assertEqual(d2.strftime('%H:%M'), '15:30') i18n.set_locale(i18n.Locale('fr', 'FR', timezone='Africa/Niamey')) d2 = i18n.to_utc(d1) self.assertEqual(str(d2.tzinfo), 'UTC') self.assertEqual(d2.strftime('%H:%M'), '15:30') i18n.set_locale( i18n.Locale('fr', 'FR', timezone='Africa/Niamey', default_timezone=pytz.UTC)) d2 = i18n.to_utc(d1) self.assertEqual(str(d2.tzinfo), 'UTC') self.assertEqual(d2.strftime('%H:%M'), '15:30')
def test_format_time_time_en(self): i18n.set_locale(i18n.Locale('en', 'US', timezone='Pacific/Pitcairn')) t = datetime.time(15, 30) self.assertEqual(i18n.format_time(t, format='full'), '3:30:00 PM Pitcairn Time') self.assertEqual(i18n.format_time(t, format='long'), '3:30:00 PM -0800') self.assertEqual(i18n.format_time(t, format='medium'), '3:30:00 PM') self.assertEqual(i18n.format_time(t), '3:30:00 PM') self.assertEqual(i18n.format_time(t, format='short'), '3:30 PM')
def test_format_time_datetime_fr4(self): i18n.set_locale(i18n.Locale('fr', 'FR')) tz = pytz.timezone('Pacific/Pitcairn') d = tz.localize(datetime.datetime(2007, 4, 1, 15, 30)) self.assertEqual(i18n.format_time(d, format='full'), '23:30:00 UTC+00:00') self.assertEqual(i18n.format_time(d, format='long'), '23:30:00 +0000') self.assertEqual(i18n.format_time(d, format='medium'), '23:30:00') self.assertEqual(i18n.format_time(d), '23:30:00') self.assertEqual(i18n.format_time(d, format='short'), '23:30')
def test_format_time_datetime_fr5(self): i18n.set_locale(i18n.Locale('fr', 'FR', timezone='Africa/Niamey')) tz = pytz.timezone('Pacific/Pitcairn') d = tz.localize(datetime.datetime(2007, 4, 1, 15, 30)) self.assertEqual(i18n.format_time(d, format='full'), u'00:30:00 heure normale d’Afrique de l’Ouest') self.assertEqual(i18n.format_time(d, format='long'), '00:30:00 +0100') self.assertEqual(i18n.format_time(d, format='medium'), '00:30:00') self.assertEqual(i18n.format_time(d), '00:30:00') self.assertEqual(i18n.format_time(d, format='short'), '00:30')
def test_rendering_security_view_board_3(self): """Test rendering security view board 3 Test rendering (Board public / user not logged) """ helpers.set_dummy_context() # for board creation board = helpers.create_board() helpers.set_context() # for realistic security check board.set_visibility(board_module.BOARD_PUBLIC) with i18n.Locale('en', 'US'): component.Component(board).render(xhtml5.Renderer())