Python Statements/Keyword Localization

MRAB python at mrabarnett.plus.com
Wed Nov 25 15:26:34 EST 2009


Emanuele D'Arrigo wrote:
> Greetings everybody,
> 
> some time ago I saw a paper that used an XSL transformation sheet to
> transform (if I remember correctly) a Chinese xml file (inclusive of
> Chinese-script XML tags) into an XHTML file.
> 
> More recently you might have all heard how the ICANN has opened up the
> way for non-latin characters in domain names, so that we'll soon start
> seeing URLs using Russian, Asian and Arabic characters.
> 
> In this context I was wondering if there has ever been much thought
> about a mechanism to allow the localization not only of the strings
> handled by python but also of its built-in keywords, such as "if",
> "for", "while", "class" and so on. For example, the following English-
> based piece of code:
> 
> class MyClass(object):
>     def myMethod(self, aVariable):
>          if aVariable == True:
>             print "It's True!"
>          else:
>             print "It's False!"
> 
> would become (in Italian):
> 
> classe LaMiaClasse(oggetto):
>     def ilMioMetodo(io, unaVariabile)
>          se unaVariabile == Vero:
>              stampa "E' Vero!"
>          altrimenti:
>              stampa "E' Falso!"
> 
> I can imagine how a translation script going through the source code
> could do a 1:1 keyword translation to English fairly quickly but this
> would mean that the runtime code still is in English and any error
> message would be in English. I can also imagine that it should be
> possible to "simply" recompile python to use different keywords, but
> then all libraries using the English keywords would become
> incompatible, wouldn't they?
> 
> In this context it seems to be the case that the executable would have
> to be able to optionally accept -a list- of dictionaries to internally
> translate to English the keywords found in the input code and at most -
> one- dictionary to internally translate from English output messages
> such as a stack trace.
> 
> What do you guys think?
> 
It might be necessary to work in tokens, where a token is a word or a
string (or maybe also a comment). Your example would be encoded to:

     «1» «2»(«3»):
         «4» «5»(«6», «7»):
             «8» «7» == «9»:
                 «10» «11»
             «12»:
                 «10» «13»

with either English:

     «1» class
     «2» MyClass
     «3» object
     «4» def
     «5» myMethod
     «6» self
     «7» aVariable
     «8» if
     «9» True
     «10» print
     «11» "It's True!"
     «12» else
     «13» "It's False!"

or Italian:

     «1» classe
     «2» LaMiaClasse
     «3» oggetto
     «4» def
     «5» ilMioMetodo
     «6» io
     «7» unaVariabile
     «8» se
     «9» Vero
     «10» stampa
     «11» "É Vero!"
     «12» altrimenti
     «13» "É Falso!"

Any messages produced by, or format strings used by, the runtime would
also be tokens.

Python currently does lexical analysis on the source code to identify
names, strings, etc; a new tokenised file format would partially bypass
that because the names and strings (and comments?) have already been
identified.



More information about the Python-list mailing list