Пример #1
0
 def test_format_float(self):
     assert localize(14.234) == '14.234'
     assert localize(125.34, '000000') == '000125'
     assert localize(125.44, '000000;(0)') == '000125'
     assert localize(-125, '000000;(-0)') == '(-000125)'
     assert localize(0, '000000;(0)') == '000000'
     assert localize(0, '000000;(0);--') == '--'
     assert localize(125, 'CR 0;DB 0; NoChange') == 'CR 125'
     assert localize(-125, 'CR 0;DB 0; NoChange') == 'DB 125'
     assert localize(0, 'CR 0;DB 0;No Change') == 'No Change'
Пример #2
0
    def test_timeLocale(self):
        assert localize(self.time, locale='en', format='short') == '12:23 PM'
        assert localize(self.time, locale='it', format='short') == '12:23'
        assert localize(self.time, locale='de', format='short') == '12:23'

        assert localize(self.time, locale='en', format='medium') == '12:23:44 PM'
        assert localize(self.time, locale='it', format='medium') == '12:23:44'
        assert localize(self.time, locale='de', format='medium') == '12:23:44'

        assert localize(self.time, locale='en', format='long') == '12:23:44 PM +0000'
        assert localize(self.time, locale='it', format='long') == '12:23:44 +0000'
        assert localize(self.time, locale='de', format='long') == '12:23:44 +0000'
Пример #3
0
def toText(obj, locale=None, format=None, mask=None, encoding=None, currency=None):
    """Return a unicode string representing an object of any class
    
    If there are ``locale`` or ``format`` parameters Babel is used to format the value
    according to the given localization or format
    
    :param obj: the object to be transformed in a string
    :param locale: the current locale (e.g: en, en_us, it)
    :param format: TODO
    :param mask: TODO
    :param encoding: The multibyte character encoding you choose
    :param currency: TODO"""
    if hasattr(obj, '_htraverse') and format:
        return templateReplace(format,obj)
    if isinstance(obj, list) or isinstance(obj, tuple):
        return ','.join([toText(v) for v in obj])
        #what?
    if obj in (None,''): return u''
    if not (locale or format):
        result = unicode(obj)
    else:
        result = localize(obj, locale=locale, format=format, currency=currency)
        
    if mask:
        result = mask % result
    return result
Пример #4
0
def toText(obj, locale=None, format=None, mask=None, encoding=None, currency=None):
    """Return a unicode string representing an object of any class.
       
       If there are ``locale`` or ``format`` parameters Babel is used to format the value 
       according to the given localization or format.
       
       :param obj: the object to be transformed in a string
       :param locale: add???. Default value is ``None``
       :param format: add???. Default value is ``None``
       :param mask: add???. Default value is ``None``
       :param encoding: The multibyte character encoding you choose. Default value is ``None``
       :param currency: add???. Default value is ``None``
       :returns: a unicode string representing an object of any class
       """
    if isinstance(obj, list) or isinstance(obj, tuple):
        return ','.join([toText(v) for v in obj])
        #what?
    if obj is None: return u''
    if not (locale or format):
        result = unicode(obj)
    else:
        result = localize(obj, locale=locale, format=format, currency=currency)
        
    if mask:
        result = mask % result
    return result
Пример #5
0
 def test_format_dec_positive(self):
     x = Decimal("1239988655.45611665677")
     assert localize(x, '#.0') == '1239988655.5'
     assert localize(x, '#.0', locale='it_IT') == '1239988655,5'
     assert localize(x, '#.#') == '1239988655.5'
     assert localize(x, '#.#', locale='it_IT') == '1239988655,5'
     assert localize(x, '#.#####') == '1239988655.45612'
     assert localize(x, '#.#####', locale='it_IT') == '1239988655,45612'
     assert localize(x, '#,###.#####') == '1,239,988,655.45612'
     assert localize(x, '#,###.#####', locale='it_IT') == '1.239.988.655,45612'
Пример #6
0
 def test_dateLocaleFormat(self):
     assert localize(self.date, locale='en', format="EEE MMM d 'year:'yy") == 'Mon Dec 10 year:07'
     assert localize(self.date, locale='it', format="EEE MMM d 'anno:'yy") == "lun dic 10 anno:07"
     assert localize(self.date, locale='it', format='EEEE d MMMM yyyyG') == u'lunedì 10 dicembre 2007dC'
     assert localize(self.date, locale='de', format='EEEE d MMMM yyyyG') == u'Montag 10 Dezember 2007n. Chr.'
     assert localize(self.date, locale='en', format='D yyyyG') == '344 2007AD'
     assert localize(self.date, locale='en', format='ee LLLLL yyyyG') == '01 D 2007AD'
Пример #7
0
 def test_timeLocaleFormat(self):
     assert localize(self.time, locale='en', format='a h:m:s--A') == 'PM 12:23:44--44624059'
Пример #8
0
 def test_date(self):
     assert localize(self.date) == '12/10/07'
Пример #9
0
 def test_format_int_special(self):
     assert localize(125, 'CR 0;DB 0; NoChange') == 'CR 125'
     assert localize(-125, 'CR 0;DB 0; NoChange') == 'DB 125'
     assert localize(0, 'CR 0;DB 0;No Change') == 'No Change'
Пример #10
0
 def test_format_int_negative(self):
     assert localize(-12567, '00000000;-0') == '-00012567'
     assert localize(-12567, '00000000;0 -') == '00012567 -'
     assert localize(-12567, '00000000;(#)') == '(00012567)'
Пример #11
0
 def test_format_int_zero(self):
     assert localize(0, '######;(#)') == '0'
     assert localize(0, '#####0;(#);-*-') == '-*-'
Пример #12
0
 def test_format_int_positive(self):
     assert localize(12567, '00000000') == '00012567'
     assert localize(12567, '#;(#)') == '12567'
     assert localize(1256789, "#,##0;(#)") == '1,256,789'
Пример #13
0
 def test_noformat_decimal(self):
     x = Decimal("1239988655.45111665677")
     assert localize(x) == '1,239,988,655.451'
     assert localize(x, locale='it_IT') == '1.239.988.655,451'
Пример #14
0
 def test_noformat_float(self):
     assert localize(12.445) == '12.445'
     assert localize(125.445, locale='it_IT') == '125,445'
Пример #15
0
 def test_noformat_int(self):
     assert localize(125) == '125'
     assert localize(125, locale='it_IT') == '125'
Пример #16
0
 def test_dateLocaleShort(self):
     assert localize(self.date, locale='en') == '12/10/07'
     assert localize(self.date, locale='it') == '10/12/07'
     assert localize(self.date, locale='de') == '10.12.07'
Пример #17
0
 def test_format_dec_negative(self):
     x = Decimal("-1239988655.45611665677")
     assert localize(x, '#.0') == '-1239988655.5'
     assert localize(x, '#.0;(#)', locale='it_IT') == '(1239988655,5)'
     assert localize(x, '#.#;-  #') == '-  1239988655.5'
     assert localize(x, '#.#;DB #', locale='it_IT') == 'DB 1239988655,5'
Пример #18
0
 def test_dateLocaleMedium(self):
     assert localize(self.date, locale='en', format='medium') == 'Dec 10, 2007'
     assert localize(self.date, locale='it', format='medium') == '10/dic/2007'
     assert localize(self.date, locale='de', format='medium') == '10.12.2007'
Пример #19
0
    def test_datetimeLocale(self):
        assert localize(self.datetime, locale='en', format='short') == '12/10/07, 10:15 PM'
        assert localize(self.datetime, locale='it', format='short') == '10/12/07 22:15'
        assert localize(self.datetime, locale='de', format='short') == '10.12.07 22:15'

        assert localize(self.datetime, locale='en', format='medium') == 'Dec 10, 2007, 10:15:36 PM'
        assert localize(self.datetime, locale='it', format='medium') == '10/dic/2007 22:15:36'
        assert localize(self.datetime, locale='de', format='medium') == '10.12.2007 22:15:36'

        assert localize(self.datetime, locale='en', format='long') == 'December 10, 2007 at 10:15:36 PM +0000'
        assert localize(self.datetime, locale='it', format='long') == '10 dicembre 2007 22:15:36 +0000'
        assert localize(self.datetime, locale='de', format='long') == '10. Dezember 2007 22:15:36 +0000'

        assert localize(self.datetime, locale='en',
                        format='full') == 'Monday, December 10, 2007 at 10:15:36 PM GMT+00:00'
        assert localize(self.datetime, locale='it', format='full') == u'lunedì 10 dicembre 2007 22:15:36 GMT+00:00'
        assert localize(self.datetime, locale='de', format='full') == u'Montag, 10. Dezember 2007 22:15:36 GMT+00:00'
Пример #20
0
 def test_dateLocaleFull(self):
     assert localize(self.date, locale='en', format='full') == 'Monday, December 10, 2007'
     assert localize(self.date, locale='it', format='full') == u'lunedì 10 dicembre 2007'
     assert localize(self.date, locale='de', format='full') == 'Montag, 10. Dezember 2007'
Пример #21
0
 def test_dateLocaleLong(self):
     assert localize(self.date, locale='en', format='long') == 'December 10, 2007'
     assert localize(self.date, locale='it', format='long') == '10 dicembre 2007'
     assert localize(self.date, locale='de', format='long') == '10. Dezember 2007'
Пример #22
0
 def test_format_currency(self):
     x = Decimal("1239988655.45611665677")
     assert localize(x, u'¤ #.0', u'EUR') == u'€ 1239988655.5'
     assert localize(x, u'¤ #.0', u'$') == u'$ 1239988655.5'
     assert localize(x, u'Please pay #.#### ¤ ASAP', u'EUR', locale='it_IT') == u'Please pay 1239988655,4561 € ASAP'
Пример #23
0
 def test_none(self):
     assert localize(None) == ''
     assert localize(None, locale='de') == ''
     assert localize(None, locale='it') == ''