[web2py] Re: Map database values to display values

2012-06-25 Thread Anthony

>
> 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

-- 





[web2py] Re: Map database values to display values

2012-06-25 Thread Daniel Gonzalez


On Monday, June 25, 2012 6:38:47 PM UTC+2, Anthony wrote:
>
> 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
>
> Anthony
>
>
I  see. It is still iterating through the whole mapping for each request, 
though. Why not just use the original_mapping directly?

-- 





[web2py] Re: Map database values to display values

2012-06-25 Thread Anthony
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

Anthony

On Monday, June 25, 2012 11:53:24 AM UTC-4, Daniel Gonzalez wrote:
>
> Yes, that is one of the options I mentioned. If I include all possible 
> countries, my list would have200 entries.
>
> I still do not fully understand the process in which 
> modules/views/controllers are loaded when requests are received.
> But, if I am not completely mistaken, these files are parsed each time 
> that a request is received.
> That means that, for each request, the list of *200 countries* will be 
> translated to the user language.
> That means, lots of times the same translation will be performed (most of 
> my users will have the same language settings). Even if this is highly 
> efficient (caching of the translated objects?), this is still a lot of 
> processing, which could be avoided if I had a list of pre-translated 
> dictionaries (one for each language that I want to support).
>
> Of course it could be that I am not understanding at all how localization 
> in web2py is done ...
>
> On Monday, June 25, 2012 5:22:10 PM UTC+2, Anthony wrote:
>>
>> Now, to support internationalization here, I see two options:
>>>
>>> - I implement a function which returns the desired predefined mapping, 
>>> according to the language of the user. How can I know this? How does the T 
>>> operator know this?
>>> - I walk the list of ISO country codes applying the T operator to return 
>>> the mapping.
>>>
>>
>> Can't you just do:
>>
>> ISO_COUNTRY_CODES_MAPPING = {
>> 'DE': T('Germany'),
>> 'ES': T('Spain'),
>> 'IT': T('Italy'),
>> 'US': T('United States')
>> } 
>>
>>

-- 





[web2py] Re: Map database values to display values

2012-06-25 Thread Daniel Gonzalez
Yes, that is one of the options I mentioned. If I include all possible 
countries, my list would have200 entries.

I still do not fully understand the process in which 
modules/views/controllers are loaded when requests are received.
But, if I am not completely mistaken, these files are parsed each time that 
a request is received.
That means that, for each request, the list of *200 countries* will be 
translated to the user language.
That means, lots of times the same translation will be performed (most of 
my users will have the same language settings). Even if this is highly 
efficient (caching of the translated objects?), this is still a lot of 
processing, which could be avoided if I had a list of pre-translated 
dictionaries (one for each language that I want to support).

Of course it could be that I am not understanding at all how localization 
in web2py is done ...

On Monday, June 25, 2012 5:22:10 PM UTC+2, Anthony wrote:
>
> Now, to support internationalization here, I see two options:
>>
>> - I implement a function which returns the desired predefined mapping, 
>> according to the language of the user. How can I know this? How does the T 
>> operator know this?
>> - I walk the list of ISO country codes applying the T operator to return 
>> the mapping.
>>
>
> Can't you just do:
>
> ISO_COUNTRY_CODES_MAPPING = {
> 'DE': T('Germany'),
> 'ES': T('Spain'),
> 'IT': T('Italy'),
> 'US': T('United States')
> } 
>
>

-- 





[web2py] Re: Map database values to display values

2012-06-25 Thread Anthony

>
> Now, to support internationalization here, I see two options:
>
> - I implement a function which returns the desired predefined mapping, 
> according to the language of the user. How can I know this? How does the T 
> operator know this?
> - I walk the list of ISO country codes applying the T operator to return 
> the mapping.
>

Can't you just do:

ISO_COUNTRY_CODES_MAPPING = {
'DE': T('Germany'),
'ES': T('Spain'),
'IT': T('Italy'),
'US': T('United States')
} 

-- 





[web2py] Re: Map database values to display values

2012-06-25 Thread Daniel Gonzalez

On Monday, June 25, 2012 4:45:22 PM UTC+2, Anthony wrote:
>
> Check out the IS_IN_SET documentation at 
> http://web2py.com/books/default/chapter/29/7. Instead of a list of 
> individual items, it can be a list of two-tuples or a dictionary, where the 
> first tuple item or dictionary key is the value and the second tuple item 
> or dictionary value is the label to be displayed in the select widget.
>
> Anthony
>
>
Thanks, this worked. For othesr who might have the same problem, this is 
what I have done:

 ISO_COUNTRY_CODES_MAPPING = {
'DE': 'Germany',
'ES': 'Spain',
'IT': 'Italy',
'US': 'United States'
}
...
member.country.requires= IS_IN_SET(ISO_COUNTRY_CODES_MAPPING)

Now, to support internationalization here, I see two options:

- I implement a function which returns the desired predefined mapping, 
according to the language of the user. How can I know this? How does the T 
operator know this?
- I walk the list of ISO country codes applying the T operator to return 
the mapping.

Since it is not clear for me when this mapping will be created (at startup? 
in each request?) I do not understand the performance implications.
Maybe somebody can comment.

Thanks,
Daniel

-- 





[web2py] Re: Map database values to display values

2012-06-25 Thread Anthony
Check out the IS_IN_SET documentation at 
http://web2py.com/books/default/chapter/29/7. Instead of a list of 
individual items, it can be a list of two-tuples or a dictionary, where the 
first tuple item or dictionary key is the value and the second tuple item 
or dictionary value is the label to be displayed in the select widget.

Anthony

On Monday, June 25, 2012 10:08:36 AM UTC-4, Daniel Gonzalez wrote:
>
> Hello,
>
> I am extending the registration data with some custom fields (pretty 
> standard). This is my code:
>
> from gluon.tools import Service
> from gluon.tools import Auth
>
> db  = DAL('sqlite://storage.sqlite')
> auth= Auth(db)
> service = Service()
>
> db.define_table(
> auth.settings.table_user_name,
> Field('first_name',   length=128, default=''),
> Field('last_name',length=128, default=''),
> Field('email',length=128, default='', unique=True),
> Field('address',  length=256, default=''),
> Field('postcode', length=128, default=''),
> Field('city', length=128, default=''),
> Field('country',  length=128),
> Field('password', 'password', length=512, readable=False, 
> label='Password'),
> Field('registration_key', length=512, writable=False, 
> readable=False, default=''),
> Field('reset_password_key',   length=512, writable=False, 
> readable=False, default=''),
> Field('registration_id',  length=512, writable=False, 
> readable=False, default=''),
> format='%(first_name)s %(last_name)s')
>
> ISO_COUNTRY_CODES = ('DE', 'ES', 'IT', 'US')
>
> # get the custom_auth_table
> member = db[auth.settings.table_user_name]
> member.first_name.requires = 
> IS_NOT_EMPTY(error_message=auth.messages.is_empty)
> member.last_name.requires  = 
> IS_NOT_EMPTY(error_message=auth.messages.is_empty)
> member.password.requires   = [IS_STRONG(min=5, special=0, upper=0), 
> CRYPT()]
> member.email.requires  = 
> [IS_EMAIL(error_message=auth.messages.invalid_email), IS_NOT_IN_DB(db, 
> 'auth_user.email')]
> member.country.requires= IS_IN_SET(ISO_COUNTRY_CODES)
>
> auth.define_tables()
>
> The problem that I have is that in the registration form I am offered the 
> choice among DE, ES, IT, US ...
> I would like to convert those ISO country codes (I need that in my 
> database) to human readable contry names.
> Besides, this must be done according to the selected language (T operator?)
>
> Is it possible to tell the form serializer to map one of the fields?
> How can I specifiy the map function? Do you have an example to clarify the 
> syntax?
>
> Thanks,
> Daniel
>
>

--