diff options
| author | Jannis Leidel <jannis@leidel.info> | 2010-11-04 10:48:27 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2010-11-04 10:48:27 +0000 |
| commit | 83aeb3c768173f48dc295c3194fecd705d1c05ac (patch) | |
| tree | f8ec0f35e86dce4291cea33a851a51e22b26bdd6 /django/utils | |
| parent | 0659391baf60902168f738f520f75ed20d835aa1 (diff) | |
Fixed #9988 -- Added support for translation contexts. Thanks, Claude Paroz.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14450 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/translation/__init__.py | 11 | ||||
| -rw-r--r-- | django/utils/translation/trans_null.py | 6 | ||||
| -rw-r--r-- | django/utils/translation/trans_real.py | 20 |
3 files changed, 36 insertions, 1 deletions
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 4477c291f4..0e1b4f8d67 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -10,7 +10,8 @@ __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', 'get_language', 'get_language_bidi', 'get_date_formats', 'get_partial_date_formats', 'check_for_language', 'to_locale', 'get_language_from_request', 'templatize', 'ugettext', 'ugettext_lazy', - 'ungettext', 'deactivate_all'] + 'ungettext', 'ungettext_lazy', 'pgettext', 'pgettext_lazy', + 'npgettext', 'npgettext_lazy', 'deactivate_all'] # Here be dragons, so a short explanation of the logic won't hurt: # We are trying to solve two problems: (1) access settings, in particular @@ -63,10 +64,18 @@ def ugettext(message): def ungettext(singular, plural, number): return _trans.ungettext(singular, plural, number) +def pgettext(context, message): + return _trans.pgettext(context, message) + +def npgettext(context, singular, plural, number): + return _trans.npgettext(context, singular, plural, number) + ngettext_lazy = lazy(ngettext, str) gettext_lazy = lazy(gettext, str) ungettext_lazy = lazy(ungettext, unicode) ugettext_lazy = lazy(ugettext, unicode) +pgettext_lazy = lazy(pgettext, unicode) +npgettext_lazy = lazy(npgettext, unicode) def activate(language): return _trans.activate(language) diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py index 8a075806ec..a8bebad942 100644 --- a/django/utils/translation/trans_null.py +++ b/django/utils/translation/trans_null.py @@ -15,6 +15,12 @@ ngettext_lazy = ngettext def ungettext(singular, plural, number): return force_unicode(ngettext(singular, plural, number)) +def pgettext(context, message): + return ugettext(message) + +def npgettext(context, singular, plural, number): + return ungettext(singular, plural, number) + activate = lambda x: None deactivate = deactivate_all = lambda: None get_language = lambda: settings.LANGUAGE_CODE diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index fe34b6ab0f..8486fdf8f4 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -24,6 +24,9 @@ _default = None # file lookups when checking the same locale on repeated requests. _accepted = {} +# magic gettext number to separate context from message +CONTEXT_SEPARATOR = u"\x04" + # Format of Accept-Language header values. From RFC 2616, section 14.4 and 3.9. accept_language_re = re.compile(r''' ([A-Za-z]{1,8}(?:-[A-Za-z]{1,8})*|\*) # "en", "en-au", "x-y-z", "*" @@ -279,6 +282,14 @@ def gettext(message): def ugettext(message): return do_translate(message, 'ugettext') +def pgettext(context, message): + result = do_translate( + u"%s%s%s" % (context, CONTEXT_SEPARATOR, message), 'ugettext') + if CONTEXT_SEPARATOR in result: + # Translation not found + result = message + return result + def gettext_noop(message): """ Marks strings for translation but doesn't translate them now. This can be @@ -313,6 +324,15 @@ def ungettext(singular, plural, number): """ return do_ntranslate(singular, plural, number, 'ungettext') +def npgettext(context, singular, plural, number): + result = do_ntranslate(u"%s%s%s" % (context, CONTEXT_SEPARATOR, singular), + u"%s%s%s" % (context, CONTEXT_SEPARATOR, plural), + number, 'ungettext') + if CONTEXT_SEPARATOR in result: + # Translation not found + result = do_ntranslate(singular, plural, number, 'ungettext') + return result + def check_for_language(lang_code): """ Checks whether there is a global language file for the given language |
