python脚本中的多语言支持 [英] multi language support in python script
问题描述
我有一个 large python (2.7) 脚本,它从数据库中读取数据并生成 pdf 格式的图片.我的图片有标签字符串等......
I have a large python (2.7) script that reads data from a database and generate pictures in pdf format. My pictures have strings for labels, etc...
现在我想为脚本添加多语言支持,这样我就可以通过向我的脚本传递一个变量来生成不同语言的相同图片.
Now I want to add a multi language support for the script, so I can generate the same pictures in different languages by passing a variable to my script.
我想开设一个课程,其中包含字典中的所有字符串及其翻译.例如:
I was thinking in having a class with all the strings and their translations in a dictionary. For example:
Strings['string1'] = {'de':'string in german', 'en': 'string in english'}
我可以访问字符串
my_needed_string = 'string1'
selected_language = 'en'
Strings[my_needed_string][selected_language]
有没有更好、更专业的方法来做到这一点?更好"是指更灵活、更易于维护?我至少有 80 个不同的字符串,有 2 种或更多语言.
is there a better, more professional way to do this? with "better" I mean more flexible and easier to maintain? I have at least 80 different strings in 2 or more languages.
谢谢
推荐答案
参见 python gettext i18n 支持模块
see python gettext module for i18n support
这篇关于python脚本中的多语言支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!