Esempio n. 1
0
    def create_translation(self, *args, **kwargs):
        """Create translation for an object with uid in the given
        target_language and return its UID

        Usage::

            Create translation  /plone/en/foo  ca  title=Translated
        """
        disableCSRFProtection()
        # Parse arguments:
        uid_or_path = args[0]
        target_language = args[1]

        # BBB: Support keywords arguments with robotframework < 2.8.3
        kwargs.update(dict([arg.split('=', 1) for arg in args[2:]]))

        # Look up translatable content
        pc = getToolByName(self, "portal_catalog")
        uid_results = pc.unrestrictedSearchResults(UID=uid_or_path)
        path_results = pc.unrestrictedSearchResults(
            path={
                'query': uid_or_path.rstrip('/'),
                'depth': 0
            })
        obj = (uid_results or path_results)[0]._unrestrictedGetObject()

        # Translate
        manager = ITranslationManager(obj)
        manager.add_translation(target_language)
        translation = manager.get_translation(target_language)

        # Update fields
        data = constructMessageFromSchemata(obj, iterSchemata(obj))
        for key, value in kwargs.items():
            del data[key]
            data[key] = Header(value, 'utf-8')
        del data['language']
        initializeObjectFromSchemata(translation, iterSchemata(obj), data)
        notify(ObjectModifiedEvent(translation))

        # Return uid for the translation
        return IUUID(translation)
Esempio n. 2
0
    def copy_contents_of(self, item, target_languages):
        if item.portal_type in SKIPPED_PORTAL_TYPES:
            log.info("Item skipped: {0}".format("/".join(
                item.getPhysicalPath())))
        else:
            for language in target_languages:
                manager = ITranslationManager(item)
                if not manager.has_translation(language):
                    manager.add_translation(language)
                    log.info("Created translation for {}: {}".format(
                        "/".join(item.getPhysicalPath()), language))
                    import transaction

                    transaction.commit()
                translated = manager.get_translation(language)
                self.copy_fields(item, translated)
                self.copy_seo_properties(item, translated)
                self.copy_other_properties(item, translated)
                self.copy_other_things(item, translated)
                # translated.id = safe_unicode(translated.id).encode('utf-8')
                translated.reindexObject()
    def create_translation(self, *args, **kwargs):
        """Create translation for an object with uid in the given
        target_language and return its UID

        Usage::

            Create translation  /plone/en/foo  ca  title=Translated
        """
        disableCSRFProtection()
        # Parse arguments:
        uid_or_path = args[0]
        target_language = args[1]

        # BBB: Support keywords arguments with robotframework < 2.8.3
        kwargs.update(dict([arg.split('=', 1) for arg in args[2:]]))

        # Look up translatable content
        pc = getToolByName(self, "portal_catalog")
        uid_results = pc.unrestrictedSearchResults(UID=uid_or_path)
        path_results = pc.unrestrictedSearchResults(
            path={'query': uid_or_path.rstrip('/'), 'depth': 0})
        obj = (uid_results or path_results)[0]._unrestrictedGetObject()

        # Translate
        manager = ITranslationManager(obj)
        manager.add_translation(target_language)
        translation = manager.get_translation(target_language)

        # Update fields
        data = constructMessageFromSchemata(obj, iterSchemata(obj))
        for key, value in kwargs.items():
            del data[key]
            data[key] = Header(value, 'utf-8')
        del data['language']
        initializeObjectFromSchemata(translation, iterSchemata(obj), data)
        notify(ObjectModifiedEvent(translation))

        # Return uid for the translation
        return IUUID(translation)
Esempio n. 4
0
def translate(content, target_language='en'):
    manager = ITranslationManager(content)
    manager.add_translation(target_language)
    return manager.get_translation(target_language)
Esempio n. 5
0
def makeTranslation(content, language="en"):
    manager = ITranslationManager(content)
    manager.add_translation(language)
    return manager.get_translation(language)
Esempio n. 6
0
def makeTranslation(content, language='en'):
    manager = ITranslationManager(content)
    manager.add_translation(language)
    return manager.get_translation(language)
Esempio n. 7
0
    def __call__(self):
        context = self.context
        request = self.request
        catalog = context.portal_catalog
        portal = api.portal.get()
        alsoProvides(request, IDisableCSRFProtection)

        root_zh = portal.get('zh')
        if not root_zh: return
        root_en = portal.get('en')
        if not root_en: return

        prog_zh = root_zh.get('programs')
        if not prog_zh: return
        prog_en = root_en.get('programs')
        if not prog_en: return

        container_zh = prog_zh.get('recipients')
        if not container_zh: return
        container_en = prog_en.get('recipients')
        if not container_en: return

        with open('grant-list.csv', "rb") as file:
            rows = csv.reader(file, delimiter='\t')

            next(rows, None)
            for row in rows:
                folder_zh = container_zh.get(str(row[1]))
                if not folder_zh:
                    fldr = api.content.create(
                        type='Folder',
                        container=container_zh,
                        id=row[1],
                        title=row[1]
                    )
                    fldr.reindexObject()

                if row[2] == '': row[2] = row[3]
                if row[4] == '': row[4] = row[5]
                if row[3] == '': row[3] = row[2]
                if row[5] == '': row[5] = row[4]

                ctnt = api.content.create(
                    type='Grant',
                    container=container_zh.get(str(row[1])),
                    id=row[0],
                    title=safe_unicode(row[2]),
                    year=row[1],
                    director=row[4],
                    description=row[6],
                    department=row[7],
                    region=row[8],
                    category=row[9],
                    budget=row[10],
                    currency=row[11],
                    status=row[12]
                )

                manager = ITranslationManager(ctnt)
                manager.add_translation('en')
                trns = manager.get_translation('en')
                trns.id = str(row[0])
                trns.title = row[3]
                trns.year = row[1]
                trns.director = row[5]
                trns.description = row[6]
                trns.department = row[7]
                trns.region = row[8]
                trns.category = row[9]
                trns.budget = row[10]
                trns.currency = row[11]
                trns.status = row[13]

                ctnt.reindexObject()
                trns.reindexObject()
Esempio n. 8
0
def makeTranslation(content, language='en'):
    manager = ITranslationManager(content)
    manager.add_translation(language)
    import transaction; transaction.commit()
    return manager.get_translation(language)