我可以在库和使用它的模块中同时调用和设置 Python gettext 模块吗?
我正在编写一个库,其中包括我需要翻译的文本反馈。
我将以下行放入 _config.py
模块中,并在应用程序中的任何位置导入该模块:
import gettext, os, sys
pathname = os.path.dirname(sys.argv[0])
localdir = os.path.abspath(pathname) + "/locale"
gettext.install("messages", localdir)
我在 ./locale/lang_LANG/ 中有
和我将 *.mo
文件LC_MESSAGES_()
函数应用于所有需要翻译的字符串。
现在,我刚刚为用户(假设是程序员)添加了一项功能,以便能够创建自己的消息。 我不希望他关心底层实现,所以我希望他能够使其变得简单,例如:
lib_object.message = "My message"
我使用属性来使其干净,但是如果我的用户要翻译他自己的代码(使用我的代码)怎么办? )并执行以下操作:
import gettext, os, sys
pathname = os.path.dirname(sys.argv[0])
localdir = os.path.abspath(pathname) + "/locale"
gettext.install("user_app", localdir)
lib_object.message = _("My message")
这是一个问题吗? 我该怎么做才能避免麻烦而不打扰我的用户?
Im a coding a library including textual feedback that I need to translate.
I put the following lines in a _config.py
module that I import everywhere in my app :
import gettext, os, sys
pathname = os.path.dirname(sys.argv[0])
localdir = os.path.abspath(pathname) + "/locale"
gettext.install("messages", localdir)
I have the *.mo
files in ./locale/lang_LANG/LC_MESSAGES
and I apply the _()
function to all the strings that need to be translated.
Now I just added a feature for the user, supposedly a programmer, to be able to create his own messages. I don't want him to care about the underlying implementation, so I want him to be able to make it something straightforward like :
lib_object.message = "My message"
I used properties to make it clean, but what if my user whats to translate his own code (that uses mine) and does something like :
import gettext, os, sys
pathname = os.path.dirname(sys.argv[0])
localdir = os.path.abspath(pathname) + "/locale"
gettext.install("user_app", localdir)
lib_object.message = _("My message")
Is it a problem ? What can I do to avoid troubles without bothering my user ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用基于类的 gettext api 来隔离消息目录。 这也是 python gettext 文档 中推荐的内容。
缺点是您或其他开发人员必须使用 gettext 方法或在本地范围内定义绑定到特定 gettext 类的
_()
方法。 具有自己的字符串目录的类的示例:您可以将添加
_()
的代码粘贴到装饰器中,因此所有需要它的方法都以@with_local_gettext 等前缀
(注意,我没有测试过上面的方法,但它应该工作得很好(tm))
如果目标是不打扰你的用户(而且他不是很好)我想你可以使用基于类的方法在您的代码中并让用户使用全局代码。
You can use the class based gettext api to isolate message catalogs. This is also what is recommended in the python gettext documentation.
The drawback is that you, or the other dev, will have to use the gettext method or define the
_()
method in the local scope, bound to the specific gettext class. An example of a class with its own string catalog:You could stick the code for adding the
_()
in a decorator, so all the methods that need it is prefixed with something like@with_local_gettext
(Note, I've not tested the above could but It Should Work Just Fine(tm) )
If the goal is to not bother your user (and he's not very good) I guess you could use the class based approach in your code and let the user use the global one.
您只能 gettext.install() 一次。 一般来说,它对于库工作是没有用的——只有当调用它的模块负责整个程序时, gettext.install() 才会做正确的事情,因为它只会为您提供一个可供加载的目录。 库代码应该做一些类似于 Mailman 所做的事情:有自己的 gettext() 包装器,为该模块传递正确的参数,然后在每个想要使用它的模块中将其导入为“_”。
You can only gettext.install() once. In general it's useless for library work -- gettext.install() will only do the right thing if the module calling it is in charge of the whole program, since it will only provide you with one catalog to load from. Library code should do something akin to what Mailman does: have their own wrapper for gettext() that passes the right arguments for this module, then imports that as '_' in each module that wants to use it.