多语言字符串
mls的Python项目详细描述
这个简单的模块实现了简单的类unicode对象,它可以包含 一个字符串的多语言变异。实际上它是Unicode的子类 type(对于python 2k)/string type(对于py3k)和一些帮助方法, 它允许您将字符串转换为其他语言。
在这里,我将向您展示一些用法示例:
fromlocaleimportsetlocale,LC_ALLfrommlsimportmlssetlocale(LC_ALL,"en_US.UTF-8")# Our system locale will be US englishempty=mls()# same as unicode, butprint(empty.language)# with .language attribute containing "en"s=mls("Hello, world!")t=s.translate_to("ru_RU",u"Здравствуй, мир!")# generate new ru-mutationprint(t.language)# => "ru"print(t)# => "Здравствуй, мир!"x=mls("Ahoj",language="cs")# czech mutation of "Hi"x<<="Nazdar"# Change translation to anotherprint(repr(x))# => "cs'Nazdar'"a=mls({"en":"Bye","ru":u"Пока","cs":u"Čau"})print(a)# => "Bye"b=a.translate_to("ru")print(b)# => "Пока"c=a>>"cs"print(c)# => "Čau"v=mls(en="Vodka",pl=u"Wódka",ru=u"Водка")print(repr(v>>"pl"))# => "pl'W\xf3dka'"print(repr(v>>"fr"))# => "fr'Vodka'" (no translation to french)# Also you can use any unicode/str methodsprint("world"ins)# => Trueprint(s[:5])# => "Hello"