def init_localization():  # pragma: no cover
    """
    Application localization

    :return: gettext translator method
    :rtype: method
    """
    try:
        # Language message file
        lang_filename = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            "LC_MESSAGES/%s.mo" % settings.get_config('Alignak-app', 'locale'))
        logger.info("Opening message file %s for locale %s", lang_filename,
                    settings.get_config('Alignak-app', 'locale'))
        translation = GNUTranslations(open(lang_filename, "rb"))
        translation.install()
        _ = translation.gettext
    except IOError:
        logger.warning(
            "Locale not found. Using default language messages (English)")
        null_translation = NullTranslations()
        null_translation.install()
        _ = null_translation.gettext
    except Exception as e:  # pragma: no cover - should not happen
        logger.error("Locale not found. Exception: %s", str(e))
        null_translation = NullTranslations()
        null_translation.install()
        _ = null_translation.gettext

    return _
def     flask_before_i18n_set_user_language(lng=None):
    # Get current request language
    if lng is None:
        lng = request.headers.get('Accept-Language')
    if lng is not None:
        lng = lng[0:2]  # Todo: check priority : en-US,en;q=0.5
    else:
        lng = 'en'
    setattr(request, 'i18n_language', lng)

    # Init i18n
    try:
        setlocale(LC_ALL, (lng, 'utf-8'))
    except:
        setlocale(LC_ALL, '')

    # Build translation filename
    if lng not in settings.FLASK_TRANSLATION_LNG:
        lng = settings.FLASK_TRANSLATION_LNG[0]
    filename = ''
    filename = join(filename, join(join(settings.FLASK_TRANSLATION_DIR, lng), 'LC_MESSAGES/messages.mo'))

    # Open translation file or use NullTranslation if fail
    try:
        trans = GNUTranslations(open(filename, 'rb'))
    except IOError:
        trans = NullTranslations()

    # Install i18n
    trans.install()
    setattr(request, 'i18n_engine', trans)
 def reload(self):
     'See IMessageCatalog'
     fp = open(self._path_to_file, 'rb')
     try:
         self._catalog = GNUTranslations(fp)
     finally:
         fp.close()
Esempio n. 4
0
def init_localization(app):
    """prepare l10n"""

    # -----
    # Application localization
    # -----
    try:
        # Language message file
        lang_filename = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            "../locales/%s.mo" % app.config.get('locale', 'en_US')
        )
        print("Opening message file %s for locale %s"
              % (lang_filename, app.config.get('locale', 'en_US')))
        translation = GNUTranslations(open(lang_filename, "rb"))
        translation.install(True)
        _ = translation.gettext
    except IOError:
        print("Locale not found. Using default language messages (English)")
        null_translation = NullTranslations()
        null_translation.install()
        _ = null_translation.gettext
    except Exception as e:  # pragma: no cover - should not happen
        print("Locale not found. Exception: %s" % str(e))
        null_translation = NullTranslations()
        null_translation.install()
        _ = null_translation.gettext

    # Provide translation methods to templates
    app.config['_'] = _
    print(_("Language is English (default)..."))

    return _
Esempio n. 5
0
def process_dir(dirpath, filenames, projects):
    '''
    Process a directory
    '''
    translations = GNUTranslations(
        open(os.path.join(options.podir, options.lang + '.mo')))
    loader = TemplateLoader(['.'],
                            callback=lambda template: template.filters.insert(
                                0, Translator(translations.ugettext)))
    for fn in filenames:
        if fn.endswith('~') or fn.endswith('.swp'):
            continue
        src_file = os.path.join(dirpath, fn)
        dest_file = os.path.join(
            options.output,
            src_file[len(options.input):]) + '.' + options.lang  # Hideous
        curpage = src_file[len(options.input):].rstrip('.html')
        relpath = '../' * (dest_file.count('/') - 1)
        relpath = relpath.rstrip('/')
        if relpath == '': relpath = '.'
        if not os.path.exists(os.path.dirname(dest_file)):
            os.makedirs(os.path.dirname(dest_file))
        template = loader.load(src_file)
        # Variables made availble to all templates
        page = template.generate(
            _=lambda text: Markup(translations.ugettext(text)),
            lang=options.lang,
            path=options.basepath,
            relpath=relpath,
            curpage=curpage,
            projects=projects).render(method='html', doctype='html')
        output = open(dest_file, 'w')
        output.write(page)
        output.close()
class GettextMessageCatalog(object):
    """A message catalog based on GNU gettext and Python's gettext module."""

    implements(IGlobalMessageCatalog)

    def __init__(self, language, domain, path_to_file):
        """Initialize the message catalog"""
        self.language = language
        self.domain = domain
        self._path_to_file = path_to_file
        self.reload()
        self._catalog.add_fallback(_KeyErrorRaisingFallback())

    def reload(self):
        'See IMessageCatalog'
        fp = open(self._path_to_file, 'rb')
        try:
            self._catalog = GNUTranslations(fp)
        finally:
            fp.close()

    def getMessage(self, id):
        'See IMessageCatalog'
        return self._catalog.ugettext(id)

    def queryMessage(self, id, default=None):
        'See IMessageCatalog'
        try:
            return self._catalog.ugettext(id)
        except KeyError:
            return default

    def getIdentifier(self):
        'See IMessageCatalog'
        return self._path_to_file
class GettextMessageCatalog(object):
    """A message catalog based on GNU gettext and Python's gettext module."""

    implements(IGlobalMessageCatalog)

    def __init__(self, language, domain, path_to_file):
        """Initialize the message catalog"""
        self.language = language
        self.domain = domain
        self._path_to_file = path_to_file
        self.reload()
        self._catalog.add_fallback(_KeyErrorRaisingFallback())

    def reload(self):
        'See IMessageCatalog'
        fp = open(self._path_to_file, 'rb')
        try:
            self._catalog = GNUTranslations(fp)
        finally:
            fp.close()

    def getMessage(self, id):
        'See IMessageCatalog'
        return self._catalog.ugettext(id)

    def queryMessage(self, id, default=None):
        'See IMessageCatalog'
        try:
            return self._catalog.ugettext(id)
        except KeyError:
            return default

    def getIdentifier(self):
        'See IMessageCatalog'
        return self._path_to_file
def _catalog(self):
    'See IMessageCatalog'
    fp = open(self._path_to_file, 'rb')
    try:
        catalog = GNUTranslations(fp)
        catalog.add_fallback(_KeyErrorRaisingFallback())
        return catalog
    finally:
        fp.close()
Esempio n. 9
0
def set_translators():
    global _lang_trans, lcdata
    # To test different translations invoke as
    # CALIBRE_OVERRIDE_LANG=de_DE.utf8 program
    lang = get_lang()
    t = buf = iso639 = None

    if 'CALIBRE_TEST_TRANSLATION' in os.environ:
        buf = load_po(
            os.path.expanduser(os.environ['CALIBRE_TEST_TRANSLATION']))

    if lang:
        mpath = get_lc_messages_path(lang)
        if buf is None and mpath and os.access(mpath + '.po', os.R_OK):
            buf = load_po(mpath + '.po')

        if mpath is not None:
            from zipfile import ZipFile
            with ZipFile(
                    P('localization/locales.zip', allow_user_override=False),
                    'r') as zf:
                if buf is None:
                    buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
                if mpath == 'nds':
                    mpath = 'de'
                isof = mpath + '/iso639.mo'
                try:
                    iso639 = cStringIO.StringIO(zf.read(isof))
                except:
                    pass  # No iso639 translations for this lang
                if buf is not None:
                    from calibre.utils.serialize import msgpack_loads
                    try:
                        lcdata = msgpack_loads(
                            zf.read(mpath + '/lcdata.calibre_msgpack'))
                    except:
                        pass  # No lcdata

    if buf is not None:
        t = GNUTranslations(buf)
        if iso639 is not None:
            iso639 = _lang_trans = GNUTranslations(iso639)
            t.add_fallback(iso639)

    if t is None:
        t = NullTranslations()

    try:
        set_translators.lang = t.info().get('language')
    except Exception:
        pass
    t.install(unicode=True, names=('ngettext', ))
    # Now that we have installed a translator, we have to retranslate the help
    # for the global prefs object as it was instantiated in get_lang(), before
    # the translator was installed.
    from calibre.utils.config_base import prefs
    prefs.retranslate_help()
Esempio n. 10
0
def set_translators():
    global _lang_trans, lcdata
    # To test different translations invoke as
    # CALIBRE_OVERRIDE_LANG=de_DE.utf8 program
    lang = get_lang()
    t = buf = iso639 = None

    if 'CALIBRE_TEST_TRANSLATION' in os.environ:
        buf = load_po(os.path.expanduser(os.environ['CALIBRE_TEST_TRANSLATION']))

    if lang:
        mpath = get_lc_messages_path(lang)
        if buf is None and mpath and os.access(mpath + '.po', os.R_OK):
            buf = load_po(mpath + '.po')

        if mpath is not None:
            from zipfile import ZipFile
            with ZipFile(P('localization/locales.zip',
                allow_user_override=False), 'r') as zf:
                if buf is None:
                    buf = io.BytesIO(zf.read(mpath + '/messages.mo'))
                if mpath == 'nds':
                    mpath = 'de'
                isof = mpath + '/iso639.mo'
                try:
                    iso639 = io.BytesIO(zf.read(isof))
                except:
                    pass  # No iso639 translations for this lang
                if buf is not None:
                    from calibre.utils.serialize import msgpack_loads
                    try:
                        lcdata = msgpack_loads(zf.read(mpath + '/lcdata.calibre_msgpack'))
                    except:
                        pass  # No lcdata

    if buf is not None:
        t = GNUTranslations(buf)
        if iso639 is not None:
            iso639 = _lang_trans = GNUTranslations(iso639)
            t.add_fallback(iso639)

    if t is None:
        t = NullTranslations()

    try:
        set_translators.lang = t.info().get('language')
    except Exception:
        pass
    if is_py3:
        t.install(names=('ngettext',))
    else:
        t.install(unicode=True, names=('ngettext',))
    # Now that we have installed a translator, we have to retranslate the help
    # for the global prefs object as it was instantiated in get_lang(), before
    # the translator was installed.
    from calibre.utils.config_base import prefs
    prefs.retranslate_help()
Esempio n. 11
0
def set_translators():
    global _lang_trans, lcdata
    # To test different translations invoke as
    # CALIBRE_OVERRIDE_LANG=de_DE.utf8 program
    lang = get_lang()
    t = None

    if lang:
        buf = iso639 = None
        mpath = get_lc_messages_path(lang)
        if mpath and os.access(mpath + '.po', os.R_OK):
            from calibre.translations.msgfmt import make
            buf = cStringIO.StringIO()
            try:
                make(mpath + '.po', buf)
            except:
                print(('Failed to compile translations file: %s,'
                       ' ignoring') % (mpath + '.po'))
                buf = None
            else:
                buf = cStringIO.StringIO(buf.getvalue())

        if mpath is not None:
            from zipfile import ZipFile
            with ZipFile(
                    P('localization/locales.zip', allow_user_override=False),
                    'r') as zf:
                if buf is None:
                    buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
                if mpath == 'nds':
                    mpath = 'de'
                isof = mpath + '/iso639.mo'
                try:
                    iso639 = cStringIO.StringIO(zf.read(isof))
                except:
                    pass  # No iso639 translations for this lang
                if buf is not None:
                    try:
                        lcdata = cPickle.loads(
                            zf.read(mpath + '/lcdata.pickle'))
                    except:
                        pass  # No lcdata

        if buf is not None:
            t = GNUTranslations(buf)
            if iso639 is not None:
                iso639 = _lang_trans = GNUTranslations(iso639)
                t.add_fallback(iso639)

    if t is None:
        t = NullTranslations()

    t.install(unicode=True, names=('ngettext', ))
    # Now that we have installed a translator, we have to retranslate the help
    # for the global prefs object as it was instantiated in get_lang(), before
    # the translator was installed.
    from calibre.utils.config_base import prefs
    prefs.retranslate_help()
 def reload(self):
     'See IMessageCatalog'
     fp = open(self._path_to_file, 'rb')
     try:
         self._catalog = GNUTranslations(fp)
     finally:
         fp.close()
Esempio n. 13
0
def mo_to_no(mofile):
    translations = GNUTranslations(mofile)

    info = translations._info
    catalog = translations._catalog

    numplurals_m = re.match(r"\s*nplurals\s*=\s*(\d+)", info['plural-forms'])
    if not numplurals_m or not numplurals_m:
        fn = "<unknown file>"
        if hasattr(mofile, 'name'):
            fn = "file " + mofile.name
        raise Error(
            "No plural-forms or nplurals in message catalog info in %s" % fn)

    numplurals = int(numplurals_m.group(1))
    info['nplurals'] = numplurals

    newcatalog = {}
    for msgid, trans in catalog.iteritems():
        if isinstance(msgid, tuple):
            forms = newcatalog.setdefault(msgid[0], [None] * numplurals)
            try:
                forms[msgid[1]] = trans
            except:
                print "Broken translation: ", msgid
        else:
            newcatalog[msgid] = trans

    return '{ "info": %s, "catalog": %s }\n' % (json.dumps(
        info, ensure_ascii=False,
        indent=0), json.dumps(newcatalog, ensure_ascii=False, indent=0))
Esempio n. 14
0
def load_translations(namespace, zfp):
    null = object()
    trans = _translations_cache.get(zfp, null)
    if trans is None:
        return
    if trans is null:
        from calibre.utils.localization import get_lang
        lang = get_lang()
        if not lang or lang == 'en':  # performance optimization
            _translations_cache[zfp] = None
            return
        with zipfile.ZipFile(zfp) as zf:
            try:
                mo = zf.read('translations/%s.mo' % lang)
            except KeyError:
                mo = None  # No translations for this language present
        if mo is None:
            _translations_cache[zfp] = None
            return
        from gettext import GNUTranslations
        from io import BytesIO
        trans = _translations_cache[zfp] = GNUTranslations(BytesIO(mo))

    namespace['_'] = trans.ugettext
    namespace['ngettext'] = trans.ungettext
Esempio n. 15
0
 def read_translations(self):
     for filename in [x for x in os.listdir(self.i18ndir) if x.endswith('.mo')]:
         lang = filename[:-3]
         filename = os.path.join(self.i18ndir, filename)
         f = open(filename, 'rb')
         self.translations[lang] = GNUTranslations(f)
         f.close()
     self.languages = self.translations.keys()
Esempio n. 16
0
def translation(lang):
    try:
        with ZipFile(PACKAGE_PATH) as f:
            mo = f.read('tm/mos/{0}.mo'.format(lang))
    except KeyError:
        return NullTranslations()
    else:
        return GNUTranslations(BytesIO(mo))
Esempio n. 17
0
def get_all_translators():
    from zipfile import ZipFile
    with ZipFile(P('localization/locales.zip', allow_user_override=False), 'r') as zf:
        for lang in available_translations():
            mpath = get_lc_messages_path(lang)
            if mpath is not None:
                buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
                yield lang, GNUTranslations(buf)
class GettextMessageCatalog(object):
    """A message catalog based on GNU gettext and Python's gettext module."""

    _catalog = None

    def __init__(self, language, domain, path_to_file):
        """Initialize the message catalog"""
        self.language = (
            language.decode('utf-8') if isinstance(language, bytes)
            else language)
        self.domain = (
            domain.decode("utf-8") if isinstance(domain, bytes)
            else domain)
        self._path_to_file = path_to_file
        self.reload()
        catalog = self._catalog
        catalog.add_fallback(_KeyErrorRaisingFallback())
        self._gettext = (
            catalog.gettext if str is not bytes else catalog.ugettext)
        self._ngettext = (
            catalog.ngettext if str is not bytes else catalog.ungettext)

    def reload(self):
        'See IMessageCatalog'
        with open(self._path_to_file, 'rb') as fp:
            self._catalog = GNUTranslations(fp)

    def getMessage(self, id):
        'See IMessageCatalog'
        return self._gettext(id)

    @plural_formatting
    def getPluralMessage(self, singular, plural, n):
        'See IMessageCatalog'
        return self._ngettext(singular, plural, n)

    @plural_formatting
    def queryPluralMessage(self, singular, plural, n, dft1=None, dft2=None):
        'See IMessageCatalog'
        try:
            return self._ngettext(singular, plural, n)
        except KeyError:
            # Here, we use the catalog plural function to determine
            # if `n` triggers a plural form or not.
            if self._catalog.plural(n):
                return dft2
            return dft1

    def queryMessage(self, id, default=None):
        'See IMessageCatalog'
        try:
            return self._gettext(id)
        except KeyError:
            return default

    def getIdentifier(self):
        'See IMessageCatalog'
        return self._path_to_file
Esempio n. 19
0
class GettextMessageCatalog(object):
    """A message catalog based on GNU gettext and Python's gettext module."""

    _catalog = None

    def __init__(self, language, domain, path_to_file):
        """Initialize the message catalog"""
        self.language = (language.decode('utf-8') if isinstance(
            language, bytes) else language)
        self.domain = (domain.decode("utf-8")
                       if isinstance(domain, bytes) else domain)
        self._path_to_file = path_to_file
        self.reload()
        catalog = self._catalog
        catalog.add_fallback(_KeyErrorRaisingFallback())
        self._gettext = (catalog.gettext
                         if str is not bytes else catalog.ugettext)
        self._ngettext = (catalog.ngettext
                          if str is not bytes else catalog.ungettext)

    def reload(self):
        'See IMessageCatalog'
        with open(self._path_to_file, 'rb') as fp:
            self._catalog = GNUTranslations(fp)

    def getMessage(self, id):
        'See IMessageCatalog'
        return self._gettext(id)

    @plural_formatting
    def getPluralMessage(self, singular, plural, n):
        'See IMessageCatalog'
        return self._ngettext(singular, plural, n)

    @plural_formatting
    def queryPluralMessage(self, singular, plural, n, dft1=None, dft2=None):
        'See IMessageCatalog'
        try:
            return self._ngettext(singular, plural, n)
        except KeyError:
            # Here, we use the catalog plural function to determine
            # if `n` triggers a plural form or not.
            if self._catalog.plural(n):
                return dft2
            return dft1

    def queryMessage(self, id, default=None):
        'See IMessageCatalog'
        try:
            return self._gettext(id)
        except KeyError:
            return default

    def getIdentifier(self):
        'See IMessageCatalog'
        return self._path_to_file
Esempio n. 20
0
def translate(lang, text):
    trans = None
    if lang in _CACHE:
        trans = _CACHE[lang]
    else:
        mpath = get_lc_messages_path(lang)
        if mpath is not None:
            with ZipFile(P('localization/locales.zip',
                allow_user_override=False), 'r') as zf:
                try:
                    buf = io.BytesIO(zf.read(mpath + '/messages.mo'))
                except Exception:
                    pass
                else:
                    trans = GNUTranslations(buf)
                    _CACHE[lang] = trans
    if trans is None:
        return getattr(__builtins__, '_', lambda x: x)(text)
    return trans.gettext(text)
Esempio n. 21
0
def translate(lang, text):
    trans = None
    if lang in _CACHE:
        trans = _CACHE[lang]
    else:
        mpath = get_lc_messages_path(lang)
        if mpath is not None:
            with ZipFile(P('localization/locales.zip',
                allow_user_override=False), 'r') as zf:
                try:
                    buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
                except:
                    pass
                else:
                    trans = GNUTranslations(buf)
                    _CACHE[lang] = trans
    if trans is None:
        return getattr(__builtins__, '_', lambda x: x)(text)
    return trans.ugettext(text)
Esempio n. 22
0
class MOFile(File):

    class_mimetypes = ['application/x-gettext-translation']
    class_extension = 'mo'

    def _load_state_from_file(self, file):
        self.translations = GNUTranslations(file)

    def gettext(self, message):
        """Returns the translation for the given message.
        """
        return self.translations.ugettext(message)
Esempio n. 23
0
class MOFile(File):

    class_mimetypes = ['application/x-gettext-translation']
    class_extension = 'mo'

    def _load_state_from_file(self, file):
        self.translations = GNUTranslations(file)


    def gettext(self, message):
        """Returns the translation for the given message.
        """
        return self.translations.ugettext(message)
Esempio n. 24
0
def init_localization(language):
    """prepare l10n"""

    try:
        # Language message file
        filename = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                "../res/%s.mo" % language)
        logger.info("Opening message file %s for locale %s", filename,
                    language)
        translation = GNUTranslations(open(filename, "rb"))
        translation.install()
    except IOError:
        logger.warning("Locale not found. Using default messages")
        logger.debug("Backtrace: %s", traceback.format_exc())
        default = NullTranslations()
        default.install()
    except Exception as e:
        logger.error("Locale not found. Exception: %s", str(e))
        logger.debug("Backtrace: %s", traceback.format_exc())
        default = NullTranslations()
        default.install()
    logger.info(_("Language is English (default)..."))
Esempio n. 25
0
def set_translators():
    global _lang_trans, lcdata
    # To test different translations invoke as
    # CALIBRE_OVERRIDE_LANG=de_DE.utf8 program
    lang = get_lang()
    t = None

    if lang:
        buf = iso639 = None
        mpath = get_lc_messages_path(lang)
        if mpath and os.access(mpath + '.po', os.R_OK):
            from calibre.translations.msgfmt import make
            buf = cStringIO.StringIO()
            try:
                make(mpath + '.po', buf)
            except:
                print(('Failed to compile translations file: %s,'
                       ' ignoring') % (mpath + '.po'))
                buf = None
            else:
                buf = cStringIO.StringIO(buf.getvalue())

        if mpath is not None:
            from zipfile import ZipFile
            with ZipFile(
                    P('localization/locales.zip', allow_user_override=False),
                    'r') as zf:
                if buf is None:
                    buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
                if mpath == 'nds':
                    mpath = 'de'
                isof = mpath + '/iso639.mo'
                try:
                    iso639 = cStringIO.StringIO(zf.read(isof))
                except:
                    pass  # No iso639 translations for this lang
                if buf is not None:
                    try:
                        lcdata = cPickle.loads(
                            zf.read(mpath + '/lcdata.pickle'))
                    except:
                        pass  # No lcdata

        if buf is not None:
            t = GNUTranslations(buf)
            if iso639 is not None:
                iso639 = _lang_trans = GNUTranslations(iso639)
                t.add_fallback(iso639)

    if t is None:
        t = NullTranslations()

    t.install(unicode=True, names=('ngettext', ))
    # Now that we have installed a translator, we have to retranslate the help
    # for the global prefs object as it was instantiated in get_lang(), before
    # the translator was installed.
    from calibre.utils.config_base import prefs
    prefs.retranslate_help()
Esempio n. 26
0
def set_translators():
    # To test different translations invoke as
    # CALIBRE_OVERRIDE_LANG=de_DE.utf8 program
    lang = get_lang()
    t = None

    if lang:
        buf = iso639 = None
        mpath = get_lc_messages_path(lang)
        if mpath and os.access(mpath + '.po', os.R_OK):
            from calibre.translations.msgfmt import make
            buf = cStringIO.StringIO()
            try:
                make(mpath + '.po', buf)
            except:
                print(('Failed to compile translations file: %s,'
                       ' ignoring') % (mpath + '.po'))
                buf = None
            else:
                buf = cStringIO.StringIO(buf.getvalue())

        if mpath is not None:
            from zipfile import ZipFile
            with ZipFile(
                    P('localization/locales.zip', allow_user_override=False),
                    'r') as zf:
                if buf is None:
                    buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
                if mpath == 'nds':
                    mpath = 'de'
                isof = mpath + '/iso639.mo'
                try:
                    iso639 = cStringIO.StringIO(zf.read(isof))
                except:
                    pass  # No iso639 translations for this lang

        if buf is not None:
            t = GNUTranslations(buf)
            if iso639 is not None:
                iso639 = GNUTranslations(iso639)
                t.add_fallback(iso639)

    if t is None:
        t = NullTranslations()

    t.install(unicode=True, names=('ngettext', ))
Esempio n. 27
0
def get_single_translator(mpath, which='messages'):
    from zipfile import ZipFile
    with ZipFile(P('localization/locales.zip', allow_user_override=False), 'r') as zf:
        path = '{}/{}.mo'.format(mpath, which)
        data = zf.read(path)
        buf = io.BytesIO(data)
        try:
            return GNUTranslations(buf)
        except Exception as e:
            import traceback
            traceback.print_exc()
            import hashlib
            sig = hashlib.sha1(data).hexdigest()
            raise ValueError('Failed to load translations for: {} (size: {} and signature: {}) with error: {}'.format(
                path, len(data), sig, e))
Esempio n. 28
0
def load_calibre_translations():
    from tornado import locale
    tmp = tempfile.mkdtemp()
    with zipfile.ZipFile(P('localization/locales.zip')) as zf:
        trans = {}
        for name in zf.namelist():
            if name.endswith(".mo"):
                trans[name.split("/")[0]] = name
        for code, zpath in trans.items():
            try:
                buf = cStringIO.StringIO(zf.read(zpath))
                locale._translations[code] = GNUTranslations(buf)
            except:
                pass
        locale._use_gettext = True
        locale.set_default_locale("zh_CN")
Esempio n. 29
0
    def _doPrepareTranslations(self):
        """Generate the translation object from a po file
        """
        self._updateFromFS()
        tro = None
        if getattr(self, '_v_tro', None) is None:
            self._v_tro = tro = translationRegistry.get(self.getId(), None)
        if tro is None:
            moFile = self._getMoFile()
            tro = GNUTranslations(moFile)
            if not self._language:
                self._language = (
                    tro._info.get('language-code', None)  # new way
                    or tro._info.get('language', None))  # old way
            if not self._domain:
                self._domain = tro._info.get('domain', None)
            if self._language is None or self._domain is None:
                raise ValueError, 'potfile %s has no metadata, PTS needs a language and a message domain!' % os.path.join(
                    *self._pofile)
            self._language = self._language.lower().replace('_', '-')
            self._other_languages = tro._info.get('x-is-fallback-for',
                                                  '').split()
            self.preferred_encodings = tro._info.get('preferred-encodings',
                                                     '').split()
            self.name = unicode(tro._info.get('language-name', ''),
                                tro._charset)
            self.default_zope_data_encoding = tro._charset

            translationRegistry[self.getId()] = self._v_tro = tro

            # right to left support
            is_rtl = tro._info.get('x-is-rtl', 'no').strip().lower()
            if is_rtl in ('yes', 'y', 'true', '1'):
                self._is_rtl = True
            elif is_rtl in ('no', 'n', 'false', '0'):
                self._is_rtl = False
            else:
                raise ValueError, 'Unsupported value for X-Is-RTL' % is_rtl
            rtlRegistry[self.getId()] = self.isRTL()

            if self.name:
                self.title = '%s language (%s) for %s' % (
                    self._language, self.name, self._domain)
            else:
                self.title = '%s language for %s' % (self._language,
                                                     self._domain)
    def test_mo_file_parsing(self, mo_file_path):
        """
        Check `.mo` files against bad data and plural forms.

        This issue should _not_ happen when running `$ make translations_pull`.

        In case it happened, it can be fixed manually by:
         - Remove the line
           "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
           from the offending file
         - Recompile:
           $ cd translations/en/LC_MESSAGES/
           $ msgfmt text.po -o text.mo
        """
        with open(mo_file_path, 'rb') as mo_file_fp:
            # Fails if there's a bad plural form in `.mo` file
            GNUTranslations(mo_file_fp)
Esempio n. 31
0
def translator_for_lang(lang):
    t = buf = iso639 = lcdata = None
    if 'CALIBRE_TEST_TRANSLATION' in os.environ:
        buf = load_po(
            os.path.expanduser(os.environ['CALIBRE_TEST_TRANSLATION']))

    mpath = get_lc_messages_path(lang)
    if buf is None and mpath and os.access(mpath + '.po', os.R_OK):
        buf = load_po(mpath + '.po')

    if mpath is not None:
        from zipfile import ZipFile
        with ZipFile(P('localization/locales.zip', allow_user_override=False),
                     'r') as zf:
            if buf is None:
                buf = io.BytesIO(zf.read(mpath + '/messages.mo'))
            if mpath == 'nds':
                mpath = 'de'
            isof = mpath + '/iso639.mo'
            try:
                iso639 = io.BytesIO(zf.read(isof))
            except:
                pass  # No iso639 translations for this lang
            if buf is not None:
                from calibre.utils.serialize import msgpack_loads
                try:
                    lcdata = msgpack_loads(
                        zf.read(mpath + '/lcdata.calibre_msgpack'))
                except:
                    pass  # No lcdata

    if buf is not None:
        try:
            t = GNUTranslations(buf)
        except Exception:
            import traceback
            traceback.print_exc()
            t = None
        if iso639 is not None:
            try:
                iso639 = GNUTranslations(iso639)
            except Exception:
                iso639 = None
            else:
                if t is not None:
                    t.add_fallback(iso639)

    if t is None:
        t = NullTranslations()

    return {'translator': t, 'iso639_translator': iso639, 'lcdata': lcdata}
Esempio n. 32
0
def set_language(language):
    global translations

    try:
        with open(
                pkg_resources.resource_filename(
                    "middlewared",
                    f"locale/{language}/LC_MESSAGES/middlewared.mo"),
                "rb") as f:
            translations = GNUTranslations(f)

        return True
    except Exception as e:
        if language != "en":
            logger.warning("Failed to set language %r: %r", language, e)

        translations = NullTranslations()

        return False
Esempio n. 33
0
def set_translators():
    global _lang_trans
    # To test different translations invoke as
    # CALIBRE_OVERRIDE_LANG=de_DE.utf8 program
    lang = get_lang()
    t = None

    if lang:
        buf = iso639 = None
        mpath = get_lc_messages_path(lang)
        if mpath and os.access(mpath+'.po', os.R_OK):
            from calibre.translations.msgfmt import make
            buf = cStringIO.StringIO()
            try:
                make(mpath+'.po', buf)
            except:
                print (('Failed to compile translations file: %s,'
                        ' ignoring')%(mpath+'.po'))
                buf = None
            else:
                buf = cStringIO.StringIO(buf.getvalue())

        if mpath is not None:
            from zipfile import ZipFile
            with ZipFile(P('localization/locales.zip',
                allow_user_override=False), 'r') as zf:
                if buf is None:
                    buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
                if mpath == 'nds':
                    mpath = 'de'
                isof = mpath + '/iso639.mo'
                try:
                    iso639 = cStringIO.StringIO(zf.read(isof))
                except:
                    pass  # No iso639 translations for this lang

        if buf is not None:
            t = GNUTranslations(buf)
            if iso639 is not None:
                iso639 = _lang_trans = GNUTranslations(iso639)
                t.add_fallback(iso639)

    if t is None:
        t = NullTranslations()

    t.install(unicode=True, names=('ngettext',))
 def reload(self):
     'See IMessageCatalog'
     with open(self._path_to_file, 'rb') as fp:
         self._catalog = GNUTranslations(fp)
 def reload(self):
     'See IMessageCatalog'
     with open(self._path_to_file, 'rb') as fp:
         self._catalog = GNUTranslations(fp)
Esempio n. 36
0
 def _load_state_from_file(self, file):
     self.translations = GNUTranslations(file)
Esempio n. 37
0
def get_translator(lang=None, request=None):
    """
    return a GNUTranslations instance for `lang`::

        >>> translate = get_translator('fr')
        >>> assert translate('Remove') == 'Supprimer'
        >>> assert translate('month_01') == 'Janvier'
        >>> translate = get_translator('en')
        >>> assert translate('Remove') == 'Remove'
        >>> assert translate('month_01') == 'January'

    The correct gettext method is stored in request if possible::

        >>> from webob import Request
        >>> req = Request.blank('/')
        >>> translate = get_translator('fr', request=req)
        >>> assert translate('Remove') == 'Supprimer'
        >>> translate = get_translator('en', request=req)
        >>> assert translate('Remove') == 'Supprimer'

    """
    if request is not None:
        translate = request.environ.get('fa.translate')
        if translate:
            return translate

        if HAS_PYRAMID:
            translate = get_localizer(request).translate
            request.environ['fa.translate'] = translate
            return translate

    # get possible fallback languages
    try:
        langs = get_lang() or []
    except TypeError:
        # this occurs when Pylons is available and we are not in a valid thread
        langs = []

    # insert lang if provided
    if lang and lang not in langs:
        langs.insert(0, lang)

    if not langs:
        langs = ['en']

    # get the first available catalog
    for lang in langs:
        filename = os.path.join(i18n_path, lang, 'LC_MESSAGES',
                                'formalchemy.mo')
        if os.path.isfile(filename):
            translations_path = os.path.join(i18n_path, lang, 'LC_MESSAGES',
                                             'formalchemy.mo')
            tr = GNUTranslations(open(translations_path, 'rb')).gettext

            def translate(value):
                value = tr(value)
                if isinstance(value, six.binary_type):
                    value = six.text_type(value, 'utf-8')
                return value

            if request is not None:
                request.environ['fa.translate'] = translate
            return translate

    # dummy translator
    if request is not None:
        request.environ['fa.translate'] = _translator.gettext
    return _translator.gettext
Esempio n. 38
0
 def __call__(self, language_code=None):
     if language_code in ('en', None):
         return NullTranslations()
     mo_file = 'mailman-%s.mo' % language_code
     with closing(resource_stream('mailman.testing', mo_file)) as fp:
         return GNUTranslations(fp)
Esempio n. 39
0
def set_app_config(config):
    """
    Update global application configuration
    """
    global bottle_app, app_config, _

    # Localization
    try:
        # Language message file
        filename = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                "res/%s.mo" % config.get('locale', 'en_US'))
        print("Opening message file %s for locale %s" %
              (filename, config.get('locale', 'en_US')))
        translation = GNUTranslations(open(filename, "rb"))
        translation.install()
        _ = translation.gettext
    except IOError:
        print("Locale not found. Using default language messages (English)")
        default = NullTranslations()
        default.install()
        _ = default.gettext
    except Exception as e:  # pragma: no cover - should not happen
        print("Locale not found. Exception: %s" % str(e))
        default = NullTranslations()
        default.install()
        _ = default.gettext
    print(_("Language is English (default)..."))

    app_config = config
    bottle_app.config.update(config)

    # Set logging options for the application
    set_console_logger(logger)

    # Store logs in a daily file, keeping 6 days along ... as default!
    set_file_logger(logger,
                    path=app_config.get('logs.dir', '/var/log/' +
                                        manifest['name'].lower()),
                    filename=app_config.get('logs.filename',
                                            manifest['name'].lower() + '.log'),
                    when=app_config.get('logs.when', 'D'),
                    interval=int(app_config.get('logs.interval', '1')),
                    backup_count=int(app_config.get('logs.backupCount', '6')))

    # Set application log level (default is INFO (20))
    print("Activate logs level: %d" % int(app_config.get('logs.level', '20')))
    logger.setLevel(int(app_config.get('logs.level', '20')))
    if app_config.get('debug',
                      '0') == '1':  # pragma: no cover - not testable easily...
        print("Activate DEBUG logs")
        logger.setLevel(DEBUG)

    logger.info(
        "--------------------------------------------------------------------------------"
    )
    logger.info("%s, version %s", manifest['name'], manifest['version'])
    logger.info("Copyright %s", manifest['copyright'])
    logger.info("License: %s", manifest['license'])
    logger.info(
        "--------------------------------------------------------------------------------"
    )
    logger.debug("Doc: %s", manifest['doc'])
    logger.debug("Release notes: %s", manifest['release'])
    logger.debug(
        "--------------------------------------------------------------------------------"
    )

    logger.info(
        "--------------------------------------------------------------------------------"
    )
    logger.info("%s, listening on %s:%d (debug mode: %s)",
                app_config.get('name', 'Test'),
                app_config.get('host', '127.0.0.1'),
                int(app_config.get('port', '5001')),
                app_config.get('debug', '0') == '1')
    logger.info("%s, using alignak backend on %s",
                app_config.get('name', 'Test'),
                app_config.get('alignak_backend', 'http://127.0.0.1:5000'))
    logger.info(
        "--------------------------------------------------------------------------------"
    )

    logger.debug("Application settings: ")
    for key, value in sorted(app_config.items()):
        logger.debug(" %s = %s", key, value)
    logger.debug(
        "--------------------------------------------------------------------------------"
    )
Esempio n. 40
0
def set_app_config(config):
    '''
    Update global application configuration
    '''
    global bottle_app, app_config, _

    # Localization
    try:
        # Language message file
        filename = os.path.join(
            os.path.abspath(os.path.dirname(__file__)), "res/%s.mo" % config.get('locale', 'en_US')
        )
        print "Opening message file %s for locale %s" % (filename, config.get('locale', 'en_US'))
        translation = GNUTranslations(open(filename, "rb"))
        translation.install()
        _ = translation.gettext
    except IOError:
        print "Locale not found. Using default language messages (English)"
        default = NullTranslations()
        default.install()
        _ = default.gettext
    except Exception as e:  # pragma: no cover - should not happen
        print "Locale not found. Exception: %s" % str(e)
        default = NullTranslations()
        default.install()
        _ = default.gettext
    print _("Language is English (default)...")

    app_config = config
    bottle_app.config.update(config)

    # Set logging options for the application
    set_console_logger(logger)

    # Store logs in a daily file, keeping 6 days along ... as default!
    set_file_logger(
        logger,
        path=app_config.get('logs.dir', '/var/log'),
        filename=app_config.get('logs.filename', manifest['name'].lower() + '.log'),
        when=app_config.get('logs.when', 'D'),
        interval=int(app_config.get('logs.interval', '1')),
        backupCount=int(app_config.get('logs.backupCount', '6'))
    )

    # Set application log level (default is INFO (20))
    print "Activate logs level: %d" % int(app_config.get('logs.level', '20'))
    logger.setLevel(int(app_config.get('logs.level', '20')))
    if app_config.get('debug', '0') == '1':
        print "Activate DEBUG logs"
        logger.setLevel(DEBUG)

    logger.info(
        "--------------------------------------------------------------------------------"
    )
    logger.info("%s, version %s", manifest['name'], manifest['version'])
    logger.info("Copyright %s", manifest['copyright'])
    logger.info("License: %s", manifest['license'])
    logger.info(
        "--------------------------------------------------------------------------------"
    )
    logger.debug("Doc: %s", manifest['doc'])
    logger.debug("Release notes: %s", manifest['release'])
    logger.debug(
        "--------------------------------------------------------------------------------"
    )

    logger.debug("Application settings: ")
    for key, value in sorted(app_config.items()):
        logger.debug(" %s = %s", key, value)
    logger.debug(
        "--------------------------------------------------------------------------------"
    )
Esempio n. 41
0
Because of this, gettext will be initialized here,
to search for a .mo file for the users locale inside the zip
"""
if __name__ == '__main__':

    locale.setlocale(locale.LC_ALL, '')
    loc, enc = locale.getlocale(locale.LC_MESSAGES)
    l10n_resource = None

    # try to find the corresponding gettext file (*.mo) for the users locale in the zip file
    if loc is not None and loc != 'C':
        try:
            l10n_resource = pkgutil.get_data(
                'luckyLUKS', 'locale/{0}/LC_MESSAGES/luckyLUKS.mo'.format(loc))
        except IOError:
            if '_' in loc:
                try:
                    l10n_resource = pkgutil.get_data(
                        'luckyLUKS',
                        'locale/{0}/LC_MESSAGES/luckyLUKS.mo'.format(
                            loc.split('_')[0]))
                except IOError:
                    pass

    if l10n_resource is None:
        translation = NullTranslations()
    else:
        translation = GNUTranslations(io.BytesIO(l10n_resource))

    main.luckyLUKS(translation)
Esempio n. 42
0
 def _load_state_from_file(self, file):
     self.translations = GNUTranslations(file)
Esempio n. 43
0
def get_single_translator(mpath):
    from zipfile import ZipFile
    with ZipFile(P('localization/locales.zip', allow_user_override=False),
                 'r') as zf:
        buf = io.StringIO(zf.read(mpath + '/messages.mo'))
        return GNUTranslations(buf)
Esempio n. 44
0
def process_dir(dirpath, filenames):
    '''
    Process a directory
    '''
    if options.podir and options.lang:
        translations = GNUTranslations(open(os.path.join(options.podir, options.lang + '.mo')))
        if int(get_distribution('genshi').version[2]) < 6:
            loader = TemplateLoader(['.'], callback=lambda template: template.filters.insert(0, Translator(translations.ugettext)))
        else:
            loader = TemplateLoader(['.'], callback=lambda template: template.filters.insert(0, Translator(translations)))
    for fn in filenames:
        if fn.endswith('~') or fn.endswith('.swp'):
            continue
        src_file = os.path.join(dirpath, fn)
        if options.rss:
            sys.setrecursionlimit(1500)
            for line in fileinput.input(src_file):
                if line.find('feedparse')>0:
                    match = re.split('^.*feedparse\(\'', line)
                    feedurl = re.split('\'\)', match[1])
                    feedparse(feedurl[0])
            continue;
        release_date = None
        if schedule is not None:
            release_date = schedule(globalvar.release['next_id'])

            # We need to localize the date format. First, set locale to the original format
            locale.setlocale(locale.LC_ALL, 'en_US')

            # Then convert to time_struct
            alpha =  time.strptime(release_date[globalvar.release['next_id']]['alpha'], "%Y-%b-%d")
            beta =  time.strptime(release_date[globalvar.release['next_id']]['beta'], "%Y-%b-%d")
            final =  time.strptime(release_date[globalvar.release['next_id']]['final'], "%Y-%b-%d")

            # Stort the US format for further processing
            alpha_us = time.strftime("%Y-%b-%d", alpha)
            beta_us = time.strftime("%Y-%b-%d", beta)
            final_us = time.strftime("%Y-%b-%d", final)

            # Let's slice the US date for the calendar png

            beta_cal = beta_us[-2:]
            final_cal = final_us[-2:]

            # Move to the right locale (if known)
            try:
                locale.setlocale(locale.LC_ALL, locale.locale_alias[options.lang.lower()])
            except:
                pass

            # Convert back!
            alpha = convert_date(alpha)
            beta = convert_date(beta)
            final = convert_date(final)

            locale.setlocale(locale.LC_ALL, 'en_US')
            release_date = {'alpha':alpha, 'beta':beta, 'final':final, 'alpha_us':alpha_us, 'beta_us':beta_us, 'final_us':final_us, 'beta_cal':beta_cal, 'final_cal':final_cal}

        ec2 = None
        if get_amis is not None:
            ec2 = get_amis()

        dest_file = os.path.join(options.output, src_file[len(options.input):]) + '.' + options.lang # Hideous
        curpage = src_file[len(options.input):].rstrip('.html')
        relpath = '../' * (dest_file.count('/') - 1)
        relpath = relpath.rstrip('/')
        if relpath == '': relpath = '.'
        safe_makedir(os.path.dirname(dest_file))
        template = loader.load(src_file)
        # Variables made availble to all templates
        page = template.generate(
            _=lambda text: translations.ugettext(text),
            feedparse=feedparse,
            lang=options.lang,
            relpath=relpath,
            path=options.basepath,
            curpage=curpage,
            global_variables=globalvar,
            schedule = release_date,
            ec2_ami = ec2
            ).render(method='html', doctype='html', encoding='utf-8')
        output = open(dest_file, 'w')
        output.write(page)
        output.close()