Esempio n. 1
0
    def _processCatalog(self, catalog, messages, fallBack=None):
        '''
        Processes a catalog based on the given messages list. Basically the catalog will be made in sync with the list of
        messages.

        @param catalog: Catalog
            The catalog to keep in sync.
        @param messages: Iterable
            The messages to update the catalog with.
        @param fallBack: Catalog
            The fall back catalog to get the missing catalog messages .
        @return: Catalog
            The same catalog
        '''
        assert isinstance(catalog, Catalog), 'Invalid catalog %s' % catalog
        assert isinstance(messages,
                          Iterable), 'Invalid messages list %s' % messages

        for msg in catalog:
            msg.locations = []

        for msg in messages:
            assert isinstance(msg, Message)
            id = msg.Singular if not msg.Plural else (msg.Singular, ) + tuple(
                msg.Plural)
            src = self.sourceService.getById(msg.Source)
            context = msg.Context if msg.Context != '' else None
            msgC = catalog.get(msg.Singular, context)
            if msgC is None and fallBack is not None:
                assert isinstance(
                    fallBack,
                    Catalog), 'Invalid fall back catalog %s' % fallBack
                msgC = fallBack.get(msg.Singular, context)
                if msgC is not None:
                    msgC.locations = []
                    catalog[msg.Singular] = msgC
            msgCOrig = copy(msgC)
            catalog.add(id,
                        context=msg.Context if msg.Context != '' else None,
                        locations=((src.Path, msg.LineNumber), ),
                        user_comments=(msg.Comments if msg.Comments else '', ))
            if msgC: fixBabelCatalogAddBug(msgC, catalog.num_plurals)
            if msg.Plural and msgC and msgCOrig and isinstance(
                    msgCOrig.string, str) and msgCOrig.string != '':
                copyTranslation(msgCOrig, msgC)

        creationDate = catalog.creation_date  # We need to make sure that the catalog keeps its creation date.
        catalog.creation_date = creationDate
        return catalog
Esempio n. 2
0
    def _processCatalog(self, catalog, messages, fallBack=None):
        '''
        Processes a catalog based on the given messages list. Basically the catalog will be made in sync with the list of
        messages.

        @param catalog: Catalog
            The catalog to keep in sync.
        @param messages: Iterable
            The messages to update the catalog with.
        @param fallBack: Catalog
            The fall back catalog to get the missing catalog messages .
        @return: Catalog
            The same catalog
        '''
        assert isinstance(catalog, Catalog), 'Invalid catalog %s' % catalog
        assert isinstance(messages, Iterable), 'Invalid messages list %s' % messages

        for msg in catalog: msg.locations = []

        for msg in messages:
            assert isinstance(msg, Message)
            id = msg.Singular if not msg.Plural else (msg.Singular,) + tuple(msg.Plural)
            src = self.sourceService.getById(msg.Source)
            context = msg.Context if msg.Context != '' else None
            msgC = catalog.get(msg.Singular, context)
            if msgC is None and fallBack is not None:
                assert isinstance(fallBack, Catalog), 'Invalid fall back catalog %s' % fallBack
                msgC = fallBack.get(msg.Singular, context)
                if msgC is not None:
                    msgC.locations = []
                    catalog[msg.Singular] = msgC
            msgCOrig = copy(msgC)
            catalog.add(id, context=msg.Context if msg.Context != '' else None,
                        locations=((src.Path, msg.LineNumber),),
                        user_comments=(msg.Comments if msg.Comments else '',))
            if msgC: fixBabelCatalogAddBug(msgC, catalog.num_plurals)
            if msg.Plural and msgC and msgCOrig and isinstance(msgCOrig.string, str) and msgCOrig.string != '':
                copyTranslation(msgCOrig, msgC)

        creationDate = catalog.creation_date  # We need to make sure that the catalog keeps its creation date.
        catalog.creation_date = creationDate
        return catalog
Esempio n. 3
0
    def _update(self, locale, messages, poFile, path, pathMO, isGlobal=True):
        assert isinstance(locale, Locale), 'Invalid locale %s' % locale
        assert isinstance(messages, Iterable), 'Invalid messages %s' % messages
        assert isinstance(poFile, IInputStream), 'Invalid file object %s' % poFile
        assert isinstance(path, str), 'Invalid path %s' % path
        assert isinstance(pathMO, str), 'Invalid path MO %s' % pathMO
        assert isinstance(isGlobal, bool), 'Invalid is global flag %s' % isGlobal

        catalog = read_po(poFile, locale=locale)
        assert isinstance(catalog, Catalog), 'Invalid catalog %s' % catalog
        if not catalog:
            # The catalog has no messages, no need for updating.
            return

        if not isGlobal:
            pathGlobal = self._filePath(locale)
            pathGlobalMO = self._filePath(locale, format=FORMAT_MO)
            if isfile(pathGlobal):
                with open(pathGlobal) as fObj: catalogGlobal = read_po(fObj, locale)
                self._processCatalog(catalogGlobal, self.messageService.getMessages())
            else:
                isGlobal, path, pathMO = True, pathGlobal, pathGlobalMO
                messages = self.messageService.getMessages()
        self._processCatalog(catalog, messages)

        if isfile(path):
            with open(path) as fObj: catalogOld = read_po(fObj, locale)
            for msg in catalog:
                msgO = catalogOld.get(msgId(msg), msg.context)
                if not isMsgTranslated(msg) and msgO and isMsgTranslated(msgO):
                    msg.string = msgO.string
            catalog.creation_date = catalogOld.creation_date
        else:
            pathDir = dirname(path)
            if not isdir(pathDir): os.makedirs(pathDir)
            catalog.creation_date = datetime.now()

        if not isGlobal:
            # We remove all the messages that are not translated or have the same translation as in the global locale
            # or are the only plugin that makes use of the message in the global.
            updatedGlobal = False
            for msg in list(catalog):
                id = msgId(msg)
                if not id: continue
                if not isMsgTranslated(msg):
                    catalog.delete(id, msg.context)
                else:
                    msgG = catalogGlobal.get(id, msg.context)
                    if not msgG or msgG.string == msg.string:
                        catalog.delete(id, msg.context)
                    elif not isMsgTranslated(msgG) or msgG.locations == msg.locations:
                        copyTranslation(msg, msgG)
                        catalog.delete(id, msg.context)
                        updatedGlobal = True

            if updatedGlobal:
                # We remove all the messages that are not translated.
                for msg in list(catalogGlobal):
                    if not isMsgTranslated(msg):
                        catalogGlobal.delete(msgId(msg), msg.context)

                catalogGlobal.revision_date = datetime.now()
                os.makedirs(dirname(pathGlobal), exist_ok=True)
                with open(pathGlobal, 'wb') as fObj: write_po(fObj, catalogGlobal, **self.write_po_config)
                os.makedirs(dirname(pathGlobalMO), exist_ok=True)
                with open(pathGlobalMO, 'wb') as fObj: write_mo(fObj, catalogGlobal)
        else:
            # We remove all the messages that are not translated.
            for msg in list(catalog):
                if not isMsgTranslated(msg):
                    catalog.delete(msgId(msg), msg.context)

        catalog.revision_date = datetime.now()
        os.makedirs(dirname(path), exist_ok=True)
        with open(path, 'wb') as fObj: write_po(fObj, catalog, **self.write_po_config)
        os.makedirs(dirname(pathMO), exist_ok=True)
        with open(pathMO, 'wb') as fObj: write_mo(fObj, catalog)
Esempio n. 4
0
    def _update(self, locale, messages, poFile, path, pathMO, isGlobal=True):
        assert isinstance(locale, Locale), 'Invalid locale %s' % locale
        assert isinstance(messages, Iterable), 'Invalid messages %s' % messages
        assert isinstance(poFile,
                          IInputStream), 'Invalid file object %s' % poFile
        assert isinstance(path, str), 'Invalid path %s' % path
        assert isinstance(pathMO, str), 'Invalid path MO %s' % pathMO
        assert isinstance(isGlobal,
                          bool), 'Invalid is global flag %s' % isGlobal

        catalog = read_po(poFile, locale=locale)
        assert isinstance(catalog, Catalog), 'Invalid catalog %s' % catalog
        if not catalog:
            # The catalog has no messages, no need for updating.
            return

        if not isGlobal:
            pathGlobal = self._filePath(locale)
            pathGlobalMO = self._filePath(locale, format=FORMAT_MO)
            if isfile(pathGlobal):
                with open(pathGlobal) as fObj:
                    catalogGlobal = read_po(fObj, locale)
                self._processCatalog(catalogGlobal,
                                     self.messageService.getMessages())
            else:
                isGlobal, path, pathMO = True, pathGlobal, pathGlobalMO
                messages = self.messageService.getMessages()
        self._processCatalog(catalog, messages)

        if isfile(path):
            with open(path) as fObj:
                catalogOld = read_po(fObj, locale)
            for msg in catalog:
                msgO = catalogOld.get(msgId(msg), msg.context)
                if not isMsgTranslated(msg) and msgO and isMsgTranslated(msgO):
                    msg.string = msgO.string
            catalog.creation_date = catalogOld.creation_date
        else:
            pathDir = dirname(path)
            if not isdir(pathDir): os.makedirs(pathDir)
            catalog.creation_date = datetime.now()

        if not isGlobal:
            # We remove all the messages that are not translated or have the same translation as in the global locale
            # or are the only plugin that makes use of the message in the global.
            updatedGlobal = False
            for msg in list(catalog):
                id = msgId(msg)
                if not id: continue
                if not isMsgTranslated(msg):
                    catalog.delete(id, msg.context)
                else:
                    msgG = catalogGlobal.get(id, msg.context)
                    if not msgG or msgG.string == msg.string:
                        catalog.delete(id, msg.context)
                    elif not isMsgTranslated(
                            msgG) or msgG.locations == msg.locations:
                        copyTranslation(msg, msgG)
                        catalog.delete(id, msg.context)
                        updatedGlobal = True

            if updatedGlobal:
                # We remove all the messages that are not translated.
                for msg in list(catalogGlobal):
                    if not isMsgTranslated(msg):
                        catalogGlobal.delete(msgId(msg), msg.context)

                catalogGlobal.revision_date = datetime.now()
                os.makedirs(dirname(pathGlobal), exist_ok=True)
                with open(pathGlobal, 'wb') as fObj:
                    write_po(fObj, catalogGlobal, **self.write_po_config)
                os.makedirs(dirname(pathGlobalMO), exist_ok=True)
                with open(pathGlobalMO, 'wb') as fObj:
                    write_mo(fObj, catalogGlobal)
        else:
            # We remove all the messages that are not translated.
            for msg in list(catalog):
                if not isMsgTranslated(msg):
                    catalog.delete(msgId(msg), msg.context)

        catalog.revision_date = datetime.now()
        os.makedirs(dirname(path), exist_ok=True)
        with open(path, 'wb') as fObj:
            write_po(fObj, catalog, **self.write_po_config)
        os.makedirs(dirname(pathMO), exist_ok=True)
        with open(pathMO, 'wb') as fObj:
            write_mo(fObj, catalog)