forked from faif/python-patterns
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory_method.py
More file actions
Latest commit
45 lines (31 loc) · 1022 Bytes
/
Copy pathfactory_method.py
File metadata and controls
45 lines (31 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/"""
classGreekGetter:
"""A simple localizer a la gettext"""
def__init__(self):
self.trans=dict(dog="σκύλος", cat="γάτα")
defget(self, msgid):
"""We'll punt if we don't have a translation"""
try:
returnself.trans[msgid]
exceptKeyError:
returnstr(msgid)
classEnglishGetter:
"""Simply echoes the msg ids"""
defget(self, msgid):
returnstr(msgid)
defget_localizer(language="English"):
"""The factory method"""
languages=dict(English=EnglishGetter, Greek=GreekGetter)
returnlanguages[language]()
# Create our localizers
e, g=get_localizer(language="English"), get_localizer(language="Greek")
# Localize some text
formsgidin"dog parrot cat bear".split():
print(e.get(msgid), g.get(msgid))
### OUTPUT ###
# dog σκύλος
# parrot parrot
# cat γάτα
# bear bear