Skip to main content

Automatically translate files uploaded by PloneFlashUpload

Project description

Full readme with doctests in slc/autotranslate/README.txt

Introduction

This product adds a boolean ‘autoTranslateUploadedFiles’ on all BaseFolder based folders, which enables any uploaded files to be automatically translated.

‘Translation’ here merely refers to a procedure whereby the file’s language is identified and its language code set accordingly (via setLanguage).

The language that the file will be translated into, is determined form the file’s name. The file name must be prefixed or suffixed with a language code, followed (or preceded) with an underscore.

The following are valid file names:

  • de_factsheet.pdf

  • factsheet_de.pdf

How To Use (Doc Tests):

First, we import everything we need:

>>> from z3c.widget.flashupload.interfaces import FlashUploadedEvent
>>> from Products.Archetypes.event import ObjectInitializedEvent
>>> from zope import event

We will use the test user’s Member folder, to upload the files in.

>>> self.folder
<ATFolder at /plone/Members/test_user_1_>
>>> folder = self.folder

To enable the autotranslation feature, we must set the ‘autoTranslateUploadedFiles’ field on the parent folder.

This is a field added via schema-extension, so we cannot use an Archetypes generated mutator.

>>> folder.Schema().get('autoTranslateUploadedFiles', True).set(folder, True)

Now we simulate the uploading of 2 files via PloneFlashUpload. Each time we create a file inside our folder, and then call the FlashUploadedEvent.

>>> for fn in ['en_file.txt', 'de_file.txt']:
...     fid = folder.invokeFactory('File', fn)
...     file = getattr(folder, fid)
...     f = open('src/slc.autotranslate/slc/autotranslate/tests/%s' % fn)
...     file.setFile(f)
...     event.notify(FlashUploadedEvent(file))

Let’s see if our uploaded files were set correctly to their indicated languages, and also that the canonical was set properly:

>>> file = getattr(folder, 'en_file.txt')
>>> file.getLanguage()
'en'
>>> file.getCanonical()
<ATFile at /plone/Members/test_user_1_/en_file.txt>
>>> file = getattr(folder, 'de_file.txt')
>>> file.getLanguage()
'de'
>>> file.getCanonical()
<ATFile at /plone/Members/test_user_1_/en_file.txt>

File names that end with the language code (as apposed to it being prefixed) are also valid. We’ll test now that this is indeed the case.

First we again upload a file, this time file_es.txt (Spanish):

>>> fid = folder.invokeFactory('File', 'file_es.txt')
>>> file = getattr(folder, fid)
>>> f = open('src/slc.autotranslate/slc/autotranslate/tests/file_es.txt')
>>> file.setFile(f)

Now we fire the event. This time, we use Archetypes’ ObjectInitializedEvent, to show that normally added (as opposed to PloneFlashUpload) files are also translated.

>>> event.notify(ObjectInitializedEvent(file))

Let’s test that the file’s language and the canonical was set correctly:

>>> file = getattr(folder, 'file_es.txt')
>>> file.getLanguage()
'es'
>>> file.getCanonical()
<ATFile at /plone/Members/test_user_1_/en_file.txt>

Additional Tests:

We test now that every file’s language as well as it’s relationship to the canonical is properly set, when the canonical is uploaded last.

First, we clear the folder to start anew.

>>> folder.manage_delObjects(folder.objectIds())
>>> folder.objectIds()
[]

Create the files and call the event:

>>> for fn in ['de_file.txt', 'file_es.txt', 'fr_file.txt', 'en_file.txt']:
...     fid = folder.invokeFactory('File', fn)
...     file = getattr(folder, fid)
...     f = open('src/slc.autotranslate/slc/autotranslate/tests/%s' % fn)
...     file.setFile(f)
...     event.notify(ObjectInitializedEvent(file))

Test that everything is still in order:

>>> file = getattr(folder, 'de_file.txt')
>>> file.getLanguage()
'de'
>>> file.getCanonical()
<ATFile at /plone/Members/test_user_1_/en_file.txt>
>>> file = getattr(folder, 'file_es.txt')
>>> file.getLanguage()
'es'
>>> file.getCanonical()
<ATFile at /plone/Members/test_user_1_/en_file.txt>
>>> file = getattr(folder, 'fr_file.txt')
>>> file.getLanguage()
'fr'
>>> file.getCanonical()
<ATFile at /plone/Members/test_user_1_/en_file.txt>
>>> file = getattr(folder, 'en_file.txt')
>>> file.getLanguage()
'en'
>>> file.getCanonical()
<ATFile at /plone/Members/test_user_1_/en_file.txt>

Change history

slc.autotranslate 1.2 (2009-08-24)

  • Add interlude as package dependency

slc.autotranslate 1.1 (2009-08-21)

  • Use setLanguage instead of addTranslation

  • Properly set the canonical and translation references

  • Add more extensive tests

slc.autotranslate 1.0 (2009-08-13)

  • Initial release.

Contributors

  • Jan-Carel Brand (Syslab.com GmbH)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slc.autotranslate-1.2.zip (16.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page