On Tuesday 27 June 2006 08:08, Frank Burkhardt wrote:
>  data={
>     'foo':{
>        'en':u"nice Foo",
>        'de':u"schoener Foo"
>     },
>     'bar':{
>        'en':u"even better Bar",
>     }
> How could I do this?

Just do the usual thing: _(u'text'). If you do not type the text in Python, 
then you have to write your own I18n support. For example, I wrote a simple 
CSV vocabulary that generates messages. I then wrote a special CSV title 
message extraction tool and integrated it in the i18nextract tool.

Here is the code:

def CSVVocabulary(filename, messageFactory=_):
    # Create a prefix
    prefix = os.path.split(filename)[-1][:-4]
    # Open a file and read the data
    f = file(filename)
    reader = csv.reader(f, delimiter=";")
    # Create the terms and the vocabulary
    terms = []
    for id, title in reader:
        title = unicode(title, 'latin1')
        term = vocabulary.SimpleTerm(id, title=_(prefix+'-'+id, default=title)
    return vocabulary.SimpleVocabulary(terms)

def _extract_csv_strings(arg, dirname, fnames):
    catalog, basepath, exclude_dirs = arg
    # Make sure we have a data directory
    if os.path.split(dirname)[-1] != 'data':
    # Make sure we are not stepping into an excluded dir
    for exclude_dir in exclude_dirs:
        if dirname.startswith(exclude_dir):
    # Now we extract all strings from the csv files
    for filename in fnames:
        if filename.endswith('.csv'):
            fullpath = os.path.join(dirname, filename)
            vocab = CSVVocabulary(fullpath)
            for index, term in enumerate(vocab):
                if term.title not in catalog:
                    catalog[term.title] = []
                reportpath = fullpath.replace(basepath, '')
                catalog[term.title].append((reportpath, index+1))

def csv_strings(path, base_dir, exclude_dirs=()):
    """Extract message strings from CSV data files"""
    catalog = {}
    exclude_dirs = [os.path.join(path, dir) for dir in exclude_dirs]
    os.path.walk(path, _extract_csv_strings, (catalog, base_dir, exclude_dirs)
    return catalog

Stephan Richter
CBU Physics & Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training
Zope3-users mailing list

Reply via email to