Python gettext 模块,translation() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用gettext.translation()

项目:StarCraft-Casting-Tool    作者:teampheenix    | 项目源码 | 文件源码
def changeLanguage(self, language):
        """Change the language."""
        try:
            lang = gettext.translation(
                'messages', localedir='locales', languages=[language])
            lang.install()
        except:
            lang = gettext.NullTranslations()

        self.app.removeTranslator(self.translator)
        self.translator = PyQt5.QtCore.QTranslator(self.app)
        self.translator.load(PyQt5.QtCore.QLocale(language),
                             "qtbase", "_",  scctool.settings.getAbsPath('locales'), ".qm")
        self.app.installTranslator(self.translator)

        scctool.settings.config.parser.set("SCT", "language", language)
        self.restart()
项目:sndlatr    作者:Schibum    | 项目源码 | 文件源码
def __resource_exists(name):
    """Return true if the given resource exists"""
    try:
        open_resource(name).close()
        return True
    except IOError:
        return False


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
# 
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:CodingDojo    作者:ComputerSocietyUNB    | 项目源码 | 文件源码
def __init__(self, language):
        """Create a GNUTranslations() using many locale directories"""
        gettext_module.GNUTranslations.__init__(self)
        self.set_output_charset('utf-8')  # For Python 2 gettext() (#25720)

        self.__language = language
        self.__to_language = to_language(language)
        self.__locale = to_locale(language)
        self._catalog = None

        self._init_translation_catalog()
        self._add_installed_apps_translations()
        self._add_local_translations()
        if self.__language == settings.LANGUAGE_CODE and self._catalog is None:
            # default lang should have at least one translation file available.
            raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)
        self._add_fallback()
        if self._catalog is None:
            # No catalogs found for this language, set an empty catalog.
            self._catalog = {}
项目:enigma2    作者:OpenLD    | 项目源码 | 文件源码
def activateLanguage(self, index):
        try:
            lang = self.lang[index]
            print "Activating language " + lang[0]
            self.catalog = gettext.translation('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), languages=[index], fallback=True)
            self.catalog.install(names=("ngettext", "pgettext"))
            self.activeLanguage = index
            for x in self.callbacks:
                if x:
                    x()
        except:
            print "Selected language does not exist!"
        # NOTE: we do not use LC_ALL, because LC_ALL will not set any of the categories, when one of the categories fails.
        # We'd rather try to set all available categories, and ignore the others
        for category in [locale.LC_CTYPE, locale.LC_COLLATE, locale.LC_TIME, locale.LC_MONETARY, locale.LC_MESSAGES, locale.LC_NUMERIC]:
            try:
                locale.setlocale(category, (self.getLanguage(), 'UTF-8'))
            except:
                pass
        # HACK: sometimes python 2.7 reverts to the LC_TIME environment value, so make sure it has the correct value
        os.environ["LC_TIME"] = self.getLanguage() + '.UTF-8'
        os.environ["LANGUAGE"] = self.getLanguage() + '.UTF-8'
        os.environ["GST_SUBTITLE_ENCODING"] = self.getGStreamerSubtitleEncoding()
项目:enigma2    作者:OpenLD    | 项目源码 | 文件源码
def updateLanguageCache(self):
        t = localtime(time())
        createdate = strftime("%d.%m.%Y  %H:%M:%S", t)
        f = open('/usr/lib/enigma2/python/Components/Language_cache.py','w')
        f.write('# -*- coding: UTF-8 -*-\n')
        f.write('# date: ' + createdate + '\n#\n\n')
        f.write('LANG_TEXT = {\n')
        for lang in self.langlist:
            catalog = gettext.translation('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), languages=[str(lang)], fallback=True)
            T1 = catalog.gettext("Please use the UP and DOWN keys to select your language. Afterwards press the OK button.")
            T2 = catalog.gettext("Language selection")
            T3 = catalog.gettext("Cancel")
            T4 = catalog.gettext("Save")
            f.write('"' + lang + '"' + ': {\n')
            f.write('\t "T1"' + ': "' + T1 + '",\n')
            f.write('\t "T2"' + ': "' + T2 + '",\n')
            f.write('\t "T3"' + ': "' + T3 + '",\n')
            f.write('\t "T4"' + ': "' + T4 + '",\n')
            f.write('},\n')
        f.write('}\n')
        f.close
        catalog = None
        lang = None
项目:Sci-Finder    作者:snverse    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:Sci-Finder    作者:snverse    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:Texty    作者:sarthfrey    | 项目源码 | 文件源码
def resource_exists(name):
    """Return true if the given resource exists"""
    try:
        open_resource(name).close()
        return True
    except IOError:
        return False


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
# 
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:gprime    作者:GenealogyCollective    | 项目源码 | 文件源码
def check_available_translations(self, locale):
        """
        Test a locale for having a translation available
        locale -- string with standard language code, locale code, or name
        """
        if not self.localedir:
            return None
        #Note that this isn't a typo for self.language; self.languages
        #is cached so we don't have to query the file system every
        #time this function is called.
        if not hasattr(self, 'languages'):
            self.languages = self.get_available_translations()

        if not locale:
            return None

        if locale[:5] in self.languages:
            return locale[:5]
        #US English is the outlier, all other English locales want real English:
        if locale[:2] == 'en' and locale[:5] != 'en_US':
            return 'en_GB'
        if locale[:2] in self.languages:
            return locale[:2]
        return None
项目:gprime    作者:GenealogyCollective    | 项目源码 | 文件源码
def ngettext(self, singular, plural, num):
        """
        The translation of singular/plural is returned unless the translation is
        not available and the singular contains the separator. In that case,
        the returned value is the singular.

        :param singular: The singular form of the string to be translated.
                         may contain a context seperator
        :type singular: unicode
        :param plural: The plural form of the string to be translated.
        :type plural: unicode
        :param num: the amount for which to decide the translation
        :type num: int
        :returns: Translation or the original.
        :rtype: unicode
        """
        return gettext.GNUTranslations.ngettext(self, singular, plural, num)
项目:gprime    作者:GenealogyCollective    | 项目源码 | 文件源码
def sgettext(self, msgid, sep='|'):
        """
        Strip the context used for resolving translation ambiguities.

        The translation of msgid is returned unless the translation is
        not available and the msgid contains the separator. In that case,
        the returned value is the portion of msgid following the last
        separator. Default separator is '|'.

        :param msgid: The string to translated.
        :type msgid: unicode
        :param sep: The separator marking the context.
        :type sep: unicode
        :returns: Translation or the original with context stripped.
        :rtype: unicode
        """
        msgval = self.gettext(msgid)
        if msgval == msgid:
            sep_idx = msgid.rfind(sep)
            msgval = msgid[sep_idx+1:]
        return msgval
项目:python-kingbirdclient    作者:openstack    | 项目源码 | 文件源码
def __init__(self, domain, lazy=False, localedir=None):
        """Establish a set of translation functions for the domain.

        :param domain: Name of translation domain,
                       specifying a message catalog.
        :type domain: str
        :param lazy: Delays translation until a message is emitted.
                     Defaults to False.
        :type lazy: Boolean
        :param localedir: Directory with translation catalogs.
        :type localedir: str
        """
        self.domain = domain
        self.lazy = lazy
        if localedir is None:
            localedir = os.environ.get(domain.upper() + '_LOCALEDIR')
        self.localedir = localedir
项目:python-kingbirdclient    作者:openstack    | 项目源码 | 文件源码
def _make_translation_func(self, domain=None):
        """Return a new translation function ready for use.

        Takes into account whether or not lazy translation is being
        done.

        The domain can be specified to override the default from the
        factory, but the localedir from the factory is always used
        because we assume the log-level translation catalogs are
        installed in the same directory as the main application
        catalog.

        """
        if domain is None:
            domain = self.domain
        if self.lazy:
            return functools.partial(Message, domain=domain)
        t = gettext.translation(
            domain,
            localedir=self.localedir,
            fallback=True,
        )
        if six.PY3:
            return t.gettext
        return t.ugettext
项目:python-kingbirdclient    作者:openstack    | 项目源码 | 文件源码
def _translate_msgid(msgid, domain, desired_locale=None):
        if not desired_locale:
            system_locale = locale.getdefaultlocale()
            # If the system locale is not available to the runtime use English
            if not system_locale[0]:
                desired_locale = 'en_US'
            else:
                desired_locale = system_locale[0]

        locale_dir = os.environ.get(domain.upper() + '_LOCALEDIR')
        lang = gettext.translation(domain,
                                   localedir=locale_dir,
                                   languages=[desired_locale],
                                   fallback=True)
        if six.PY3:
            translator = lang.gettext
        else:
            translator = lang.ugettext

        translated_message = translator(msgid)
        return translated_message
项目:python-kingbirdclient    作者:openstack    | 项目源码 | 文件源码
def _sanitize_mod_params(self, other):
        """Sanitize the object being modded with this Message.

        - Add support for modding 'None' so translation supports it
        - Trim the modded object, which can be a large dictionary, to only
        those keys that would actually be used in a translation
        - Snapshot the object being modded, in case the message is
        translated, it will be used as it was when the Message was created
        """
        if other is None:
            params = (other,)
        elif isinstance(other, dict):
            # Merge the dictionaries
            # Copy each item in case one does not support deep copy.
            params = {}
            if isinstance(self.params, dict):
                for key, val in self.params.items():
                    params[key] = self._copy_param(val)
            for key, val in other.items():
                params[key] = self._copy_param(val)
        else:
            params = self._copy_param(other)
        return params
项目:InplusTrader_Linux    作者:zhengwsh    | 项目源码 | 文件源码
def set_locale(self, locales, trans_dir=None):
        if locales[0] is None or "en" in locales[0].lower():
            self.trans = NullTranslations()
            return
        if "cn" in locales[0].lower():
            locales = ["zh_Hans_CN"]
        try:
            if trans_dir is None:
                trans_dir = os.path.join(
                    os.path.dirname(
                        os.path.abspath(
                            __file__,
                        ),
                    ),
                    "translations"
                )
            self.trans = translation(
                domain="messages",
                localedir=trans_dir,
                languages=locales,
            )
        except Exception as e:
            system_log.debug(e)
            self.trans = NullTranslations()
项目:InplusTrader_Linux    作者:zhengwsh    | 项目源码 | 文件源码
def set_locale(self, locales, trans_dir=None):
        if locales[0] is None or "en" in locales[0].lower():
            self.trans = NullTranslations()
            return
        if "cn" in locales[0].lower():
            locales = ["zh_Hans_CN"]
        try:
            if trans_dir is None:
                trans_dir = os.path.join(
                    os.path.dirname(
                        os.path.abspath(
                            __file__,
                        ),
                    ),
                    "translations"
                )
            self.trans = translation(
                domain="messages",
                localedir=trans_dir,
                languages=locales,
            )
        except Exception as e:
            system_log.debug(e)
            self.trans = NullTranslations()
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def findtranslation(self):
    "Find the translation for the document language."
    self.langcodes = None
    if not DocumentParameters.language:
      Trace.error('No language in document')
      return
    if not DocumentParameters.language in TranslationConfig.languages:
      Trace.error('Unknown language ' + DocumentParameters.language)
      return
    if TranslationConfig.languages[DocumentParameters.language] == 'en':
      return
    langcodes = [TranslationConfig.languages[DocumentParameters.language]]
    try:
      self.translation = gettext.translation('elyxer', None, langcodes)
    except IOError:
      Trace.error('No translation for ' + unicode(langcodes))
项目:true_review    作者:lucadealfaro    | 项目源码 | 文件源码
def resource_exists(name):
    return loader.resource_exists(name)


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
#
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:AshsSDK    作者:thehappydinoa    | 项目源码 | 文件源码
def findtranslation(self):
    "Find the translation for the document language."
    self.langcodes = None
    if not DocumentParameters.language:
      Trace.error('No language in document')
      return
    if not DocumentParameters.language in TranslationConfig.languages:
      Trace.error('Unknown language ' + DocumentParameters.language)
      return
    if TranslationConfig.languages[DocumentParameters.language] == 'en':
      return
    langcodes = [TranslationConfig.languages[DocumentParameters.language]]
    try:
      self.translation = gettext.translation('elyxer', None, langcodes)
    except IOError:
      Trace.error('No translation for ' + unicode(langcodes))
项目:flasky    作者:RoseOu    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:aiyprojects-raspbian    作者:google    | 项目源码 | 文件源码
def set_language_code(code, gettext_install=False):
    """Set the BCP-47 language code that the speech systems should use.

    Args:
      gettext_install: if True, gettext's _() will be installed in as a builtin.
          As this has global effect, it should only be done by applications.
    """
    global _language_code
    _language_code = code.replace('_', '-')

    if gettext_install:
        if not _locale_dir:
            raise ValueError('locale_dir is not set. Please call set_locale_dir().')
        language_id = code.replace('-', '_')
        t = gettext.translation(_LOCALE_DOMAIN, _locale_dir, [language_id], fallback=True)
        t.install()
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:enigma2    作者:Openeight    | 项目源码 | 文件源码
def activateLanguage(self, index):
        try:
            lang = self.lang[index]
            print "Activating language " + lang[0]
            self.catalog = gettext.translation('enigma2', resolveFilename(SCOPE_LANGUAGE, ""), languages=[index], fallback=True)
            self.catalog.install(names=("ngettext", "pgettext"))
            self.activeLanguage = index
            for x in self.callbacks:
                x()
        except:
            print "Selected language does not exist!"
        # NOTE: we do not use LC_ALL, because LC_ALL will not set any of the categories, when one of the categories fails.
        # We'd rather try to set all available categories, and ignore the others
        for category in [locale.LC_CTYPE, locale.LC_COLLATE, locale.LC_TIME, locale.LC_MONETARY, locale.LC_MESSAGES, locale.LC_NUMERIC]:
            try:
                locale.setlocale(category, (self.getLanguage(), 'UTF-8'))
            except:
                pass
        # HACK: sometimes python 2.7 reverts to the LC_TIME environment value, so make sure it has the correct value
        os.environ["LC_TIME"] = self.getLanguage() + '.UTF-8'
        os.environ["LANGUAGE"] = self.getLanguage() + '.UTF-8'
        os.environ["GST_SUBTITLE_ENCODING"] = self.getGStreamerSubtitleEncoding()
项目:K40-Whisperer    作者:jkramarz    | 项目源码 | 文件源码
def errormsg(msg):
    """Intended for end-user-visible error messages.

       (Currently just writes to stderr with an appended newline, but could do
       something better in future: e.g. could add markup to distinguish error
       messages from status messages or debugging output.)

       Note that this should always be combined with translation:

         import inkex
         ...
         inkex.errormsg(_("This extension requires two selected paths."))
    """
    if isinstance(msg, unicode):
        sys.stderr.write(msg.encode("utf-8") + "\n")
    else:
        sys.stderr.write((unicode(msg, "utf-8", errors='replace') + "\n").encode("utf-8"))
项目:epg-dk.bundle    作者:ukdtom    | 项目源码 | 文件源码
def resource_exists(name):
    """Return true if the given resource exists"""
    try:
        open_resource(name).close()
        return True
    except IOError:
        return False


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
#
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_the_alternative_interface(self):
        eq = self.assertEqual
        # test the alternative interface
        with open(self.mofile, 'rb') as fp:
            t = gettext.GNUTranslations(fp)
        # Install the translation object
        t.install()
        eq(_('nudge nudge'), 'wink wink')
        # Try unicode return type
        t.install()
        eq(_('mullusk'), 'bacon')
        # Test installation of other methods
        import builtins
        t.install(names=["gettext", "lgettext"])
        eq(_, t.gettext)
        eq(builtins.gettext, t.gettext)
        eq(lgettext, t.lgettext)
        del builtins.gettext
        del builtins.lgettext
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_cache(self):
        self.localedir = os.curdir
        self.mofile = MOFILE

        self.assertEqual(len(gettext._translations), 0)

        t = gettext.translation('gettext', self.localedir)

        self.assertEqual(len(gettext._translations), 1)

        t = gettext.translation('gettext', self.localedir,
                                class_=DummyGNUTranslations)

        self.assertEqual(len(gettext._translations), 2)
        self.assertEqual(t.__class__, DummyGNUTranslations)

        # Calling it again doesn't add to the cache

        t = gettext.translation('gettext', self.localedir,
                                class_=DummyGNUTranslations)

        self.assertEqual(len(gettext._translations), 2)
        self.assertEqual(t.__class__, DummyGNUTranslations)
项目:chihu    作者:yelongyu    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:pdftohtml5canvas    作者:greipfrut    | 项目源码 | 文件源码
def errormsg(msg):
    """Intended for end-user-visible error messages.

       (Currently just writes to stderr with an appended newline, but could do
       something better in future: e.g. could add markup to distinguish error
       messages from status messages or debugging output.)

       Note that this should always be combined with translation:

         import inkex
         inkex.localize()
         ...
         inkex.errormsg(_("This extension requires two selected paths."))
    """
    if isinstance(msg, unicode):
        sys.stderr.write(msg.encode("UTF-8") + "\n")
    else:
        sys.stderr.write((unicode(msg, "utf-8", errors='replace') + "\n").encode("UTF-8"))
项目:pyetje    作者:rorlika    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:respeaker_virtualenv    作者:respeaker    | 项目源码 | 文件源码
def resource_exists(name):
    """Return true if the given resource exists"""
    try:
        open_resource(name).close()
        return True
    except IOError:
        return False


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
#
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:userbase-sns-lambda    作者:fartashh    | 项目源码 | 文件源码
def resource_exists(name):
    """Return true if the given resource exists"""
    try:
        open_resource(name).close()
        return True
    except IOError:
        return False


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
#
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:FileStoreGAE    作者:liantian-cn    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_the_alternative_interface(self):
        eq = self.assertEqual
        # test the alternative interface
        with open(self.mofile, 'rb') as fp:
            t = gettext.GNUTranslations(fp)
        # Install the translation object
        t.install()
        eq(_('nudge nudge'), 'wink wink')
        # Try unicode return type
        t.install(unicode=True)
        eq(_('mullusk'), 'bacon')
        # Test installation of other methods
        import __builtin__
        t.install(unicode=True, names=["gettext", "lgettext"])
        eq(_, t.ugettext)
        eq(__builtin__.gettext, t.ugettext)
        eq(lgettext, t.lgettext)
        del __builtin__.gettext
        del __builtin__.lgettext
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_cache(self):
        self.localedir = os.curdir
        self.mofile = MOFILE

        self.assertEqual(len(gettext._translations), 0)

        t = gettext.translation('gettext', self.localedir)

        self.assertEqual(len(gettext._translations), 1)

        t = gettext.translation('gettext', self.localedir,
                                class_=DummyGNUTranslations)

        self.assertEqual(len(gettext._translations), 2)
        self.assertEqual(t.__class__, DummyGNUTranslations)

        # Calling it again doesn't add to the cache

        t = gettext.translation('gettext', self.localedir,
                                class_=DummyGNUTranslations)

        self.assertEqual(len(gettext._translations), 2)
        self.assertEqual(t.__class__, DummyGNUTranslations)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_the_alternative_interface(self):
        eq = self.assertEqual
        # test the alternative interface
        with open(self.mofile, 'rb') as fp:
            t = gettext.GNUTranslations(fp)
        # Install the translation object
        t.install()
        eq(_('nudge nudge'), 'wink wink')
        # Try unicode return type
        t.install(unicode=True)
        eq(_('mullusk'), 'bacon')
        # Test installation of other methods
        import __builtin__
        t.install(unicode=True, names=["gettext", "lgettext"])
        eq(_, t.ugettext)
        eq(__builtin__.gettext, t.ugettext)
        eq(lgettext, t.lgettext)
        del __builtin__.gettext
        del __builtin__.lgettext
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_cache(self):
        self.localedir = os.curdir
        self.mofile = MOFILE

        self.assertEqual(len(gettext._translations), 0)

        t = gettext.translation('gettext', self.localedir)

        self.assertEqual(len(gettext._translations), 1)

        t = gettext.translation('gettext', self.localedir,
                                class_=DummyGNUTranslations)

        self.assertEqual(len(gettext._translations), 2)
        self.assertEqual(t.__class__, DummyGNUTranslations)

        # Calling it again doesn't add to the cache

        t = gettext.translation('gettext', self.localedir,
                                class_=DummyGNUTranslations)

        self.assertEqual(len(gettext._translations), 2)
        self.assertEqual(t.__class__, DummyGNUTranslations)
项目:dodotable    作者:spoqa    | 项目源码 | 文件源码
def get_translations(self):
        if self.get_locale is None:
            return None
        locale = self.get_locale()
        if locale is None:
            return None
        elif not isinstance(locale, str):
            # locale might be an instance of babel.core.Locale
            locale = str(locale)
        if '_' in locale or '-' in locale:
            # If the locale has territory (e.g. 'ko_KR')
            # we can search the proper match (e.g. ko_KR) and then
            # the non-territory match (e.g. ko) as a fallback.
            locale = locale.replace('-', '_')
            locales = [locale, locale[:locale.index('_')]]
        else:
            locales = [locale]
        return gettext.translation(
            'dodotable',
            os.path.join(os.path.dirname(__file__), '..', 'locale'),
            fallback=True,
            codeset='utf-8',
            languages=locales
        )
项目:chalktalk_docs    作者:loremIpsum1771    | 项目源码 | 文件源码
def findtranslation(self):
    "Find the translation for the document language."
    self.langcodes = None
    if not DocumentParameters.language:
      Trace.error('No language in document')
      return
    if not DocumentParameters.language in TranslationConfig.languages:
      Trace.error('Unknown language ' + DocumentParameters.language)
      return
    if TranslationConfig.languages[DocumentParameters.language] == 'en':
      return
    langcodes = [TranslationConfig.languages[DocumentParameters.language]]
    try:
      self.translation = gettext.translation('elyxer', None, langcodes)
    except IOError:
      Trace.error('No translation for ' + unicode(langcodes))
项目:chalktalk_docs    作者:loremIpsum1771    | 项目源码 | 文件源码
def resource_exists(name):
    """Return true if the given resource exists"""
    try:
        open_resource(name).close()
        return True
    except IOError:
        return False


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
# 
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:python-group-proj    作者:Sharcee    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:nicfit.py    作者:nicfit    | 项目源码 | 文件源码
def initGetText(domain, install=False, fallback=True):
    locale_paths = [
        Path(__file__).parent / ".." / "locale",
        Path(sys.prefix) / "share" / "locale",
    ]

    locale_dir, translation = None, None
    for locale_dir in [d for d in locale_paths if d.exists()]:
        if gettext.find(domain, str(locale_dir)):
            log.debug("Loading message catalogs from {}".format(locale_dir))
            translation = gettext.translation(domain, str(locale_dir))
            break

    if translation is None:
        # This with either throw FileNotFoundError (fallback=False) or set a
        # gettext.NullTranslations
        translation = gettext.translation(domain, str(locale_dir),
                                          fallback=fallback)
    assert translation

    if install:
        gettext.install(domain, str(locale_dir), names=["ngettext"])

    return translation
项目:Flask-NvRay-Blog    作者:rui7157    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:Flask-NvRay-Blog    作者:rui7157    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations
项目:Callandtext    作者:iaora    | 项目源码 | 文件源码
def resource_exists(name):
    """Return true if the given resource exists"""
    try:
        open_resource(name).close()
        return True
    except IOError:
        return False


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
# 
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:script.tvguide.fullscreen    作者:primaeval    | 项目源码 | 文件源码
def resource_exists(name):
    """Return true if the given resource exists"""
    try:
        open_resource(name).close()
        return True
    except IOError:
        return False


# Enable this when we get some translations?
# We want an i18n API that is useful to programs using Python's gettext
# module, as well as the Zope3 i18n package. Perhaps we should just provide
# the POT file and translations, and leave it up to callers to make use
# of them.
# 
# t = gettext.translation(
#         'pytz', os.path.join(os.path.dirname(__file__), 'locales'),
#         fallback=True
#         )
# def _(timezone_name):
#     """Translate a timezone name using the current locale, returning Unicode"""
#     return t.ugettext(timezone_name)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_the_alternative_interface(self):
        eq = self.assertEqual
        # test the alternative interface
        with open(self.mofile, 'rb') as fp:
            t = gettext.GNUTranslations(fp)
        # Install the translation object
        t.install()
        eq(_('nudge nudge'), 'wink wink')
        # Try unicode return type
        t.install()
        eq(_('mullusk'), 'bacon')
        # Test installation of other methods
        import builtins
        t.install(names=["gettext", "lgettext"])
        eq(_, t.gettext)
        eq(builtins.gettext, t.gettext)
        eq(lgettext, t.lgettext)
        del builtins.gettext
        del builtins.lgettext
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_cache(self):
        self.localedir = os.curdir
        self.mofile = MOFILE

        self.assertEqual(len(gettext._translations), 0)

        t = gettext.translation('gettext', self.localedir)

        self.assertEqual(len(gettext._translations), 1)

        t = gettext.translation('gettext', self.localedir,
                                class_=DummyGNUTranslations)

        self.assertEqual(len(gettext._translations), 2)
        self.assertEqual(t.__class__, DummyGNUTranslations)

        # Calling it again doesn't add to the cache

        t = gettext.translation('gettext', self.localedir,
                                class_=DummyGNUTranslations)

        self.assertEqual(len(gettext._translations), 2)
        self.assertEqual(t.__class__, DummyGNUTranslations)
项目:start    作者:argeweb    | 项目源码 | 文件源码
def get_translations(languages=None, getter=get_builtin_gnu_translations):
    """
    Get a WTForms translation object which wraps a low-level translations object.

    :param languages:
        A sequence of languages to try, in order.
    :param getter:
        A single-argument callable which returns a low-level translations object.
    """
    translations = getter(languages)

    if hasattr(translations, 'ugettext'):
        return DefaultTranslations(translations)
    else:
        # Python 3 has no ugettext/ungettext, so just return the translations object.
        return translations