Skip to content

Commit

Permalink
i18n: add function to get localized territory name from territory code
Browse files Browse the repository at this point in the history
and a lazy version similar to lazy_gettext
  • Loading branch information
Bertrand Mathieu committed Jan 15, 2015
1 parent 6593d07 commit 3a85a91
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion abilian/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import os
import importlib
from flask import _request_ctx_stack
from flask import _request_ctx_stack, current_app

from babel.localedata import locale_identifiers
from babel.support import Translations
Expand Down Expand Up @@ -61,6 +61,24 @@
VALID_LANGUAGES_CODE = frozenset(lang for lang in locale_identifiers()
if len(lang) == 2)

def __gettext_territory(code):
locale = flask.ext.babel.get_locale()
if locale is None:
locale = current_app.extensions['babel'].default_locale.territories.get(code)
return (
locale.territories.get(code)
or current_app.extensions['babel'].default_locale.territories.get(code))

#: get localized territory name
def country_name(code):
return __gettext_territory(code)

#: lazy version of :func:`country_name`
def lazy_country_name(code):
from speaklater import make_lazy_string
return make_lazy_string(__gettext_territory, code)


class Babel(BabelBase):
"""
Allow to load translations from other modules
Expand Down

0 comments on commit 3a85a91

Please sign in to comment.