0

我正在尝试翻译视频游戏https://github.com/mdtrooper/Obb,但 gettext 方法 _("string") 返回一个非 unicode 字符串。

好吧,我做了一个简单的例子来说明问题:

该文件是utf8编码:

# -*- coding: utf-8 -*-

import pygame, os

import locale
import gettext

gettext.textdomain('caca')
_ = gettext.gettext

def main():
    path_i18n = os.getcwd() + '/i18n/'
    gettext.bindtextdomain('caca', path_i18n)

    pygame.init()
    screen = pygame.display.set_mode((640, 480), 0, 32)
    screen.fill(pygame.Color(255, 255, 255, 255))

    font = pygame.font.Font("Isuckatgolf.ttf", 30)

    text = font.render(_("caca"), True, pygame.Color(0, 0, 0, 255))
    screen.blit(text, (0,0))

    unstraslate_text = font.render(u"ñadu", True, pygame.Color(0, 0, 0, 255))
    screen.blit(unstraslate_text, (100, 100))

    pygame.display.update()

    exit = False

    while not exit:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                exit = True
            elif e.type == pygame.KEYDOWN:
                if e.key == pygame.K_ESCAPE:
                    exit = True 

    pygame.quit()

if __name__ == '__main__':

这是西班牙语的 po 文件:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-03-30 14:06+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: UTF-8\n"

#: test.py:19
msgid "caca"
msgstr "ñandu"

当然字体没有 Ñ 或 ñ 字符但我已修改为添加此字符,您可以在http://depositfiles.com/files/dwwmp912m中找到

例如,第一个字符串(翻译后的字符串)显示不好,但第二个显示好。

4

1 回答 1

0

我已经找到。

我已经在其他文件(i18n.py)中编写了代码:

# -*- coding: utf-8 -*-

import locale
import gettext
import os

path_i18n = os.getcwd() + '/i18n/'

t = gettext.translation('Obb', path_i18n)
_ = t.ugettext

但问题是python抛出错误:

NameError: global name '_' is not defined

当我尝试用作:

from i18n import *

但是使用代码:

from i18n import _

运行良好。

于 2013-03-31T15:36:53.743 回答