예제 #1
0
    def update_file(self, filename):
        # Adapted from Document.__init__()
        from translate.storage import factory, statsdb
        newstore = factory.getobject(filename)
        oldfilename = self._trans_store.filename
        oldfileobj = self._trans_store.fileobj

        #get a copy of old stats before we convert
        from translate.filters import checks
        oldstats = statsdb.StatsCache().filestats(oldfilename,
                                                  checks.UnitChecker(),
                                                  self._trans_store)

        from translate.convert import pot2po
        self._trans_store = pot2po.convert_stores(newstore,
                                                  self._trans_store,
                                                  fuzzymatching=False)
        self._trans_store.fileobj = oldfileobj  #Let's attempt to keep the old file and name if possible

        #FIXME: ugly tempfile hack, can we please have a pure store implementation of statsdb
        import tempfile
        import os
        tempfd, tempfilename = tempfile.mkstemp()
        os.write(tempfd, str(self._trans_store))
        self.update_stats(filename=tempfilename)
        os.close(tempfd)
        os.remove(tempfilename)

        self.controller.compare_stats(oldstats, self.stats)

        # store filename or else save is confused
        self._trans_store.filename = oldfilename
        self._correct_header(self._trans_store)
        self.nplurals = self._compute_nplurals(self._trans_store)
예제 #2
0
    def update_file(self, filename):
        # Adapted from Document.__init__()
        from translate.storage import factory, statsdb
        newstore = factory.getobject(filename)
        oldfilename = self._trans_store.filename
        oldfileobj = self._trans_store.fileobj

        #get a copy of old stats before we convert
        from translate.filters import checks
        oldstats = statsdb.StatsCache().filestats(oldfilename, checks.UnitChecker(), self._trans_store)

        from translate.convert import pot2po
        self._trans_store = pot2po.convert_stores(newstore, self._trans_store, fuzzymatching=False)
        self._trans_store.fileobj = oldfileobj #Let's attempt to keep the old file and name if possible

        #FIXME: ugly tempfile hack, can we please have a pure store implementation of statsdb
        import tempfile
        import os
        tempfd, tempfilename = tempfile.mkstemp()
        os.write(tempfd, str(self._trans_store))
        self.update_stats(filename=tempfilename)
        os.close(tempfd)
        os.remove(tempfilename)

        self.controller.compare_stats(oldstats, self.stats)

        # store filename or else save is confused
        self._trans_store.filename = oldfilename
        self._correct_header(self._trans_store)
        self.nplurals = self._compute_nplurals(self._trans_store)
예제 #3
0
def convert_template(translation_project,
                     template_store,
                     target_pootle_path,
                     target_path,
                     monolingual=False):
    """run pot2po to update or initialize file on target_path with template_store"""
    ensure_target_dir_exists(target_path)
    if template_store.file:
        template_file = template_store.file.store
    else:
        template_file = template_store

    try:
        store = Store.objects.get(pootle_path=target_pootle_path)
        if monolingual and store.state < PARSED:
            #HACKISH: exploiting update from templates to parse monolingual files
            store.update(store=template_file)
            store.update(update_translation=True)
            return
        if not store.file or monolingual:
            original_file = store
        else:
            original_file = store.file.store
    except Store.DoesNotExist:
        original_file = None
        store = None

    output_file = pot2po.convert_stores(template_file,
                                        original_file,
                                        fuzzymatching=False,
                                        classes=factory_classes)
    if template_store.file:
        if store:
            store.update(update_structure=True,
                         update_translation=True,
                         conservative=False,
                         store=output_file,
                         fuzzy=True)
        output_file.settargetlanguage(translation_project.language.code)
        output_file.savefile(target_path)
    elif store:
        store.mergefile(output_file,
                        None,
                        allownewstrings=True,
                        suggestions=False,
                        notranslate=False,
                        obsoletemissing=True)
    else:
        output_file.translation_project = translation_project
        output_file.name = template_store.name
        output_file.parent = translation_project.directory
        output_file.state = PARSED
        output_file.save()

    # pot2po modifies its input stores so clear caches is needed
    if template_store.file:
        template_store.file._delete_store_cache()
    if store and store.file:
        store.file._delete_store_cache()
예제 #4
0
def convert_template(translation_project, template_store, target_pootle_path,
                     target_path, monolingual=False):
    """Run pot2po to update or initialize the file on `target_path` with
    `template_store`.
    """

    ensure_target_dir_exists(target_path)

    if template_store.file:
        template_file = template_store.file.store
    else:
        template_file = template_store

    try:
        store = Store.objects.get(pootle_path=target_pootle_path)

        if monolingual and store.state < PARSED:
            #HACKISH: exploiting update from templates to parse monolingual files
            store.update(store=template_file)
            store.update(update_translation=True)
            return

        if not store.file or monolingual:
            original_file = store
        else:
            original_file = store.file.store
    except Store.DoesNotExist:
        original_file = None
        store = None

    from translate.convert import pot2po
    from pootle_store.filetypes import factory_classes
    output_file = pot2po.convert_stores(template_file, original_file,
                                        fuzzymatching=False,
                                        classes=factory_classes)
    if template_store.file:
        if store:
            store.update(update_structure=True, update_translation=True,
                         conservative=False, store=output_file, fuzzy=True)
        output_file.settargetlanguage(translation_project.language.code)
        output_file.savefile(target_path)
    elif store:
        store.mergefile(output_file, None, allownewstrings=True,
                        suggestions=False, notranslate=False,
                        obsoletemissing=True)
    else:
        output_file.translation_project = translation_project
        output_file.name = template_store.name
        output_file.parent = translation_project.directory
        output_file.state = PARSED
        output_file.save()

    # pot2po modifies its input stores so clear caches is needed
    if template_store.file:
        template_store.file._delete_store_cache()
    if store and store.file:
        store.file._delete_store_cache()
def convert_template(translation_project, template_store, target_pootle_path, target_path, monolingual=False):
    """run pot2po to update or initialize file on target_path with template_store"""
    ensure_target_dir_exists(target_path)
    if template_store.file:
        template_file = template_store.file.store
    else:
        template_file = template_store

    try:
        store = Store.objects.get(pootle_path=target_pootle_path)
        if monolingual and store.state < PARSED:
            store.update(store=template_file)
            store.update(update_translation=True)
        if not store.file or monolingual:
            original_file = store
        else:
            original_file = store.file.store
    except Store.DoesNotExist:
        original_file = None
        store = None

    output_file = pot2po.convert_stores(template_file, original_file, classes=factory_classes)
    if template_store.file:
        output_file.savefile(target_path)
    elif store:
        store.mergefile(output_file, None, allownewstrings=True, suggestions=False, notranslate=False, obsoletemissing=True)
    else:
        output_file.translation_project = translation_project
        output_file.name = template_store.name
        output_file.parent = translation_project.directory
        output_file.state = PARSED
        output_file.save()

    # pot2po modifies its input stores so clear caches is needed
    if template_store.file:
        template_store.file._delete_store_cache()
    if store and store.file:
        store.file._delete_store_cache()