0

現在のロケールに応じて異なる言語に翻訳したい単純なプログラムを作成しましたが、動作しません。

私はさまざまなウェブサイトをフォローしていました。これはそれらの1つでした: Pythonの翻訳

ロケールが利用できず、「C」ロケールを使用する必要があることを取得する実行まで問題はありません

したがって、空き地文字列と空き地文字列の両方を含むポット コンパイル ファイルを取得できます。これは、ウェブサイトが私のプログラムに書くように言っていることです:

私がチェックしたことの 1 つは、os.environ.get が es_ES.UTF-8 のようなものを返し、コードが .UTF-8 なしでデフォルトを設定するように指示していることです。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import gi
from gi.repository import Gtk
import subprocess, sys, os
import threading
import gettext

# # Algunas cosas para gettext (para las traducciones)
APP_NAME="welcn"



WELCN_DIR = '/home/pruebas/welcn-0.1/'


class main():


    def __init__(self):



        #Translation stuff

        #Get the local directory since we are not installing anything
        self.local_path = os.path.realpath(os.path.dirname(sys.argv[0]))
        # Init the list of languages to support
        langs = []
        #Check the default locale
        lc, encoding = locale.getdefaultlocale()
        if (lc):
            #If we have a default, it's the first in the list
            langs = [lc]
        # Now lets get all of the supported languages on the system
        language = os.environ.get('LANG', None)
        if (language):
            """langage comes back something like en_CA:en_US:en_GB:en
            on linuxy systems, on Win32 it's nothing, so we need to
            split it up into a list"""
            langs += language.split(":")
        """Now add on to the back of the list the translations that we
        know that we have, our defaults"""
        langs += ["en_CA", "en_US"]

        """Now langs is a list of all of the languages that we are going
        to try to use.  First we check the default, then what the system
        told us, and finally the 'known' list"""

        gettext.bindtextdomain(APP_NAME, self.local_path)
        gettext.textdomain(APP_NAME)
        # Get the language to use
        self.lang = gettext.translation(APP_NAME, self.local_path
            , languages=langs, fallback = True)
        """Install the language, map _() (which we marked our
        strings to translate with) to self.lang.gettext() which will
        translate them."""
        _ = self.lang.gettext



        builder = Gtk.Builder()
        builder.add_from_file(WELCN_DIR + "welcn.ui")


        #### Language and Keymap window
        self.window = builder.get_object("window1")
        self.button_try = builder.get_object("button1")
        self.button_cli_installer = builder.get_object("button2")



        self.window.connect("delete-event", Gtk.main_quit)
        builder.connect_signals(self)
        self.window.set_title(_('Welcome!'))
        self.window.set_position(Gtk.WindowPosition.CENTER)


        self.window.show_all()

    def on_button1_clicked(self, widget, data=None):
        Gtk.main_quit()

    def on_button2_clicked(self, widget, data=None):
        subprocess.Popen(["cinnarch-setup"])
        sys.exit(0)





if __name__ == '__main__':
    main()
    Gtk.main()

私は何を間違っていますか?

4

1 に答える 1

2

import localeこのコード ファイルのどこかを実行しましたか?

が機能するためlocale.getdefaultlocale()には、明らかに最初にインポートする必要があります。

ipython シェルを使用してコードを実行する例:

In [7]: import locale

In [8]: locale.getdefaultlocale()
Out[8]: ('en_US', 'UTF-8')
于 2012-11-13T12:58:01.280 に答える