Beispiel #1
0
    def _i18n_string(self, token_slug, lang_code=None):

        # fetch the persistant app object for this
        # app, since the tokens are linked to it
        persistant_app = PersistantApp.resolve(self)
        if persistant_app is None:

            # this shouldn't ever happen, so log and abort
            self.warning("PersistantApp does not exist for %r" % (self))
            return None

        # we accept a few different language-aware
        # objects, so resolve this elsewhere. the
        # output is always a language code
        if lang_code is not None:
            language = self._language(
                lang_code)

            # if the langauage code doesn't exist, (which shouldn't
            # happen, since the code passed to this method should be
            # sourced from previously-validated Language objects),
            # warn and fall back to the system default
            if language is None:
                self.warning(
                    "No such language: %r" %\
                    (lang_code))

                # fall back to the system default.
                # it's better than nothing at all
                language = Language.default()

        # or if the lang_code was omitted,
        # fall back to the system default
        else:
            language = Language.default()

        # fetch the token object via its slug,
        # which should be defined in locale.py
        try:
            token = persistant_app.token_set.get(
                slug=token_slug)

        # token objects don't auto-spawn any more, so this
        # could happen because of a typo or something. warn,
        # but let the calling method deal with it
        except Token.DoesNotExist:
            self.warning(
                "No such token: %s" %\
                (token_slug))
            return None

        # attempt to fetch the translation of the token in the
        # requested language, and warn/abort if none were found
        string = token.translation(language)
        if string is None:
            self.warning(
                "No strings for token: %s in language: %s (or fallbacks)" %\
                (token_slug, lang_code))
            return None

        return string