Rub*_*era 12 python multilingual docstring internationalization
我想为自己创建一个新模块,但我也想让一些同事能够使用它.我开始用英语编写我的文档字符串,但后来我意识到,对于那些不太了解这种语言的人来说,它会使模块变得无用.
我的第一个想法是键入英语和同一文档字符串,西班牙语.但这似乎不对,如果我想让一些俄罗斯朋友也使用它呢?如果我有朋友在世界各地有朋友没有任何共同语言阅读文档怎么办?
在多种语言中编写然后阅读文档字符串的最简单方法是什么?
小智 5
我遇到过同样的问题; 有点:该cmd
模块使用文档字符串向最终用户打印帮助,我确实需要一种方法来使用多种语言的文档字符串。我是这样做的:
看看这个很棒的教程关于使用 gettext 模块的这使您能够翻译任何 Python 应用程序。我这样使用它:
\n\nimport gettext\ntry:\n lang = gettext.translation(\'myawesomeapp\', localedir=\'locale\')\n lang.install()\nexcept FileNotFoundError:\n _ = lambda x: x\n
Run Code Online (Sandbox Code Playgroud)\n\n现在,每当您想要国际化文档字符串时,请遵循以下模式:
\n\nclass MyAwesomeClass:\n def incredible_method(with_fantastic_args):\n # Some marvellous code\n\n incredible_method.__doc__ = _(\'\'\'\\\nPlace here a fabulous docstrig for your incredible method.\nThis will be translated by the gettext module at runtime.\'\'\')\n
Run Code Online (Sandbox Code Playgroud)\n\n现在是时候阅读我之前提到的教程了:调用pygettext
您的代码,用于poedit
创建翻译,并享受乐趣。
Adi\xc3\xb3s,paisanos。
\n