擅长:python、mysql、java
<p>下面的示例直接使用<code>translation</code>,如<a href="https://stackoverflow.com/users/1405588/o11c">o11c's</a><a href="https://stackoverflow.com/a/38004947/3657941">answer</a>所示,以允许使用线程:</p>
<pre><code>import gettext
import threading
import time
def translation_function(quit_flag, language):
lang = gettext.translation('simple', localedir='locale', languages=[language])
while not quit_flag.is_set():
print(lang.gettext("Running translator"), ": %s" % language)
time.sleep(1.0)
if __name__ == '__main__':
thread_list = list()
quit_flag = threading.Event()
try:
for lang in ['en', 'fr', 'de']:
t = threading.Thread(target=translation_function, args=(quit_flag, lang,))
t.daemon = True
t.start()
thread_list.append(t)
while True:
time.sleep(1.0)
except KeyboardInterrupt:
quit_flag.set()
for t in thread_list:
t.join()
</code></pre>
<p><strong>输出:</strong></p>
^{pr2}$
<p>如果我知道更多关于<code>gettext</code>的信息,我会发布这个答案的。我把我之前的答案留给那些真正想继续使用<code>_()</code>的人。在</p>