开发者

how to use a non-european language with a python library

I'm relatively new to programming and recently I've started playing around with pygame (set of modules to write games in python). I'm lo开发者_运维技巧oking to create a program/game in which some of the labels, strings, buttons, etc are in Arabic. I'm guessing that pygame has to support Arabic letters and it probably doesn't? Or could I possibly use another GUI library that does support Arabic and use that in unison with pygame? Any direction would be much appreciated!


Well Python itself uses Unicode for everything so that's not the problem. A quick googling also shows that PyGame should be able to render Unicode fonts just fine. So I assume the problem is more that it can't find fonts for the specific language to use for rendering.

Here is a short example for PyGame and especially this link should be useful.

This is the important library - so specifying a font that can render your language and using it to render it should work fine. Probably a good idea to write a small wrapper

Nb: Haven't used PyGame myself so this is based on speculation and some quick search about how PyGame renders fonts.

PS: If you want the game to work reliably for all of your users, it's probably a good idea to include an Open Source font in your release, otherwise you need some methodology to check if the user has some fonts installed that will work fine - a probably non-trivial problem if you want Xplattform support.


Python does support unicode-coded source.

Set the coding of your source file to the right type with a line of the form # coding: [yourCoding] at the very beginning of your file. I think # coding: utf-8 works for Arabic.

Then prepend your string literals with a u, like so:

u'アク'

(Sorry if you don't have a Japanese font installed, it's the only one I had handy!)

This makes python treat them as unicode characters. There's further information specific to Arabic on this site.


Both previous answers are great. There is also a great built-in python function called unicode. It's very easy to use. I wanted to write Hebrew text so I wrote a function:

def hebrew(text):
    # Encoding that supports hebrew + punctuation marks
    return unicode(text,  "Windows-1255")

Then you can call it using:

hebrew("<Hebrew text>")

And it will return your text Hebrew encoded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜