![]() You won't be able to run this program since the _() function is undefined. When you've gone through the "Guess the Number" source code, it will look like this. Print(_('Good job, %s! You guessed my number in %s guesses!') % (myName, guessesTaken)) This results in three separate strings that need to be translated, as opposed to the single string needed in the string formatting approach: print('Good job, %s! You guessed my number in %s guesses!' % (myName, guessesTaken)) Print(_('Good job, ') + myName + _('! You guessed my number in ') + guessesTaken + _(' guesses!')) For example, using string concatenation your program would have to look like this: print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!') Note that using string formatting instead of string concatenation will make your program easier to translate. The gettext system for Python uses _() as the generic name for getting the translated string since it is a short name. py file's source code again to import the gettext module and set up the language setting.įirst, go through all of the strings in your program that will need to be translated and replace them with _() calls. Use the free cross-platform Poedit software to create the.Use the pygettext.py script that comes installed with Python to create a "pot" file from the source code.py file's source code so that the strings are passed to a function named _(). There are four steps to internationalizing this program: Say you have a simple "Guess the Number" game written in Python 3 that you want to translate. gettext was designed as a system for all programming languages, but we'll focus on Python in this article. gettext is a set of tools and file formats created in the early 1990s to standardize software internationalization (also called I18N). This is pretty much what Python's gettext module does. This would work, but you'd be reinventing the wheel. For example, if the language setting was stored in a global variable named LANGUAGE, the _() function could look like this: def _(s): and the _() function could return the translation for 'Hello world!' based on what language setting the program had. you could change this to: print(_('Hello world!')) ![]() For example, if your program was: print('Hello world!') For example, you could replace every string in your program with a function call (with the function name being something simple, like _())) which will return the string translated into the correct language. You could hack together your own solution. And if you want your program in other languages, it gets even worse.įortunately, Python provides a solution with the gettext module. But this would mean you have two separate copies of your code, which doubles your workload every time you need to make a change or fix a bug. py file and replace any text strings you find. You could duplicate the entire code-base, then go painstakingly through each. You've written a Python 3 program and want to make it available in other languages. ![]()
0 Comments
Leave a Reply. |