lodgeit/lodgeit/i18n/__init__.py
2008-07-08 11:20:20 +02:00

97 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
"""
lodgeit.i18n
~~~~~~~~~~~~
i18n tools for LodgeIt!
:copyright: Copyright 2008 by Armin Ronacher, Christopher Grebs.
:license: GNU GPL.
"""
import os
from datetime import datetime
from babel import Locale, dates, UnknownLocaleError
from babel.support import Translations
from lodgeit.utils import ctx, jinja_environment, get_application
__all__ = ['_', 'gettext', 'ngettext']
DATE_FORMATS = ['%m/%d/%Y', '%d/%m/%Y', '%Y%m%d', '%d. %m. %Y',
'%m/%d/%y', '%d/%m/%y', '%d%m%y', '%m%d%y', '%y%m%d']
TIME_FORMATS = ['%H:%M', '%H:%M:%S', '%I:%M %p', '%I:%M:%S %p']
def load_translations(locale):
"""Load the translation for a locale."""
return Translations.load(os.path.dirname(__file__), [locale])
def gettext(string):
"""Translate the given string to the language of the application."""
app = get_application()
if app is None:
return string
return app.translations.ugettext(string)
def ngettext(singular, plural, n):
"""Translate the possible pluralized string to the language of the
application.
"""
app = get_application()
if app is None:
if n == 1:
return singular
return plrual
return app.translations.ungettext(singular, plural, n)
def format_datetime(datetime=None, format='medium'):
"""Return a date formatted according to the given pattern."""
return _date_format(dates.format_datetime, datetime, format)
def list_languages():
"""Return a list of all languages."""
app = get_application()
if app:
locale = app.locale
else:
locale = Locale('en')
languages = [('en', Locale('en').get_display_name(locale))]
folder = os.path.dirname(__file__)
for filename in os.listdir(folder):
if filename == 'en' or not \
os.path.isdir(os.path.join(folder, filename)):
continue
try:
l = Locale.parse(filename)
except UnknownLocaleError:
continue
languages.append((str(l), l.get_display_name(locale)))
languages.sort(key=lambda x: x[1].lower())
return languages
def has_language(language):
"""Check if a language exists."""
return language in dict(list_languages())
def _date_format(formatter, obj, format):
app = get_application()
if app is None:
locale = Locale('en')
else:
locale = app.locale
return formatter(obj, format, locale=locale)
_ = gettext