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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
=========
Languages
=========
Mailman is multilingual. A language manager handles the known set of
languages at run time, as well as enabling those languages for use in a
running Mailman instance.
::
>>> from mailman.interfaces.languages import ILanguageManager
>>> from zope.component import getUtility
>>> from zope.interface.verify import verifyObject
>>> mgr = getUtility(ILanguageManager)
>>> verifyObject(ILanguageManager, mgr)
True
# Make a copy of the language manager's dictionary, so we can restore it
# after the test. Currently the test layer doesn't manage this.
>>> saved = mgr._languages.copy()
# The language manager component comes pre-populated; clear it out.
>>> mgr.clear()
A language manager keeps track of the languages it knows about.
>>> list(mgr.codes)
[]
>>> list(mgr.languages)
[]
Adding languages
================
Adding a new language requires three pieces of information, the 2-character
language code, the English description of the language, and the character set
used by the language. The language object is returned.
>>> mgr.add('en', 'us-ascii', 'English')
<Language [en] English>
>>> mgr.add('it', 'iso-8859-1', 'Italian')
<Language [it] Italian>
And you can get information for all known languages.
>>> print(mgr['en'].description)
English
>>> print(mgr['en'].charset)
us-ascii
>>> print(mgr['it'].description)
Italian
>>> print(mgr['it'].charset)
iso-8859-1
Other iterations
================
You can iterate over all the known language codes.
>>> mgr.add('pl', 'iso-8859-2', 'Polish')
<Language [pl] Polish>
>>> sorted(mgr.codes)
['en', 'it', 'pl']
You can iterate over all the known languages.
>>> from operator import attrgetter
>>> languages = sorted((language for language in mgr.languages),
... key=attrgetter('code'))
>>> for language in languages:
... print(language.code, language.charset, language.description)
en us-ascii English
it iso-8859-1 Italian
pl iso-8859-2 Polish
You can ask whether a particular language code is known.
>>> 'it' in mgr
True
>>> 'xx' in mgr
False
You can get a particular language by its code.
>>> print(mgr['it'].description)
Italian
>>> print(mgr['xx'].code)
Traceback (most recent call last):
...
KeyError: 'xx'
>>> print(mgr.get('it').description)
Italian
>>> print(mgr.get('xx'))
None
>>> print(mgr.get('xx', 'missing'))
missing
Clearing the known languages
============================
The language manager can forget about all the language codes it knows about.
::
>>> 'en' in mgr
True
>>> mgr.clear()
>>> 'en' in mgr
False
# Restore the data.
>>> mgr._languages = saved
|