示例#1
0
def install_gettext():
    """ Initialize gettext with the correct directory that contains
    MComix translations. This has to be done before any calls to gettext.gettext
    have been made to ensure all strings are actually translated. """

    # Add the sources' base directory to PATH to allow development without
    # explicitly installing the package.
    sys.path.append(constants.BASE_PATH)

    # Initialize default locale
    locale.setlocale(locale.LC_ALL, '')

    if preferences.prefs['language'] != 'auto':
        lang_identifiers = [preferences.prefs['language']]
    else:
        # Get the user's current locale
        code = portability.get_default_locale()
        lang_identifiers = gettext._expand_lang(code)

    domain = constants.APPNAME.lower()
    translation = gettext.NullTranslations()

    # Search for .mo files manually, since gettext doesn't support setuptools/pkg_resources.
    for lang in lang_identifiers:
        resource = os.path.join(lang, 'LC_MESSAGES', '%s.mo' % domain)
        if pkg_resources.resource_exists('mcomix.messages', resource):
            translation = gettext.GNUTranslations(
                pkg_resources.resource_stream('mcomix.messages', resource))
            break

    translation.install(unicode=True)

    global _translation
    _translation = translation
示例#2
0
def find(domain, localedir, languages, all=0):
    # now normalize and expand the languages
    nelangs = []
    languages = languages or []
    if not isinstance(languages, (tuple, list)):
        languages = [languages]
    for lang in languages:
        for nelang in gettext_module._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    if all:
        result = []
    else:
        result = None
    for dir in localedir:
        for lang in nelangs:
            mofile = os.path.join(dir, 'locale', lang, 'LC_MESSAGES',
                                  '%s.mo' % domain)
            if os.path.exists(mofile):
                if all:
                    result.append(mofile)
                else:
                    return mofile
    return result
示例#3
0
    def _get_method(self, method):
        try:
            languages = _thread_language.languages
        except AttributeError:
            languages = _global_languages

        # There are opportunities for caching here to improve performance.
        # Things like language normalization are fairly complicated.  However
        # since the list of languages could potentially be provided by a user,
        # a long lived process could accumulate a lot of cached entries, so
        # it would require some pruning logic, which is more than I want to do
        # right now.
        for language in languages():
            language = language.lower()
            if language == 'c':
                # This is a special case that forces no translation
                # (English because of POSIX standard).
                return getattr(self._null, method)
            # Shouldn't really be poking our nose at an internal function,
            # but this seems better than copying the entire thing.
            for lang in _gettext_module._expand_lang(language):
                try:
                    trans = self._chained_trans[lang.lower()]
                except KeyError:
                    # This language is not available.
                    pass
                else:
                    return getattr(trans, method)
        else:
            # None of the languages are available.
            return getattr(self._null, method)
示例#4
0
def __find(domain, locale_module, localedir='locale', languages=None):
    """
    Return the name of a .mo file using the gettext strategy.

    :param domain: Gettext domain name (e.g. your module name)
    :param localedir: directory containing locales (give 'locale' if you have locale/fr_FR/LC_MESSAGES/domain.mo)
    :param languages: languages to find (if None: calculated with LANGUAGE, LC_ALL, LC_MESSAGES, LANG env. variables)
    """
    # Get some reasonable defaults for arguments that were not supplied
    if languages is None:
        languages = []
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                break
        if 'C' not in languages:
            languages.append('C')
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        # noinspection PyProtectedMember
        for nelang in gettext_module._expand_lang(lang):  # pylint: disable=W0212
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    result = []
    for lang in nelangs:
        if lang == 'C':
            break
        mofile = '%s/%s/%s/%s.mo' % (localedir, lang, 'LC_MESSAGES', domain)
        if pkg_resources.resource_exists(locale_module, mofile):
            result.append(mofile)
    return result
示例#5
0
文件: i18n.py 项目: jerrykan/roundup
def find_locales(language=None):
    """Return normalized list of locale names to try for given language

    Argument 'language' may be a single language code or a list of codes.
    If 'language' is omitted or None, use locale settings in OS environment.

    """
    # body of this function is borrowed from gettext_module.find()
    if language is None:
        languages = []
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                break
    elif is_us(language):
        languages = [language]
    else:
        # 'language' must be iterable
        languages = language
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext_module._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    return nelangs
示例#6
0
文件: i18n.py 项目: AnishShah/roundup
def find_locales(language=None):
    """Return normalized list of locale names to try for given language

    Argument 'language' may be a single language code or a list of codes.
    If 'language' is omitted or None, use locale settings in OS environment.

    """
    # body of this function is borrowed from gettext_module.find()
    if language is None:
        languages = []
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                break
    elif isinstance(language, str) or  isinstance(language, unicode):
        languages = [language]
    else:
        # 'language' must be iterable
        languages = language
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext_module._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    return nelangs
示例#7
0
def FindCatalogs(domain, localedir=None, languages=None):
    # Get some reasonable defaults for arguments that were not supplied
    if localedir is None:
        localedir = gettext._default_localedir
    if languages is None:
        languages = []
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                break
        if 'C' not in languages:
            languages.append('C')
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    result = []
    for lang in nelangs:
        if lang == 'C':
            break
        mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
        result.append(mofile)
    return result
示例#8
0
文件: i18n.py 项目: Gosha/mcomix
def install_gettext():
    """ Initialize gettext with the correct directory that contains
    MComix translations. This has to be done before any calls to gettext.gettext
    have been made to ensure all strings are actually translated. """

    # Add the sources' base directory to PATH to allow development without
    # explicitly installing the package.
    sys.path.append(constants.BASE_PATH)

    # Initialize default locale
    locale.setlocale(locale.LC_ALL, '')

    if preferences.prefs['language'] != 'auto':
        lang_identifiers = [ preferences.prefs['language'] ]
    else:
        # Get the user's current locale
        code = portability.get_default_locale()
        lang_identifiers = gettext._expand_lang(code)

    domain = constants.APPNAME.lower()
    translation = gettext.NullTranslations()

    # Search for .mo files manually, since gettext doesn't support setuptools/pkg_resources.
    for lang in lang_identifiers:
        resource = os.path.join(lang, 'LC_MESSAGES', '%s.mo' % domain)
        if pkg_resources.resource_exists('mcomix.messages', resource):
            translation = gettext.GNUTranslations(
                    pkg_resources.resource_stream('mcomix.messages', resource))
            break

    translation.install(unicode=True)

    global _translation
    _translation = translation
示例#9
0
    def _get_method(self, method):
        try:
            languages = _thread_language.languages
        except AttributeError:
            languages = _global_languages

        # There are opportunities for caching here to improve performance.
        # Things like language normalization are fairly complicated.  However
        # since the list of languages could potentially be provided by a user,
        # a long lived process could accumulate a lot of cached entries, so
        # it would require some pruning logic, which is more than I want to do
        # right now.
        for language in languages():
            language = language.lower()
            if language == 'c':
                # This is a special case that forces no translation
                # (English because of POSIX standard).
                return getattr(self._null, method)
            # Shouldn't really be poking our nose at an internal function,
            # but this seems better than copying the entire thing.
            for lang in _gettext_module._expand_lang(language):
                try:
                    trans = self._chained_trans[lang.lower()]
                except KeyError:
                    # This language is not available.
                    pass
                else:
                    return getattr(trans, method)
        else:
            # None of the languages are available.
            return getattr(self._null, method)
def get_language_dirs():
    """
    Get possible language directories to be searched for, based on
    the environment variables: LANGUAGE, LC_ALL, LC_MESSAGES and LANG.
    The result is memoized for future use.

    This function is inspired by python gettext.py.

    Returns:
        dirs (array) - an array of language directories
    """

    global language_dirs
    if language_dirs is not None:
        return language_dirs

    languages = []
    for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
        val = os.environ.get(envar)
        if val:
            languages = val.split(':')
            break
    if 'C' in languages:
        languages.remove('C')

    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)

    language_dirs = nelangs
    return language_dirs
示例#11
0
def get_language_dirs():
    """
    Get possible language directories to be searched for, based on
    the environment variables: LANGUAGE, LC_ALL, LC_MESSAGES and LANG.
    The result is memoized for future use.

    This function is inspired by python gettext.py.

    Returns:
        dirs (array) - an array of language directories
    """

    global language_dirs
    if language_dirs is not None:
        return language_dirs

    languages = []
    for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
        val = os.environ.get(envar)
        if val:
            languages = val.split(':')
            break
    if 'C' in languages:
        languages.remove('C')

    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)

    language_dirs = nelangs
    return language_dirs
示例#12
0
文件: i18n.py 项目: fliphess/rdiffweb
def _find(domain, localedirs, languages=None):
    """
    Replacement for gettext.find() to search in multiple directory. This
    function return tuples for each mo file found: (lang, translation).
    """
    # Get some reasonable defaults for arguments that were not supplied
    if languages is None:
        languages = []
        for envar in ['LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG']:
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                break
        if 'C' not in languages:
            languages.append('C')
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    result = []
    for localedir in localedirs:
        for lang in nelangs:
            if lang == 'C':
                break
            mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
            if os.path.exists(mofile):
                entry = (lang, mofile)
                result.append(entry)
    return result
示例#13
0
 def index(self, value):
     for v in gettext._expand_lang(value):
         try:
             i = list.index(self, v)
             return i
         except ValueError:
             pass
     raise ValueError('%v not in list' % value)
示例#14
0
 def index(self, value):
     for v in gettext._expand_lang(value):
         try:
             i = list.index(self, v)
             return i
         except ValueError:
             pass
     raise ValueError('%v not in list'%value)
示例#15
0
def smart_gettext_and_install(domain, localedir, languages,
                              fallback=False, unicode=False):
    try:
        t = gettext.translation(domain, localedir, languages=languages,
                                fallback=fallback)
    except Exception, e:
        # if we failed to find the language, fetch it from the web async-style
        running_count = 0
        running_deferred = {}

        # Get some reasonable defaults for arguments that were not supplied
        if languages is None:
            languages = []
            for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
                val = os.environ.get(envar)
                if val:
                    languages = val.split(':')
                    break
            if 'C' not in languages:
                languages.append('C')

        # now normalize and expand the languages
        nelangs = []
        for lang in languages:
            for nelang in gettext._expand_lang(lang):
                if nelang not in nelangs:
                    nelangs.append(nelang)
        languages = nelangs

        for lang in languages:
            d = ThreadedDeferred(None, get_language, lang)
            def translate_and_install(r, td=d):
                running_deferred.pop(td)
                # only let the last one try to install
                if len(running_deferred) == 0:
                    t = gettext.translation(domain, localedir,
                                            languages=languages,
                                            fallback=True)
                    t.install(unicode)
            def failed(e, tlang=lang, td=d):
                if td in running_deferred:
                    running_deferred.pop(td)
                # don't raise an error, just continue untranslated
                sys.stderr.write('Could not find translation for language "%s"\n' %
                                 tlang)
                #traceback.print_exc(e)
            d.addCallback(translate_and_install)
            d.addErrback(failed)
            # accumulate all the deferreds first
            running_deferred[d] = 1

        # start them all, the last one finished will install the language
        for d in running_deferred:
            d.start()

        return
示例#16
0
 def find_mo_file(self, locale):
     identifiers = gettext._expand_lang(str(locale))
     for identifier in identifiers:
         path = os.path.join('/translations', identifier, 'LC_MESSAGES',
                             'messages.mo')
         try:
             return path, self.pod.open_file(path)
         except IOError:
             pass
     return None, None
示例#17
0
 def find_mo_file(self, locale):
     identifiers = gettext._expand_lang(str(locale))
     for identifier in identifiers:
         path = os.path.join(
             '/translations', identifier, 'LC_MESSAGES',
             'messages.mo')
         try:
             return path, self.pod.open_file(path)
         except IOError:
             pass
     return None, None
示例#18
0
def setUserLanguage(lang):
    global current

    langs = []
    for l in gettext._expand_lang(lang):
        if l not in langs:
            langs.append(l)

    try:
        current = gettext.translation(domain, localedir, languages=langs, fallback=True)
    except:
        pass
示例#19
0
def get_translation(domain, languages):
    """
    Find and fetch a translation catalog for a given domain and language set
    Analagous to the gettext.translation function, but also returns the matched locale name
    returns (locale name, catalog)
    """
    nelangs = []
    primary = None
    primary_mo = None
    t = None
    result = None
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    for lang in nelangs:
        mofiles = [
            os.path.join(LOCALE_PATH, lang, 'LC_MESSAGES', '%s.mo' % domain)
        ]
        # add translations defined by applications, if available
        for appname, app in apps.local_apps.items():
            app_mofile = os.path.join(app['full_path'], 'locale', lang,
                                      'LC_MESSAGES', '%s.mo' % domain)
            mofiles.append(app_mofile)
        import cherrypy
        for mofile in mofiles:
            key = os.path.abspath(mofile)
            t = _translations.get(key)
            if t is _no_translation:
                continue
            if not os.path.exists(mofile):
                _translations[key] = _no_translation
                continue
            if not primary:
                primary = lang
                primary_mo = mofile
            if t is None:
                t = _translations.setdefault(
                    key,
                    SparkleTranslations(open(mofile,
                                             'rb')))  # was GNUTranslations
                _translations_by_locale.setdefault(lang, t)
            # Copy the translation object to allow setting fallbacks and
            # output charset. All other instance data is shared with the
            # cached object.
            t = copy.copy(t)
            if result is None:
                result = t
            else:
                result.add_fallback(t)
    return (primary, primary_mo, result)
示例#20
0
    def _expand_languages(self, lang):
        import gettext
        languages = [lang]

        if 'C' not in languages:
            languages.append('C')

        # now normalize and expand the languages
        nelangs = []
        for lang in languages:
            for nelang in gettext._expand_lang(lang):
                if nelang not in nelangs:
                    nelangs.append(nelang)
        return nelangs
示例#21
0
def install_gettext():
    ''' Initialize gettext with the correct directory that contains
    MComix translations. This has to be done before any calls to gettext.gettext
    have been made to ensure all strings are actually translated. '''

    # Add the sources' base directory to PATH to allow development without
    # explicitly installing the package.
    sys.path.append(constants.BASE_PATH)

    # Initialize default locale
    locale.setlocale(locale.LC_ALL, '')

    lang_identifiers = []
    if preferences.prefs['language'] != 'auto':
        lang = preferences.prefs['language']
        if lang not in ('en', 'en_US'):
            # .mo is not needed for english
            lang_identifiers.append(lang)
    else:
        # Get the user's current locale
        lang = portability.get_default_locale()
        for s in gettext._expand_lang(lang):
            lang = s.split('.')[0]
            if lang in ('en', 'en_US'):
                # .mo is not needed for english
                continue
            if lang not in lang_identifiers:
                lang_identifiers.append(lang)

    # Make sure GTK uses the correct language.
    os.environ['LANGUAGE'] = lang

    domain = constants.APPNAME.lower()

    for lang in lang_identifiers:
        resource_path = tools.pkg_path('messages', lang, 'LC_MESSAGES',
                                       '%s.mo' % domain)
        try:
            with open(resource_path, mode='rb') as fp:
                translation = gettext.GNUTranslations(fp)
            break
        except IOError:
            log.error('locale file: %s not found.', resource_path)
    else:
        translation = gettext.NullTranslations()

    translation.install()

    global _translation
    _translation = translation
示例#22
0
文件: comps.py 项目: davidhrbac/dnf
    def _expand_languages(self, lang):
        import gettext
        languages = [lang]

        if 'C' not in languages:
            languages.append('C')
         
        # now normalize and expand the languages
        nelangs = []
        for lang in languages:
            for nelang in gettext._expand_lang(lang):
                if nelang not in nelangs:
                    nelangs.append(nelang)
        return nelangs
示例#23
0
文件: platform.py 项目: hitzjd/DHT
def blocking_smart_gettext_and_install(domain,
                                       localedir,
                                       languages,
                                       fallback=False,
                                       unicode=False):
    try:
        t = gettext.translation(domain,
                                localedir,
                                languages=languages,
                                fallback=fallback)
    except Exception, e:
        # if we failed to find the language, fetch it from the web
        running_count = 0
        running_deferred = {}

        # Get some reasonable defaults for arguments that were not supplied
        if languages is None:
            languages = []
            for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
                val = os.environ.get(envar)
                if val:
                    languages = val.split(':')
                    break
            if 'C' not in languages:
                languages.append('C')

        # now normalize and expand the languages
        nelangs = []
        for lang in languages:
            for nelang in gettext._expand_lang(lang):
                if nelang not in nelangs:
                    nelangs.append(nelang)
        languages = nelangs

        for lang in languages:
            # HACK
            if lang.startswith('en'):
                continue
            if lang.startswith('C'):
                continue
            try:
                get_language(lang)
            except:  #urllib.HTTPError:
                pass

        t = gettext.translation(domain,
                                localedir,
                                languages=languages,
                                fallback=True)
示例#24
0
def setUserLanguage(lang):
    global current

    if not len(lang.split('.')) > 1 and not lang.endswith('.UTF-8'):
        lang = "%s.UTF-8" % (lang)

    langs = []
    for l in gettext._expand_lang(lang):
        if l not in langs:
            langs.append(l)

    try:
        current = gettext.translation(domain, languages=langs, fallback=True)
    except:
        pass
示例#25
0
文件: comps.py 项目: Akasurde/dnf
    def get(self):
        current_locale = self._dotted_locale_str()
        if self.last_locale == current_locale:
            return self.cache

        self.cache = []
        locales = [current_locale]
        if current_locale != 'C':
            locales.append('C')
        for l in locales:
            for nlang in gettext._expand_lang(l):
                if nlang not in self.cache:
                    self.cache.append(nlang)

        self.last_locale = current_locale
        return self.cache
示例#26
0
文件: comps.py 项目: auchytil/dnf
    def get(self):
        current_locale = self._dotted_locale_str()
        if self.last_locale == current_locale:
            return self.cache

        self.cache = []
        locales = [current_locale]
        if current_locale != 'C':
            locales.append('C')
        for l in locales:
            for nlang in gettext._expand_lang(l):
                if nlang not in self.cache:
                    self.cache.append(nlang)

        self.last_locale = current_locale
        return self.cache
示例#27
0
def _getDefaultLangs():
    languages = []
    for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
        val = os.environ.get(envar)
        if val:
            languages = val.split(':')
            break
    if 'C' not in languages:
        languages.append('C')

    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    return nelangs
示例#28
0
def getDefaultLangs():
    languages = []
    for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
        val = os.environ.get(envar)
        if val:
            languages = val.split(':')
            break
    if 'C' not in languages:
        languages.append('C')

    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    return nelangs
示例#29
0
def setup_localisations(logger: logging.Logger):
    """Setup gettext localisations."""
    from srctools.property_parser import PROP_FLAGS_DEFAULT
    import gettext
    import locale

    # Get the 'en_US' style language code
    lang_code = locale.getdefaultlocale()[0]

    # Allow overriding through command line.
    if len(sys.argv) > 1:
        for arg in sys.argv[1:]:
            if arg.casefold().startswith('lang='):
                lang_code = arg[5:]
                break

    # Expands single code to parent categories.
    expanded_langs = gettext._expand_lang(lang_code)

    logger.info('Language: {!r}', lang_code)
    logger.debug('Language codes: {!r}', expanded_langs)

    # Add these to Property's default flags, so config files can also
    # be localised.
    for lang in expanded_langs:
        PROP_FLAGS_DEFAULT['lang_' + lang] = True

    for lang in expanded_langs:
        try:
            file = open('../i18n/{}.mo'.format(lang), 'rb')
        except FileNotFoundError:
            pass
        else:
            trans = gettext.GNUTranslations(file)
            break
    else:
        # No translations, fallback to English.
        # That's fine if the user's language is actually English.
        if 'en' not in expanded_langs:
            logger.warning(
                "Can't find translation for codes: {!r}!",
                expanded_langs,
            )
        trans = gettext.NullTranslations()
    # Add these functions to builtins, plus _=gettext
    trans.install(['gettext', 'ngettext'])
示例#30
0
文件: i18n.py 项目: DRArpitha/splunk
def get_translation(domain, languages):
    """
    Find and fetch a translation catalog for a given domain and language set
    Analagous to the gettext.translation function, but also returns the matched locale name
    returns (locale name, catalog)
    """
    nelangs = []
    primary = None
    primary_mo = None
    t = None
    result = None
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    for lang in nelangs:
        mofiles = [os.path.join(LOCALE_PATH, lang, 'LC_MESSAGES', '%s.mo' % domain)]
        # add translations defined by applications, if available
        for appname, app in apps.local_apps.items():
            app_mofile = os.path.join(app['full_path'], 'locale', lang, 'LC_MESSAGES', '%s.mo' % domain)
            mofiles.append(app_mofile)
        import cherrypy
        for mofile in mofiles:
            key = os.path.abspath(mofile)
            t = _translations.get(key)
            if t is _no_translation:
                continue
            if not os.path.exists(mofile):
                _translations[key] = _no_translation
                continue
            if not primary:
                primary = lang
                primary_mo = mofile
            if t is None:
                t = _translations.setdefault(key, SparkleTranslations(open(mofile, 'rb'))) # was GNUTranslations
                _translations_by_locale.setdefault(lang, t)
            # Copy the translation object to allow setting fallbacks and
            # output charset. All other instance data is shared with the
            # cached object.
            t = copy.copy(t)
            if result is None:
                result = t
            else:
                result.add_fallback(t)
    return (primary, primary_mo, result)
示例#31
0
def blocking_smart_gettext_and_install(domain, localedir, languages,
                                       fallback=False, unicode=False):
    try:
        t = gettext.translation(domain, localedir, languages=languages,
                                fallback=fallback)
    except Exception, e:
        # if we failed to find the language, fetch it from the web
        running_count = 0
        running_deferred = {}

        # Get some reasonable defaults for arguments that were not supplied
        if languages is None:
            languages = []
            for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
                val = os.environ.get(envar)
                if val:
                    languages = val.split(':')
                    break
            if 'C' not in languages:
                languages.append('C')

        # now normalize and expand the languages
        nelangs = []
        for lang in languages:
            for nelang in gettext._expand_lang(lang):
                if nelang not in nelangs:
                    nelangs.append(nelang)
        languages = nelangs

        for lang in languages:
            # HACK
            if lang.startswith('en'):
                continue
            if lang.startswith('C'):
                continue
            try:
                get_language(lang)
            except: #urllib.HTTPError:
                pass

        t = gettext.translation(domain, localedir,
                                languages=languages,
                                fallback=True)
示例#32
0
def _find(domain, localedirs, languages):
    """
    Replacement for gettext.find() to search in multiple directory. This
    function return tuples for each mo file found: (lang, translation).
    """
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    result = []
    for lang in nelangs:
        for localedir in localedirs:
            mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
            if os.path.exists(mofile):
                entry = (lang, mofile)
                result.append(entry)
    return result
示例#33
0
def install_gettext():
    """ Initialize gettext with the correct directory that contains
    MComix translations. This has to be done before any calls to gettext.gettext
    have been made to ensure all strings are actually translated. """

    # Add the sources' base directory to PATH to allow development without
    # explicitly installing the package.
    sys.path.append(constants.BASE_PATH)

    # Initialize default locale
    locale.setlocale(locale.LC_ALL, '')

    if preferences.prefs['language'] != 'auto':
        lang = preferences.prefs['language']
        lang_identifiers = [ lang ]
    else:
        # Get the user's current locale
        lang = portability.get_default_locale()
        lang_identifiers = gettext._expand_lang(lang)

    # Make sure GTK uses the correct language.
    os.environ['LANGUAGE'] = lang

    domain = constants.APPNAME.lower()
    translation = gettext.NullTranslations()

    # Search for .mo files manually, since gettext doesn't support setuptools/pkg_resources.
    for lang in lang_identifiers:
        resource = os.path.join(lang, 'LC_MESSAGES', '%s.mo' % domain)
        if pkg_resources.resource_exists('mcomix.messages', resource):
            translation = gettext.GNUTranslations(
                    pkg_resources.resource_stream('mcomix.messages', resource))
            break

    try:  # dirty-ish hack for py23
        translation.install(**{'unicode': True})  # in the weird way because of a 2to3 bug
    except TypeError:
        translation.install()

    global _translation
    _translation = translation
示例#34
0
def _find(domain, localedirs, languages):
    """
    Replacement for gettext.find() to search in multiple directory. This
    function return tuples for each mo file found: (lang, translation).
    """
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    result = []
    for lang in nelangs:
        for localedir in localedirs:
            mofile = os.path.join(localedir, lang, 'LC_MESSAGES',
                                  '%s.mo' % domain)
            if os.path.exists(mofile):
                entry = (lang, mofile)
                result.append(entry)
    return result
示例#35
0
def find(domain, localedir=None, languages=None, all=False):
    # Get some reasonable defaults for arguments that were not supplied
    if localedir is None:
        localedirs = _default_localedir, gettext._default_localedir
    else:
        localedirs = localedir,
    if languages is None:
        languages = []
        if _defaultlocalecode:
            languages.append(_defaultlocalecode)
        else:
            for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
                val = os.environ.get(envar)
                if val:
                    languages = val.split(':')
                    break
        if 'C' not in languages:
            languages.append('C')
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    if all:
        result = []
    else:
        result = None
    for dir in localedirs:
        for lang in nelangs:
            if lang == 'C':
                break
            mofile = os.path.join(dir, lang, 'LC_MESSAGES', domain + '.mo')
            if path_exists(mofile):
                if all:
                    result.append(mofile)
                else:
                    return mofile
    return result
示例#36
0
def setup_localisations(logger: logging.Logger):
    """Setup gettext localisations."""
    import gettext
    import locale
    # Get the 'en_US' style language code
    lang_code = locale.getdefaultlocale()[0]

    # Allow overriding through command line.
    if len(sys.argv) > 1:
        for arg in sys.argv[1:]:
            if arg.casefold().startswith('lang='):
                lang_code = arg[5:]
                break

    # Expands single code to parent categories.
    expanded_langs = gettext._expand_lang(lang_code)

    logger.info('Language: {!r}', lang_code)
    logger.debug('Language codes: {!r}', expanded_langs)

    for lang in expanded_langs:
        try:
            file = open('../i18n/{}.mo'.format(lang), 'rb')
        except FileNotFoundError:
            pass
        else:
            trans = gettext.GNUTranslations(file)
            break
    else:
        # No translations, fallback to English.
        # That's fine if the user's language is actually English.
        if 'en' not in expanded_langs:
            logger.warning(
                "Can't find translation for codes: {!r}!",
                expanded_langs,
            )
        trans = gettext.NullTranslations()
    # Add these functions to builtins, plus _=gettext
    trans.install(['gettext', 'ngettext'])
示例#37
0
def __find(domain, localedir='locale', languages=None, all_=False):
    """
    Return the name of a .mo file using the gettext strategy.

    :param domain: Gettext domain name (e.g. your module name)
    :param localedir: directory containing locales (give 'locale' if you have locale/fr_FR/LC_MESSAGES/domain.mo)
    :param languages: languages to find (if None: calculated with LANGUAGE, LC_ALL, LC_MESSAGES, LANG env. variables)
    :param all_: if True return a list of filenames corresponding to given languages, else return the first valid file.
    """
    # Get some reasonable defaults for arguments that were not supplied
    if languages is None:
        languages = []
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                break
        if 'C' not in languages:
            languages.append('C')
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        # noinspection PyProtectedMember
        for nelang in gettext_module._expand_lang(lang):  # pylint: disable=W0212
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    result = [] if all_ else None
    for lang in nelangs:
        if lang == 'C':
            break
        mofile = '%s/%s/%s/%s.mo' % (localedir, lang, 'LC_MESSAGES', domain)
        if pkg_resources.resource_exists('starterpyth', mofile):
            if all_:
                result.append(mofile)
            else:
                return mofile
    return result
示例#38
0
def __find(domain, localedir="locale", languages=None, all_=False):
    """
    Return the name of a .mo file using the gettext strategy.

    :param domain: Gettext domain name (e.g. your module name)
    :param localedir: directory containing locales (give 'locale' if you have locale/fr_FR/LC_MESSAGES/domain.mo)
    :param languages: languages to find (if None: calculated with LANGUAGE, LC_ALL, LC_MESSAGES, LANG env. variables)
    :param all_: if True return a list of filenames corresponding to given languages, else return the first valid file.
    """
    # Get some reasonable defaults for arguments that were not supplied
    if languages is None:
        languages = []
        for envar in ("LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG"):
            val = os.environ.get(envar)
            if val:
                languages = val.split(":")
                break
        if "C" not in languages:
            languages.append("C")
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        # noinspection PyProtectedMember
        for nelang in gettext_module._expand_lang(lang):  # pylint: disable=W0212
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    result = [] if all_ else None
    for lang in nelangs:
        if lang == "C":
            break
        mofile = "%s/%s/%s/%s.mo" % (localedir, lang, "LC_MESSAGES", domain)
        if pkg_resources.resource_exists("qtexample", mofile):
            if all_:
                result.append(mofile)
            else:
                return mofile
    return result
示例#39
0
def find(domain, localedir, languages, all=0):
    # now normalize and expand the languages
    nelangs = []
    languages = languages or []
    if not isinstance(languages, (tuple, list)):
        languages = [languages]
    for lang in languages:
        for nelang in gettext_module._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    if all:
        result = []
    else:
        result = None
    for dir in localedir:
        for lang in nelangs:
            mofile = os.path.join(dir, 'locale', lang, 'LC_MESSAGES', '%s.mo' % domain)
            if os.path.exists(mofile):
                if all:
                    result.append(mofile)
                else:
                    return mofile
    return result
示例#40
0
def __findFileMO(domain, localedir=None, languages=None, all=0):
    """Модифицированный метод, ищет файл перевода

    замена gettext.find"""
    if localedir is None:
        localedir = gettext._default_localedir
    if languages is None:
        languages = []
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                break
        if 'C' not in languages:
            languages.append('C')
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    if all:
        result = []
    else:
        result = None
    for lang in nelangs:
        if lang == 'C':
            break
        mofile = os.path.join(localedir, '%s_%s.mo' % (domain,lang))
        if os.path.exists(mofile):
            if all:
                result.append(mofile)
            else:
                return mofile
    return result
示例#41
0
 def update_event(self, inp=-1):
     self.set_output_val(0, gettext._expand_lang(self.input(0)))
示例#42
0
def gettext_find(
        domain,
        localedir=None,
        languages=None,
        all=False,  # noqa: C901
        path_exists=None,
        extension='mo'):
    """
    Locate a file using the `gettext` strategy.

    This is almost a straight copy of `gettext.find`
    """

    if path_exists is None:
        path_exists = os.path.exists

    # Get some reasonable defaults for arguments that were not supplied
    if localedir is None:
        localedir = gettext._default_localedir
    if languages is None:
        languages = []
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                break
        if 'C' not in languages:
            languages.append('C')
    # now normalize and expand the languages
    nelangs = []
    for lang in languages:
        for nelang in gettext._expand_lang(lang):
            if nelang not in nelangs:
                nelangs.append(nelang)
    # select a language
    if all:
        result = []
    else:
        result = None
    for lang in nelangs:
        if lang == 'C':
            break
        mofile = os.path.join(localedir, lang, 'LC_MESSAGES',
                              '%s.%s' % (domain, extension))
        mofile_lp = os.path.join("/usr/share/locale-langpack", lang,
                                 'LC_MESSAGES', '%s.%s' % (domain, extension))

        # first look into the standard locale dir, then into the
        # langpack locale dir

        # standard mo file
        try:
            if path_exists(mofile):
                if all:
                    result.append(mofile)
                else:
                    return mofile

            # langpack mofile -> use it
            if path_exists(mofile_lp):
                if all:
                    result.append(mofile_lp)
                else:
                    return mofile_lp

        except (NotImplementedError, ValueError):
            pass

    return result
示例#43
0
 def __getitem__(self, key):
     for k in gettext._expand_lang(key):
         if self.has_key(k):
             return dict.__getitem__(self, key)
     raise KeyError(key)
示例#44
0
文件: utils.py 项目: Hiden1/BEE2.4
def setup_localisations(logger: logging.Logger) -> None:
    """Setup gettext localisations."""
    from srctools.property_parser import PROP_FLAGS_DEFAULT
    import gettext
    import locale

    # Get the 'en_US' style language code
    lang_code = locale.getdefaultlocale()[0]

    # Allow overriding through command line.
    if len(sys.argv) > 1:
        for arg in sys.argv[1:]:
            if arg.casefold().startswith('lang='):
                lang_code = arg[5:]
                break

    # Expands single code to parent categories.
    expanded_langs = gettext._expand_lang(lang_code)

    logger.info('Language: {!r}', lang_code)
    logger.debug('Language codes: {!r}', expanded_langs)

    # Add these to Property's default flags, so config files can also
    # be localised.
    for lang in expanded_langs:
        PROP_FLAGS_DEFAULT['lang_' + lang] = True

    for lang in expanded_langs:
        try:
            file = open('../i18n/{}.mo'.format(lang), 'rb')
        except FileNotFoundError:
            continue
        with file:
            trans = gettext.GNUTranslations(file)
            break
    else:
        # No translations, fallback to English.
        # That's fine if the user's language is actually English.
        if 'en' not in expanded_langs:
            logger.warning(
                "Can't find translation for codes: {!r}!",
                expanded_langs,
            )
        trans = gettext.NullTranslations()
    # Add these functions to builtins, plus _=gettext
    trans.install(['gettext', 'ngettext'])

    # Some lang-specific overrides..

    if trans.gettext('__LANG_USE_SANS_SERIF__') == 'YES':
        # For Japanese/Chinese, we want a 'sans-serif' / gothic font
        # style.
        try:
            from tkinter import font
        except ImportError:
            return
        font_names = [
            'TkDefaultFont',
            'TkHeadingFont',
            'TkTooltipFont',
            'TkMenuFont',
            'TkTextFont',
            'TkCaptionFont',
            'TkSmallCaptionFont',
            'TkIconFont',
            # Note - not fixed-width...
        ]
        for font_name in font_names:
            font.nametofont(font_name).configure(family='sans-serif')
示例#45
0
文件: platform.py 项目: hitzjd/DHT
def smart_gettext_and_install(domain,
                              localedir,
                              languages,
                              fallback=False,
                              unicode=False):
    try:
        t = gettext.translation(domain,
                                localedir,
                                languages=languages,
                                fallback=fallback)
    except Exception, e:
        # if we failed to find the language, fetch it from the web async-style
        running_count = 0
        running_deferred = {}

        # Get some reasonable defaults for arguments that were not supplied
        if languages is None:
            languages = []
            for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
                val = os.environ.get(envar)
                if val:
                    languages = val.split(':')
                    break
            if 'C' not in languages:
                languages.append('C')

        # now normalize and expand the languages
        nelangs = []
        for lang in languages:
            for nelang in gettext._expand_lang(lang):
                if nelang not in nelangs:
                    nelangs.append(nelang)
        languages = nelangs

        for lang in languages:
            d = ThreadedDeferred(None, get_language, lang)

            def translate_and_install(r, td=d):
                running_deferred.pop(td)
                # only let the last one try to install
                if len(running_deferred) == 0:
                    t = gettext.translation(domain,
                                            localedir,
                                            languages=languages,
                                            fallback=True)
                    t.install(unicode)

            def failed(e, tlang=lang, td=d):
                if td in running_deferred:
                    running_deferred.pop(td)
                # don't raise an error, just continue untranslated
                sys.stderr.write(
                    'Could not find translation for language "%s"\n' % tlang)
                #traceback.print_exc(e)

            d.addCallback(translate_and_install)
            d.addErrback(failed)
            # accumulate all the deferreds first
            running_deferred[d] = 1

        # start them all, the last one finished will install the language
        for d in running_deferred:
            d.start()

        return
示例#46
0
文件: utils.py 项目: BenVlodgi/BEE2.4
def setup_localisations(logger: logging.Logger) -> None:
    """Setup gettext localisations."""
    from srctools.property_parser import PROP_FLAGS_DEFAULT
    import gettext
    import locale

    # Get the 'en_US' style language code
    lang_code = locale.getdefaultlocale()[0]

    # Allow overriding through command line.
    if len(sys.argv) > 1:
        for arg in sys.argv[1:]:
            if arg.casefold().startswith('lang='):
                lang_code = arg[5:]
                break

    # Expands single code to parent categories.
    expanded_langs = gettext._expand_lang(lang_code)

    logger.info('Language: {!r}', lang_code)
    logger.debug('Language codes: {!r}', expanded_langs)

    # Add these to Property's default flags, so config files can also
    # be localised.
    for lang in expanded_langs:
        PROP_FLAGS_DEFAULT['lang_' + lang] = True

    lang_folder = install_path('i18n')

    for lang in expanded_langs:
        try:
            file = open(lang_folder / (lang + '.mo').format(lang), 'rb')
        except FileNotFoundError:
            continue
        with file:
            trans = gettext.GNUTranslations(file)  # type: gettext.NullTranslations
            break
    else:
        # No translations, fallback to English.
        # That's fine if the user's language is actually English.
        if 'en' not in expanded_langs:
            logger.warning(
                "Can't find translation for codes: {!r}!",
                expanded_langs,
            )
        trans = gettext.NullTranslations()
    # Add these functions to builtins, plus _=gettext
    trans.install(['gettext', 'ngettext'])

    # Some lang-specific overrides..

    if trans.gettext('__LANG_USE_SANS_SERIF__') == 'YES':
        # For Japanese/Chinese, we want a 'sans-serif' / gothic font
        # style.
        try:
            from tkinter import font
        except ImportError:
            return
        font_names = [
            'TkDefaultFont',
            'TkHeadingFont',
            'TkTooltipFont',
            'TkMenuFont',
            'TkTextFont',
            'TkCaptionFont',
            'TkSmallCaptionFont',
            'TkIconFont',
            # Note - not fixed-width...
        ]
        for font_name in font_names:
            font.nametofont(font_name).configure(family='sans-serif')
示例#47
0
def setup(logger: logging.Logger) -> None:
    """Setup gettext localisations."""
    global _TRANSLATOR
    # Get the 'en_US' style language code
    lang_code = locale.getdefaultlocale()[0]

    # Allow overriding through command line.
    if len(sys.argv) > 1:
        for arg in sys.argv[1:]:
            if arg.casefold().startswith('lang='):
                lang_code = arg[5:]
                break

    # Expands single code to parent categories.
    expanded_langs = gettext_mod._expand_lang(lang_code)

    logger.info('Language: {!r}', lang_code)
    logger.debug('Language codes: {!r}', expanded_langs)

    # Add these to Property's default flags, so config files can also
    # be localised.
    for lang in expanded_langs:
        PROP_FLAGS_DEFAULT['lang_' + lang] = True

    lang_folder = utils.install_path('i18n')


    for lang in expanded_langs:
        try:
            file = open(lang_folder / (lang + '.mo').format(lang), 'rb')
        except FileNotFoundError:
            continue
        with file:
            _TRANSLATOR = gettext_mod.GNUTranslations(file)
            break
    else:
        # To help identify missing translations, replace everything with
        # something noticeable.
        if lang_code == 'dummy':
            _TRANSLATOR = DummyTranslations()
        # No translations, fallback to English.
        # That's fine if the user's language is actually English.
        else:
            if 'en' not in expanded_langs:
                logger.warning(
                    "Can't find translation for codes: {!r}!",
                    expanded_langs,
                )
            _TRANSLATOR = gettext_mod.NullTranslations()

    # Add these functions to builtins, plus _=gettext
    _TRANSLATOR.install(['gettext', 'ngettext'])

    # Override the global funcs, to more efficiently delegate if people import
    # later.
    globals()['gettext'] = _TRANSLATOR.gettext
    globals()['ngettext'] = _TRANSLATOR.ngettext

    # Some lang-specific overrides..

    if gettext('__LANG_USE_SANS_SERIF__') == 'YES':
        # For Japanese/Chinese, we want a 'sans-serif' / gothic font
        # style.
        try:
            from tkinter import font
        except ImportError:
            return
        font_names = [
            'TkDefaultFont',
            'TkHeadingFont',
            'TkTooltipFont',
            'TkMenuFont',
            'TkTextFont',
            'TkCaptionFont',
            'TkSmallCaptionFont',
            'TkIconFont',
            # Note - not fixed-width...
        ]
        for font_name in font_names:
            font.nametofont(font_name).configure(family='sans-serif')
示例#48
0
def setup_localisations(logger: logging.Logger) -> None:
    """Setup gettext localisations."""
    from srctools.property_parser import PROP_FLAGS_DEFAULT
    import gettext
    import locale

    # Get the 'en_US' style language code
    lang_code = locale.getdefaultlocale()[0]

    # Allow overriding through command line.
    if len(sys.argv) > 1:
        for arg in sys.argv[1:]:
            if arg.casefold().startswith('lang='):
                lang_code = arg[5:]
                break

    # Expands single code to parent categories.
    expanded_langs = gettext._expand_lang(lang_code)

    logger.info('Language: {!r}', lang_code)
    logger.debug('Language codes: {!r}', expanded_langs)

    # Add these to Property's default flags, so config files can also
    # be localised.
    for lang in expanded_langs:
        PROP_FLAGS_DEFAULT['lang_' + lang] = True

    lang_folder = install_path('i18n')

    trans: gettext.NullTranslations

    for lang in expanded_langs:
        try:
            file = open(lang_folder / (lang + '.mo').format(lang), 'rb')
        except FileNotFoundError:
            continue
        with file:
            trans = gettext.GNUTranslations(file)
            break
    else:
        # To help identify missing translations, replace everything with
        # something noticable.
        if lang_code == 'dummy':

            class DummyTranslations(gettext.NullTranslations):
                """Dummy form for identifying missing translation entries."""
                def gettext(self, message: str) -> str:
                    """Generate placeholder of the right size."""
                    # We don't want to leave {arr} intact.
                    return ''.join([
                        '#' if s.isalnum() or s in '{}' else s for s in message
                    ])

                def ngettext(self, msgid1: str, msgid2: str, n: int) -> str:
                    """Generate placeholder of the right size for plurals."""
                    return self.gettext(msgid1 if n == 1 else msgid2)

                lgettext = gettext
                lngettext = ngettext

            trans = DummyTranslations()
        # No translations, fallback to English.
        # That's fine if the user's language is actually English.
        else:
            if 'en' not in expanded_langs:
                logger.warning(
                    "Can't find translation for codes: {!r}!",
                    expanded_langs,
                )
            trans = gettext.NullTranslations()

    # Add these functions to builtins, plus _=gettext
    trans.install(['gettext', 'ngettext'])

    # Some lang-specific overrides..

    if trans.gettext('__LANG_USE_SANS_SERIF__') == 'YES':
        # For Japanese/Chinese, we want a 'sans-serif' / gothic font
        # style.
        try:
            from tkinter import font
        except ImportError:
            return
        font_names = [
            'TkDefaultFont',
            'TkHeadingFont',
            'TkTooltipFont',
            'TkMenuFont',
            'TkTextFont',
            'TkCaptionFont',
            'TkSmallCaptionFont',
            'TkIconFont',
            # Note - not fixed-width...
        ]
        for font_name in font_names:
            font.nametofont(font_name).configure(family='sans-serif')
示例#49
0
 def __getitem__(self, key):
     for k in gettext._expand_lang(key):
         if self.has_key(k):
             return dict.__getitem__(self, key)
     raise KeyError(key)