Hướng dẫn python gettext change language - Python gettext thay đổi ngôn ngữ

POZZ

unread,

Ngày 15 tháng 6 năm 2017, 7:32:30 PM6/15/176/15/17

đến

Tôi biết tôi có thể tải nhiều gettext.translation:

it = getText.translation ('test', localedir = "locale", ngôn ngữ = ["it"]) es = getText.translation ('test', localedir = "locale", ngôn ngữ = ["es"])
es = gettext.translation('test', localedir="locale", languages=["es"])

và cài đặt một bản dịch trong thời gian chạy khi tôi muốn sau đó (khi người dùng chọn ngôn ngữ mới):
(when the user selects a new language):

it.install () hoặc es.install ()
or
es.install()

Tuy nhiên, vấn đề là các chuỗi đã được dịch không được dịch lại khi bản dịch mới được cài đặt. Vì vậy, họ ở lại ngôn ngữ được chọn trong khi khởi động và không thay đổi sau khi cài đặt mới ().
translated again when a new translation is installed. So they stay at
the language selected during start-up and don't change after a new
install().

Một giải pháp là khởi động lại ứng dụng, nhưng tôi nghĩ có một giải pháp tốt hơn và thanh lịch hơn.
and more elegant solution.

Peter Otten

unread,

Ngày 15 tháng 6 năm 2017, 9:23:06 PM6/15/176/15/17

đến

Tôi biết tôi có thể tải nhiều gettext.translation:
The documentation has a few ideas

it = getText.translation ('test', localedir = "locale", ngôn ngữ = ["it"]) es = getText.translation ('test', localedir = "locale", ngôn ngữ = ["es"])

và cài đặt một bản dịch trong thời gian chạy khi tôi muốn sau đó (khi người dùng chọn ngôn ngữ mới):
a custom class:

it.install () hoặc es.install ()

Tuy nhiên, vấn đề là các chuỗi đã được dịch không được dịch lại khi bản dịch mới được cài đặt. Vì vậy, họ ở lại ngôn ngữ được chọn trong khi khởi động và không thay đổi sau khi cài đặt mới ().
def __init__(self, message):
self.message = message
def __str__(self):
translate = _
if translate is DeferredTranslation:
return self.message
return translate(self.message)
@classmethod
def install(class_):
import builtins
builtins._ = class_

Một giải pháp là khởi động lại ứng dụng, nhưng tôi nghĩ có một giải pháp tốt hơn và thanh lịch hơn.

Peter Otten
print(message)

Ngày 15 tháng 6 năm 2017, 9:23:06 PM6/15/17
es = gettext.translation("test", localedir="locale", languages=["es"])

Bạn cần một cách để trì hoãn bản dịch cho đến khi chuỗi thực sự được sử dụng. Tài liệu có một vài ý tưởng
language.install()
print(message)

POZZ

unread,

https://docs.python.org/dev/l Library/getText.html#deferred-translations6/15/17

đến

Tôi biết tôi có thể tải nhiều gettext.translation:
> pozz wrote:
>
>> I know I can load multiple gettext.translation:
>>
>> it = gettext.translation('test', localedir="locale", languages=["it"])
>> es = gettext.translation('test', localedir="locale", languages=["es"])
>>
>> and install one translation at run-time when I want at a later time
>> (when the user selects a new language):
>>
>> it.install()
>> or
>> es.install()
>>
>>
>> However the problem is that strings already translated are not
>> translated again when a new translation is installed. So they stay at
>> the language selected during start-up and don't change after a new
>> install().
>>
>> One solution is to restart the application, but I think there's a better
>> and more elegant solution.
>
> You need a way to defer the translation until the string is actually used.
> The documentation has a few ideas
>
> https://docs.python.org/dev/library/gettext.html#deferred-translations
>
> and here's another one -- perform the translation in the __str__ method of
> a custom class:

it = getText.translation ('test', localedir = "locale", ngôn ngữ = ["it"]) es = getText.translation ('test', localedir = "locale", ngôn ngữ = ["es"])

và cài đặt một bản dịch trong thời gian chạy khi tôi muốn sau đó (khi người dùng chọn ngôn ngữ mới):
but a class. I think you can't use the class everywhere you can use a
string. For example, len() can't be called.

Peter Otten

unread,

Ngày 15 tháng 6 năm 2017, 9:23:06 PM6/15/176/16/17

đến

Tôi biết tôi có thể tải nhiều gettext.translation:

it = getText.translation ('test', localedir = "locale", ngôn ngữ = ["it"]) es = getText.translation ('test', localedir = "locale", ngôn ngữ = ["es"])
...
def __len__(self):
return len(str(self))

và cài đặt một bản dịch trong thời gian chạy khi tôi muốn sau đó (khi người dùng chọn ngôn ngữ mới):
methods for localized text. However, when you switch languages between the
len() and str() calls you will certainly make a mess...

it.install () hoặc es.install ()

unread,

Tuy nhiên, vấn đề là các chuỗi đã được dịch không được dịch lại khi bản dịch mới được cài đặt. Vì vậy, họ ở lại ngôn ngữ được chọn trong khi khởi động và không thay đổi sau khi cài đặt mới ().6/16/17

đến

Một giải pháp là khởi động lại ứng dụng, nhưng tôi nghĩ có một giải pháp tốt hơn và thanh lịch hơn.
> I know I can load multiple gettext.translation:
>
> it = gettext.translation('test', localedir="locale", languages=["it"])
> es = gettext.translation('test', localedir="locale", languages=["es"])
>
> and install one translation at run-time when I want at a later time
> (when the user selects a new language):
>
> it.install()
> or
> es.install()
>
>
> However the problem is that strings already translated are not
> translated again when a new translation is installed. So they stay at
> the language selected during start-up and don't change after a new
> install().

Peter Otten
"Zope". There, it is completely natural, that the individual request
determines the target language.

Ngày 15 tháng 6 năm 2017, 9:23:06 PM6/15/17
by so called "message_id"s. They behave somehow as unicode objects, but,
of course, they are more complex; especially, they may encapsulate parameters
to be incorparated in the translation.
When a "message_id" is localized, the target language is taken from
the context (i.e. the current request). It returns a unicode string
but does not change the "message_id".