-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path__init__.py
More file actions
89 lines (73 loc) · 3.17 KB
/
__init__.py
File metadata and controls
89 lines (73 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from django.conf import settings
from django.utils.translation import get_language, to_locale
from transifex.common.strings import LazyString
from transifex.native import tx
def translate(_string, _context=None, _escape=True, _key=None, **params):
"""Translate the given source string to the current language.
A convenience wrapper that uses the current language of a Django app.
If there are any placeholders to replace, they need to be passed as kwargs.
:param unicode _string: the source string to get the translation for
:param unicode _context: an optional context that gives more information
about the source string
:param bool _escape: if True, the returned string will be HTML-escaped,
otherwise it won't
:param str _key: an optional key that identifies this string;
if omitted, the key is generated automatically based on the
strings itself and its context
:return: the final translation in the current language
:rtype: unicode
"""
language = get_language()
if language is None:
is_source = True
locale = settings.LANGUAGE_CODE
else:
is_source = get_language() == settings.LANGUAGE_CODE
locale = to_locale(get_language()) # e.g. from en-us to en_US
return tx.translate(
_string,
locale,
_context=_context,
is_source=is_source,
escape=_escape,
_key=_key,
params=params,
)
def lazy_translate(_string, _context=None, _escape=True, **params):
"""Lazily translate the given source string to the current language.
Delays the evaluation of translating the given string until necessary.
This is useful in cases where the call to translate() happens
before any translations have been retrieved, e.g. in the definition
of a Python class.
See translate() for more details.
:param unicode _string: the source string to get the translation for
:param unicode _context: an optional context that gives more information
about the source string
:param bool _escape: if True, the returned string will be HTML-escaped,
otherwise it won't
:return: an object that when evaluated as a string will return
the final translation in the current language
:rtype: LazyString
"""
return LazyString(
translate,
_string,
_context=_context,
_escape=_escape,
fallback_value=_string,
**params
)
def utranslate(_string, _context=None, **params):
"""Translate the given source string to the current language, without HTML
escaping.
While the given `string` is not escaped, all `params` are, before replacing
the placeholders inside the `string`.
A convenience wrapper that uses the current language of a Django app.
If there are any placeholders to replace, they need to be passed as kwargs.
:param unicode _string: the source string to get the translation for
:param unicode _context: an optional context that gives more information
about the source string
:return: the final translation in the current language
:rtype: unicode
"""
return translate(_string, _context, _escape=False, **params)