[orm-devel] How to handle multi language?

Werner F. Bruhin werner.bruhin at free.fr
Fri Jun 17 21:12:27 CEST 2005


Hi Diedrich,

All your suggestions got me thinking.  Whenever you or anyone else has a 
moment to look at the attached, I would be greatful for comments/critics 
- this is nothing urgent.

So, I thought why not use a kind of a relation?  I put something 
together roughly based on the many2one relationship, it only allows for 
"get"ing language specific data.

I am not sure if this approach would hold up some serious testing/usage.

I basically created a "lang2one" relationship, it allows to define a 
default language and a preferred language, if later is found the data 
for it is returned, otherwise the data for the default language is returned.

I don't think that the naming of attributes etc I choose is that good.  
I am not that happy with all these fields defined for the country table, 
initially I had a setLanguage method in the lang2one relation but 
couldn't make it work.

Files attached:
relationships.py - this lives in adapter/firebird (if a generic solution 
is agreed on/found it should live in orm/relationships.py)
lang2one* - live in examples/firebird

Any hints, comments, critic (but be nice, please!) is very welcome
Werner

Diedrich Vorberg wrote:

> Hi Werner!
>
>> My application supports multiple languages (en, de, fr), my current  
>> implementation works but I think there is "space for improvement"  
>> and I am looking for some hints/suggestions on how to do this.
>
>
>> Thanks in advance for any ideas and/or hints.
>
> I've got an idea I used for a web application, It's not ideal,  
> though. I used one column per table per language like this:
>
>    CREATE TABLE something
>    (
>        title_de TEXT,
>        description_de TEXT,
>        title_en TEXT,
>        description_en TEXT
>    );
>
> I'm using Livinglogic's XIST XML handling library to create XHTML  
> output which represents the XHTML DOM tree using Python classes. I  
> inserted the relevant data into the dom tree with the HTML attribute  
> lang="" set to the correct language. Just previous to delivery I have  
> a little routine walk the DOM tree an delete all entries that do not  
> fit the user's language.
>
> If you are writing a GUI application you might want to create the  
> model-Module (the one with all the dbclasses in) on startup depending  
> on the user's language settings. Than you could mangle the columns  
> dicts according to the locale. Like this (just a code-sketch)
>
>    from orm.dbclass import dbclass
>    from orm.columns import *
>    from orm.firebird.columns import *
>
>    class something(dbclass):
>       pass
>
>    something_columns = ( ("title", text, "title_%s"),
>                          ("description", text, "description_%s"), )
>
>    class someting_else(dbclass):
>        pass
>
>    something_columns = ( ("blah", text, "blah_%s"),
>                          ("blub", text, "blub_%s"), )
>
>    # figure out my locale, determine language abriviation, store it  
> in lang variable
>
>    for class_name in ("something", "something_else"):
>       dbclass = locals()[class_name]
>       column_tuples = locals()[class_name]
>
>       columns_dict = {}
>       for attribute_name, column_class, sql_column_name in  
> column_tuples:
>          columns_dict[attribute_name] = column_class 
> (column_name="sql_column_name" % lang)
>
>       dbclass.columns = columns_dict
>
>    # now you can normaly instantiate something and something_else
>
>
> There is another idea I had: a dict_relationship that would let you  
> access different columns in a dbclass like a dict as in
>
>    dbobj.attribute[lang]
>
> which maps to SQL column dbobj's attribute_"lang"-column.
>
>
> Diedrich
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: relationships.py
Type: application/x-python
Size: 4787 bytes
Desc: not available
Url : http://mailman.tux4web.de/pipermail/orm-devel/attachments/20050617/09d60439/relationships-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lang2one_relationship.py
Type: application/x-python
Size: 2617 bytes
Desc: not available
Url : http://mailman.tux4web.de/pipermail/orm-devel/attachments/20050617/09d60439/lang2one_relationship-0002.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lang2one_relationship.sql
Type: application/octetstream
Size: 3378 bytes
Desc: not available
Url : http://mailman.tux4web.de/pipermail/orm-devel/attachments/20050617/09d60439/lang2one_relationship-0003.bin


More information about the orm-devel mailing list