您无法提取变量,因为在_(some_variable)
some_variable
运行时查找表达式。如果您从静态值列表中获取,则将提取some_variable
静态值。例如:
COLORS_OF_MAGIC = [_("green"), _("red"), _("blue"), _("white"), _("black")]
def color_for_index(index):
if not (0 < index > len(COLORS_OF_MAGIC)):
index = 0
return COLORS_OF_MAGIC[index]
def do_work():
index = get_index_from_user()
some_variable = color_for_index(index)
return _("You chose ") + _(some_variable) + _(" as the color of magic")
在 PO 文件中将具有以下值:green
、red
、blue
、white
、black
、You chose
和。as the color of magic
另一方面,如果您尝试翻译用户提供的字符串,那么您将需要另一个解决方案,因为 Babel 是一个静态翻译服务。