Skip to content

Instantly share code, notes, and snippets.

@atsuya046
Created February 2, 2014 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atsuya046/8768467 to your computer and use it in GitHub Desktop.
Save atsuya046/8768467 to your computer and use it in GitHub Desktop.
# -*- coding utf-8 -*-
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/"""
class JapaneseGetter:
"""A simple localizer a la gettext"""
def __init__(self):
self.trans = dict(dog="犬", cat="猫")
def get(self, msgid):
"""We'll punt if we don't have a translation"""
try:
return self.trans[msgid]
except KeyError:
return str(msgid)
class EnglishGetter:
"""Simply echoes the msg ids"""
def get(self, msgid):
return str(msgid)
def get_localizer(language="English"):
"""The factory method"""
languages = dict(English=EnglishGetter, Japanese=JapaneseGetter)
return languages[language]()
# Create our localizers
e, g = get_localizer("English"), get_localizer("Japanese")
for msgid in "dog parrot cat bear".split():
print(e.get(msgid), g.get(msgid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment