def format_currency(amount, currency, precision=None): if not amount: return None if precision: format = u'#,##0.%s \xa4\xa4;-#,##0.%s \xa4\xa4' % ('0' * precision, '0' * precision) return numbers.format_currency(amount, currency, format, locale=get_locale()) else: return numbers.format_currency(amount, currency, locale=get_locale())
def localized_countries(): """ Returns a list of tuples containing a two letter ISO 3166 country code and a localized name of the country. """ key = 'localized_countries_%s' % (get_locale()) res = cache.get(key) if res is not None: return res # some locales like 'no' have little or no data :( so we'll use # our default locale's data as a starting point enermyteritoy = default_locale().territories.copy() enermyteritoy.update(get_locale().territories) # exclude 3 digit codes 'cause they're not ISO 3166 res = [(k,smart_truncate(v, 27)) for k,v in enermyteritoy.items() if len(k) == 2] res.sort(key=lambda t: t[1]) cache.set(key, res) return res
def render(self, context): lang = self.lang.resolve(context) return get_locale().languages[lang]
def countryname(code): if not code: return u'None' return get_locale().territories[code]
def localized_timezones(group=False, truncate=True): """ This function is a doozy. Returns a list of timezones translated to your locale. Usually used for displaying on an HTML form in a select box. """ locale = get_locale() key = 'localized_timezones_%s%s%s' % ( locale, ['', '_grouped'][group], ['', '_truncated'][group]) zones = cache.get(key) if zones is not None: zones.sort(key=lambda t: t[1]) return zones if group: zones = {} else: zones = [] dups = {} # certain locales like en_US will assign the same name to a whole # bunch of different zones. dups detects duplicate names so we # can append extra data to differentiate the duplicated zones for tzname in SUPPORTED_TIMEZONES: name = dates.get_timezone_name(pytz.timezone(tzname), locale=locale) if name in dups: dups[name] += 1 else: dups[name] = 0 for tzname in SUPPORTED_TIMEZONES: # utc offsets change so feed in current date tz = pytz.timezone(tzname) dt = datetime.now(tz=tz) gmt = dates.get_timezone_gmt(dt, locale=locale) try: name = dates.get_timezone_name(tz, locale=locale) except KeyError: # ceratin locales like 'no' will raise "KeyError: 'ZZ'" # because they don't have any zone information at the moment name = dates.get_timezone_name(dt, locale=DEFAULT_LOCALE) if dups[name]: name = "%s (%s)" % (name, tzname[tzname.index('/')+1:].replace('_', ' ')) if group: if gmt not in zones: zones[gmt] = [] if truncate: zones[gmt].append((tzname, smart_truncate(name, 35))) else: zones[gmt].append((tzname, name)) else: if truncate: zones.append((tzname, smart_truncate("%s - %s" % (gmt, name), 45))) else: zones.append((tzname, "%s - %s" % (gmt, name))) if group: zones = zones.items() zones.sort(key=lambda t: t[0]) for k, v in zones: v.sort(key=lambda t: t[1]) else: zones.sort(key=lambda t: t[1]) cache.set(key, zones) return zones
def format_number(number): return numbers.format_number(int(str(number)), locale=get_locale())
def format_date(dt, format='short', tz=None): """Dates are only in UTC currently""" if not tz: tz = settings.DEFAULT_TIMEZONE return dates.format_date(dt, format, locale=get_locale())
def format_datetime(dt, format='short', tz=None): if not tz: tz = settings.DEFAULT_TIMEZONE return dates.format_datetime(localize_timezone(dt, tz), format, locale=get_locale())
def format_decimal(decimal, precision=None): if precision: fmt = '#,##0.%s;-#,##0.%s' % ('0' * precision, '0' * precision) return numbers.format_decimal(decimal, fmt, locale=get_locale()) else: return numbers.format_decimal(decimal, locale=get_locale())
def format_percent(number): return numbers.format_percent(Decimal(str(number)), locale=get_locale())