[Tutor] Why difference between printing string & typing its object reference at the prompt?

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Oct 18 20:22:40 CEST 2012


David Hutto wrote:
> If your app has  a standard usage of phrases, you can place a file in
> that translates a tag into a particular language phrase.
> 
> 
> 
> if submit_tag_selection == 'english':
>      submit = 'Submit'
> if submit_tag_selection == 'english':
>      submit = 'Soumettre'
> 
> Of course this could be done without the if, you would just translate
> the normal selections within a file with the commonly used phrases in
> the app, and substitute it within a parse for:
> 
> x = open('translate_file_french', 'r')
> for line in x:
>      if line.split('=')[0] == 'Submit':
>            print '%s'   %   (line.split('=')[1])
> 
> 'Soumettre'
> 
> *Untested, but should work
> 

Now missing any context I am going to assume the topic shifted to
how to do translations for a internationalized application/site. 
Feel free to ignore if I am wrong or OT.

I would do this, but not using line splitting. I would 
create a (YAML) config files that contain translations of
site text (i.e. "Submit"). You could do this with pickle too,
but I think YAML files are better for humans to edit.

text = '....<SUBMIT_BUTTON_TEXT>....'
with open('translate_file_fr') as f:
   # parse YAML into dictionary of { text_to_replace : text_to_replace_with }
   
# <some work>
for k,v in translation_key.iteritems():
    text = text.replace(k, v)

Alternately you could create a python module and just import
the appropriate language.

translation_key = __import__('translation.' + language ) 
text = ''join([ '...', translation_key.submit_button_text, '...'])


Of course, I have no idea the downsides of this approach as I 
have not had to do something like this before. I would be
interested in whether this matches the standard approach
and the up/down-sides to it.

Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list