示例#1
0
    def translation(self) -> str:
        """
        Return the translated string with interpolated kwargs, if provided.
        """

        if self._n is None and self._msgctxt is None:
            translation = gettext.dgettext(
                self._domain,
                self._msgid,
            )
        elif self._n is None:
            translation = gettext.dpgettext(
                self._domain,
                self._msgctxt,
                self._msgid,
            )
        elif self._msgctxt is None:
            translation = gettext.dngettext(
                self._domain,
                self._msgid,
                self._msgid_plural,
                self._n,
            )
        else:
            translation = gettext.dnpgettext(
                self._domain,
                self._msgctxt,
                self._msgid,
                self._msgid_plural,
                self._n,
            )

        return translation.format(**self._kwargs)
示例#2
0
    def _dnpgettext(
        self,
        msgctxt: Optional[str] = None,
        msgid: Optional[str] = None,
        msgid_plural: Optional[str] = None,
        n: Optional[int] = None,
        **kwargs,
    ) -> str:
        """
        Helper to handle all trans methods and delegate to corresponding
        gettext methods.

        Parameters
        ----------
        msgctxt : str, optional
            The message context.
        msgid : str, optional
            The singular string to translate.
        msgid_plural : str, optional
            The plural string to translate.
        n : int, optional
            The number for pluralization.
        **kwargs : dict, optional
            Any additional arguments to use when formating the string.
        """
        if msgid is None:
            trans = self
            raise ValueError(
                trans._("Must provide at least a `msgid` parameter!",
                        deferred=True))

        if PY37_OR_LOWER:
            translation = (gettext.dgettext(self._domain, msgid)
                           if n is None else gettext.dngettext(
                               self._domain, msgid, msgid_plural, n))
        else:
            if n is None and msgctxt is None:
                translation = gettext.dgettext(self._domain, msgid)
            elif n is None:
                translation = gettext.dpgettext(self._domain, msgctxt, msgid)
            elif msgctxt is None:
                translation = gettext.dngettext(
                    self._domain,
                    msgid,
                    msgid_plural,
                    n,
                )
            else:
                translation = gettext.dnpgettext(
                    self._domain,
                    msgctxt,
                    msgid,
                    msgid_plural,
                    n,
                )

        kwargs['n'] = n
        return translation.format(**kwargs)
示例#3
0
    def pgettext(self, msgctxt: str, singular: str) -> str:
        """
        Translate a singular string with context.

        Parameters
        ----------
        msgctxt: str
            The message context.
        msgid: str
            The singular string to translate.

        Returns
        -------
        str
            The translated string.
        """
        return gettext.dpgettext(self._domain, msgctxt, msgid)
示例#4
0
    def translation(self) -> str:
        """
        Return the translated string with interpolated kwargs, if provided.
        """
        # Python 3.7 or lower does not offer translations based on context.
        # On these versions `gettext.npgettext` falls back to `gettext.ngettext`
        if PY37_OR_LOWER:
            if self._n is None:
                translation = gettext.dgettext(self._domain, self._msgid)
            else:
                translation = gettext.dngettext(
                    self._domain, self._msgid, self._msgid_plural, self._n
                )
        else:
            if self._n is None and self._msgctxt is None:
                translation = gettext.dgettext(
                    self._domain,
                    self._msgid,
                )
            elif self._n is None:
                translation = gettext.dpgettext(
                    self._domain,
                    self._msgctxt,
                    self._msgid,
                )
            elif self._msgctxt is None:
                translation = gettext.dngettext(
                    self._domain,
                    self._msgid,
                    self._msgid_plural,
                    self._n,
                )
            else:
                translation = gettext.dnpgettext(
                    self._domain,
                    self._msgctxt,
                    self._msgid,
                    self._msgid_plural,
                    self._n,
                )

        return translation.format(**self._kwargs)
    def pgettext(self, msgctxt: str, msgid: str) -> str:
        """
        Translate a singular string with context.

        Parameters
        ----------
        msgctxt: str
            The message context.
        msgid: str
            The singular string to translate.

        Returns
        -------
        str
            The translated string.
        """
        # Python 3.7 or lower does not offer translations based on context.
        # On these versions `pgettext` falls back to `gettext`
        if PY37_OR_LOWER:
            translation = gettext.dgettext(self._domain, msgid)
        else:
            translation = gettext.dpgettext(self._domain, msgctxt, msgid)

        return translation
示例#6
0
 def test_some_translations_with_context_and_domain(self):
     eq = self.assertEqual
     eq(gettext.dpgettext('gettext', 'my context', 'nudge nudge'),
        'wink wink (in "my context")')
     eq(gettext.dpgettext('gettext', 'my other context', 'nudge nudge'),
        'wink wink (in "my other context")')
 def test_some_translations_with_context_and_domain(self):
     eq = self.assertEqual
     eq(gettext.dpgettext('gettext', 'my context', 'nudge nudge'),
        'wink wink (in "my context")')
     eq(gettext.dpgettext('gettext', 'my other context', 'nudge nudge'),
        'wink wink (in "my other context")')
示例#8
0
 def update_event(self, inp=-1):
     self.set_output_val(
         0, gettext.dpgettext(self.input(0), self.input(1), self.input(2)))