views:

45

answers:

4

Hello,

I'm a beginner in Python. My problem is pretty simple. I have a string to be localized in a python application containing parameters :

print _('Hello dear user, your name is ') + params['first_name'] + ' ' + params['last_name'] + _(' and blah blah blah')

This actually does the job, but is not really what I would call a nice way to do it. Not to mention that some languages would, for example, require the last name to be displayed before the first name.

Is there a better way to do it ? I thought about placing custom tags like {{fn}} or {{ln}} in the translation string and replacing them by the actual values before displaying the string. But it seems not to be really more pleasant.

Thanks,

Pierre

A: 

I thought about placing custom tags like {{fn}} or {{ln}} in the translation string and replacing them by the actual values before displaying them.

That's what I would do. Placeholders in the right places for each language versions should do the job.

One thing to mention that in some languages peoples names have to be modified dependent on where in a sentence and how they are used. You need to know each specific language to be able to do it correctly.

A possible solution: keep "in the middle of a sentence" cases to a minimum. Keep a localizable resource separated.

Instead of Hello dear user, your name is {{UserName}}

use User name: {{UserName}}

Developer Art
Thank you for your precision. I'll take care of your advice.But sometimes, when clients require things like that to be done, I'm a good boy and I just do it :)And some other times, you don't really have the choice. When generating HTML with links to dynamic URIs in the middle of the text, for example.
Pierre
+1  A: 

Something like this should do the trick :

print _('Hello dear user, your name is %s %s and blah blah blah') % (params['first_name'], params['last_name'])
gregseth
This will fail miserably if the order of the arguments need to be changed.
Ignacio Vazquez-Abrams
Just tried the solution. Indeed, I can't change the order of the strings. But thank you for the solution. I'll be using it in some other parts of the application.
Pierre
+3  A: 

I'd suggest

print 'Hello dear user, your name is %(first_name)s %(last_name)s' % params
jellybean
Does the job, as expected. And it seems more "pythonic" that my solution. I'm glad I won't have to rot in hell next time I'll have to show my code to another developer. :)
Pierre
+1  A: 

I would go with templates if I were you. That would let you have a separate template for each language. For example:

from string import Template
s_en = Template('Hello dear user, your name is $first_name $last_name and blah blah blah')
s_sco = Template('Hello, $first_name of the clan Mac$last_name...')

user = {'last_name': 'Duncan', 'first_name': 'Leod'}

print(s_en.substitute(user))
print(s_sco.substitute(user))
gc
Yep, thank you. I've never heard of that solution, and it will be useful, as well.But in my case, since 'templates' are already provided by the i18n module and the gettext function and does the trick, I don't really understand the gain.
Pierre