def get_datetime_format(kind="datetime"): """Get local datetime format. @param kind: type (date, time or datetime) @return: string @todo: cache formats to improve performance. """ if 'lang' in cherrypy.session: # server-defined formatting if kind == 'time': return cherrypy.session['lang']['time_format'] elif kind == 'date': return cherrypy.session['lang']['date_format'] else: return "%(date_format)s %(time_format)s"% cherrypy.session['lang'] # TODO: correctly convert from LDML to POSIX datetime formatting # current converter is trivial and lame and probably very easy to break date_format = _to_posix_format(dates.get_date_format( format='short', locale=get_locale())).format if kind == 'time': # Should use dates.get_time_format(locale=get_locale()) return '%H:%M:%S' elif kind == 'date': return date_format else: # Should use dates.get_datetime_format, but that one returns # a 2.6-style formats return "%s %s" % (date_format, '%H:%M:%S')
def get_datetime_format(kind="datetime"): """Get local datetime format. @param kind: type (date, time or datetime) @return: string @todo: cache formats to improve performance. """ if 'lang' in cherrypy.session: # server-defined formatting if kind == 'time': return cherrypy.session['lang']['time_format'] elif kind == 'date': return cherrypy.session['lang']['date_format'] else: return "%(date_format)s %(time_format)s" % cherrypy.session['lang'] # TODO: correctly convert from LDML to POSIX datetime formatting # current converter is trivial and lame and probably very easy to break date_format = _to_posix_format( dates.get_date_format(format='short', locale=get_locale())).format if kind == 'time': # Should use dates.get_time_format(locale=get_locale()) return '%H:%M:%S' elif kind == 'date': return date_format else: # Should use dates.get_datetime_format, but that one returns # a 2.6-style formats return "%s %s" % (date_format, '%H:%M:%S')
def format_decimal(value, digits=2): locale = get_locale() v = ("%%.%df" % digits) % value if not digits: return numbers.format_number(value, locale=locale) num, decimals = v.split(".", 1) if num == "-0": val = "-0" else: val = numbers.format_number(int(num), locale=locale) return val + unicode(numbers.get_decimal_symbol(locale) + decimals)
def parse_decimal(value): locale = cherrypy.session['lang'].get('code') or get_locale() if isinstance(value, basestring): value = ustr(value) grouping, thousands_sep, decimal_point = get_lang_float_format(locale,monetary=False) #deal with ' ' instead of u'\xa0' (SP instead of NBSP as grouping char) value = value.replace(' ', '') value = value.replace(thousands_sep, '').replace(decimal_point, '.') if not isinstance(value, float): return float(value) return value
def parse_decimal(value): if isinstance(value, basestring): value = ustr(value) #deal with ' ' instead of u'\xa0' (SP instead of NBSP as grouping char) value = value.replace(' ', '') try: value = numbers.parse_decimal(value, locale=get_locale()) except ValueError: pass if not isinstance(value, float): return float(value) return value
def parse_decimal(value): locale = cherrypy.session['lang'].get('code') or get_locale() if isinstance(value, basestring): value = ustr(value) grouping, thousands_sep, decimal_point = get_lang_float_format( locale, monetary=False) #deal with ' ' instead of u'\xa0' (SP instead of NBSP as grouping char) value = value.replace(' ', '') value = value.replace(thousands_sep, '').replace(decimal_point, '.') if not isinstance(value, float): return float(value) return value
def format_decimal(value, digits=2, grouping=True, monetary=False): locale = cherrypy.session['lang'].get('code') or get_locale() formatted = ("%%.%df" % digits) % value lang_grouping, thousands_sep, decimal_point = get_lang_float_format(locale,monetary=False) seps = 0 parts = formatted.split('.') if grouping: parts[0], seps = group(parts[0], monetary=monetary, grouping=lang_grouping, thousands_sep=thousands_sep) formatted = decimal_point.join(parts) while seps: sp = formatted.find(' ') if sp == -1: break formatted = formatted[:sp] + formatted[sp+1:] seps -= 1 return formatted
def _gettext(key, locale=None, domain=None): """Get the gettext value for key. Added to builtins as '_'. Returns Unicode string. @param key: text to be translated @param locale: locale code to be used. If locale is None, gets the value provided by get_locale. """ if locale is None: locale = get_locale() elif not isinstance(locale, babel.Locale): locale = babel.Locale.parse(locale) if key == "": return "" # special case try: return get_translations(locale, domain).ugettext(key) except KeyError: return key
def _gettext(key, locale=None, domain=None): """Get the gettext value for key. Added to builtins as '_'. Returns Unicode string. @param key: text to be translated @param locale: locale code to be used. If locale is None, gets the value provided by get_locale. """ if locale is None: locale = get_locale() elif not isinstance(locale, babel.Locale): locale = babel.Locale.parse(locale) if key == '': return '' # special case try: return get_translations(locale, domain).ugettext(key) except KeyError: return key
def format_decimal(value, digits=2, grouping=True, monetary=False): locale = cherrypy.session['lang'].get('code') or get_locale() formatted = ("%%.%df" % digits) % value lang_grouping, thousands_sep, decimal_point = get_lang_float_format( locale, monetary=False) seps = 0 parts = formatted.split('.') if grouping: parts[0], seps = group(parts[0], monetary=monetary, grouping=lang_grouping, thousands_sep=thousands_sep) formatted = decimal_point.join(parts) while seps: sp = formatted.find(' ') if sp == -1: break formatted = formatted[:sp] + formatted[sp + 1:] seps -= 1 return formatted
def format_date_custom(dt, fmt="y-M-d"): return dates.format_date(dt, format=fmt, locale=get_locale())