要勇敢选择自己想要的生活
"""工厂模式"""
class ChineseGetter:
def __init__(self):
self.trans = dict(dog="狗", cat="猫",parrot="鹦鹉",bear="熊")
def get(self, msgid):
"""如果我们没有一个翻译,我们会把"""
return self.trans.get(msgid, str(msgid))
class EnglishGetter:
"""简单地输出了消息ID"""
def get(self, msgid):
return str(msgid)
def get_localizer(language="English"):
"""工厂方法"""
languages = dict(English=EnglishGetter, Chinese=ChineseGetter)
return languages[language]()
e, c= get_localizer(language="English"), get_localizer(language="Chinese")
for msgid in "dog parrot cat bear".split():
print("english:"+e.get(msgid),"中文:"+c.get(msgid))
运行如图:
