def __new__(self, msgid, domain=None, default=None, mapping=None, context=None): # NB: this function should never never lose the *original # identity* of a non-``None`` but empty ``default`` value # provided to it. See the comment in ChameleonTranslate. self = text_type.__new__(self, msgid) if isinstance(msgid, self.__class__): domain = domain or msgid.domain and msgid.domain[:] context = context or msgid.context and msgid.context[:] default = default or msgid.default and msgid.default[:] if msgid.mapping: if mapping: for k, v in msgid.mapping.items(): mapping.setdefault(k, v) else: mapping = msgid.mapping.copy() msgid = text_type(msgid) self.domain = domain self.context = context if default is None: default = text_type(msgid) self.default = default self.mapping = mapping return self
def pluralizer(singular, plural, n, domain=None, mapping=None, context=None): """ Pluralize this object """ translated = text_type( policy(translations, singular, plural, n, domain, context)) if translated and '$' in translated and mapping: return TranslationString(translated, mapping=mapping).interpolate() return translated
def pluralizer(singular, plural, n, domain=None, mapping=None): """ Pluralize this object """ translated = text_type( policy(translations, singular, plural, n, domain)) if translated and '$' in translated and mapping: return TranslationString(translated, mapping=mapping).interpolate() return translated
def __new__(self, msgid, domain=None, default=None, mapping=None): # NB: this function should never never lose the *original # identity* of a non-``None`` but empty ``default`` value # provided to it. See the comment in ChameleonTranslate. self = text_type.__new__(self, _null + msgid) if isinstance(msgid, self.__class__): domain = domain or msgid.domain and msgid.domain[:] default = default or msgid.default and msgid.default[:] mapping = mapping or msgid.mapping and msgid.mapping.copy() msgid = text_type(msgid) self.domain = domain if default is None: default = text_type(msgid) self.default = default self.mapping = mapping return self
def __eq__(self, other): # First compare strings with strings; this tests that the # original message string compares favorably to an identical # string. string = text_type(other) if text_type.__eq__(self, string): return True # Next, interpolate and compare the result. return self.interpolate() == string
def translate(msgid, domain=None, mapping=None, context=None, target_language=None, default=None): # NB: note that both TranslationString._init__ and # TranslationString.interpolate are careful to never lose the # *identity* of an empty but non-``None`` ``default`` value we # provide to them. For example, neither of those functions # are permitted to run an empty but non-``None`` ``default`` # through ``unicode`` and throw the original default value # away afterwards. # This has a dubious cause: for Chameleon API reasons we must # ensure that, after translation, if ( (translated == msgid) # and (not default) and (default is not None) ) that we return # the ``default`` value provided to us *unmodified*, because # Chameleon uses it as a sentinel (it compares the return # value of this function by identity to what it passed in as # ``default``; this marker is a # chameleon.core.i18n.StringMarker instance, a subclass of str # that == ''). This is, of course, totally absurd, because # Chameleon *also* wants us to use ``default`` as the input to # a translation string in some cases, and maintaining the # identity of this object through translation operations isn't # a contract it spells out in its docs. # Chameleon's use of ``default`` to represent both a sentinel # and input to a translation string is a Chameleon i18n # extensibility design bug. Until we overhaul its hook point # for translation extensibility, we need to appease it by # preserving ``default`` in the aforementioned case. So we # spray these indignant comments all over this module. ;-) if not isinstance(msgid, string_types): if msgid is not None: msgid = text_type(msgid) return msgid tstring = msgid if not hasattr(tstring, 'interpolate'): tstring = TranslationString(msgid, domain, default, mapping, context) if translator is None: result = tstring.interpolate() else: result = translator(tstring) return result
def translate(msgid, domain=None, mapping=None, context=None, target_language=None, default=None): # NB: note that both TranslationString._init__ and # TranslationString.interpolate are careful to never lose the # *identity* of an empty but non-``None`` ``default`` value we # provide to them. For example, neither of those functions # are permitted to run an empty but non-``None`` ``default`` # through ``unicode`` and throw the original default value # away afterwards. # This has a dubious cause: for Chameleon API reasons we must # ensure that, after translation, if ( (translated == msgid) # and (not default) and (default is not None) ) that we return # the ``default`` value provided to us *unmodified*, because # Chameleon uses it as a sentinel (it compares the return # value of this function by identity to what it passed in as # ``default``; this marker is a # chameleon.core.i18n.StringMarker instance, a subclass of str # that == ''). This is, of course, totally absurd, because # Chameleon *also* wants us to use ``default`` as the input to # a translation string in some cases, and maintaining the # identity of this object through translation operations isn't # a contract it spells out in its docs. # Chameleon's use of ``default`` to represent both a sentinel # and input to a translation string is a Chameleon i18n # extensibility design bug. Until we overhaul its hook point # for translation extensibility, we need to appease it by # preserving ``default`` in the aforementioned case. So we # spray these indignant comments all over this module. ;-) if not isinstance(msgid, string_types): if msgid is not None: msgid = text_type(msgid) return msgid tstring = msgid if not hasattr(tstring, 'interpolate'): tstring = TranslationString(msgid, domain, default, mapping) if translator is None: result = tstring.interpolate() else: result = translator(tstring) return result
def __getstate__(self): return text_type(self), self.domain, self.default, self.mapping, self.context
def replace(match): whole, param1, param2 = match.groups() return text_type(self.mapping.get(param1 or param2, whole))
import re from gettext import NullTranslations from translationstring.compat import text_type from translationstring.compat import string_types from translationstring.compat import PY3 NAME_RE = r"[a-zA-Z][-a-zA-Z0-9_]*" _interp_regex = re.compile(r'(?<!\$)(\$(?:(%(n)s)|{(%(n)s)}))' % ({'n': NAME_RE})) CONTEXT_MASK = text_type('%s\x04%s') class TranslationString(text_type): """ The constructor for a :term:`translation string`. A translation string is a Unicode-like object that has some extra metadata. This constructor accepts one required argument named ``msgid``. ``msgid`` must be the :term:`message identifier` for the translation string. It must be a ``unicode`` object or a ``str`` object encoded in the default system encoding. Optional keyword arguments to this object's constructor include ``domain``, ``default``, and ``mapping``. ``domain`` represents the :term:`translation domain`. By default, the translation domain is ``None``, indicating that this translation string is associated with the default translation domain (usually ``messages``).
def __getstate__(self): return text_type(self), self.domain, self.default, self.mapping
import re from gettext import NullTranslations from translationstring.compat import text_type from translationstring.compat import string_types from translationstring.compat import PY3 NAME_RE = r"[a-zA-Z][-a-zA-Z0-9_]*" _interp_regex = re.compile(r'(?<!\$)(\$(?:(%(n)s)|{(%(n)s)}))' % ({ 'n': NAME_RE })) CONTEXT_MASK = text_type('%s\x04%s') class TranslationString(text_type): """ The constructor for a :term:`translation string`. A translation string is a Unicode-like object that has some extra metadata. This constructor accepts one required argument named ``msgid``. ``msgid`` must be the :term:`message identifier` for the translation string. It must be a ``unicode`` object or a ``str`` object encoded in the default system encoding. Optional keyword arguments to this object's constructor include ``domain``, ``default``, and ``mapping``. ``domain`` represents the :term:`translation domain`. By default, the translation domain is ``None``, indicating that this translation string is associated with the default translation
import re from gettext import NullTranslations from translationstring.compat import text_type from translationstring.compat import string_types from translationstring.compat import PY3 NAME_RE = r"[a-zA-Z][-a-zA-Z0-9_]*" _interp_regex = re.compile(r'(?<!\$)(\$(?:(%(n)s)|{(%(n)s)}))' % ({'n': NAME_RE})) _null = text_type() class TranslationString(text_type): """ The constructor for a :term:`translation string`. A translation string is a Unicode-like object that has some extra metadata. This constructor accepts one required argument named ``msgid``. ``msgid`` must be the :term:`message identifier` for the translation string. It must be a ``unicode`` object or a ``str`` object encoded in the default system encoding. Note that if the object is coerced to its native string type (i.e. string or unicode, depending on the platform), the default translation (or message identifier if not applicable) is returned with any mapped values applied. Optional keyword arguments to this object's constructor include ``domain``, ``default``, and ``mapping``.