>
> Do you already have the mapping, perhaps in a dictionary (or can you get 
>> it into one)? If so, maybe something like:
>>
>> ISO_COUNTRY_CODE_MAPPINGS = {k: T(v) for (k, v) in original_mapping.
>> iteritems()} # requires Python 2.7
>>
>> I  see. It is still iterating through the whole mapping for each request, 
> though. Why not just use the original_mapping directly?
>

The T() object is created per request. How would you use the 
original_mapping directly -- I'm assuming that has all the country names in 
English, but you want them translated to the current language? Anyway, I'm 
not sure this adds too much overhead. First, by default, calls to T(v) are 
lazy -- translation isn't done until serialization in the view, so 
translation will only happen for requests that serialize the auth_user form 
in a view, not all requests. Second, the translation itself is just a 
dictionary lookup in a dictionary that is already in memory. Third, if you 
use T.set_current_languages(), you can specify the language used in 
original_mappings, and whenever that language is requested, no translation 
will happen at all. Finally, you can move the definition of this particular 
validator to the user() function that handles the Auth forms, so even the 
dictionary comprehension will only be executed on requests involving Auth 
forms.

You can also have pre-translated dictionaries for every language, but 
you'll have to store them somewhere (maybe a module). In that case, you can 
use T.accepted_language to determine the language of the current request. 
Another option might be adding an item like the following to all language 
files:

'COUNTRY_NAMES':'Germany|Spain|Italy|United States|...'

Then in the application, do:

member.country.requests = IS_IN_SET(zip(COUNTRY_CODES, T('COUNTRY_NAMES').
split('|')))

That puts all the translated country names in a single pipe-separated list, 
so it involves only one translator lookup. Then split the list and zip it 
with the matching country codes to get a list of tuples for the validator.

Anthony

-- 



Reply via email to