~ubuntu-langpack/langpack-o-matic/main

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
#!/usr/bin/python3

# this is part of langpack-o-matic, by Martin Pitt <martin.pitt@canonical.com>
#
# (C) 2006 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@canonical.com>
#
# Update maps/{languages,countries,scripts} from currently installed iso-codes package.

import xml.dom.minidom
import sys

assert sys.version >= '3', 'needs python3'

def write_sorted_dict(filename, dict):
    f = open(filename, 'w')
    for k in sorted(dict.keys()):
        f.write('%s:%s\n' % (k, dict[k]))

# ISO-639: List of languages, create maps/languages
iso639 = {}
dom = xml.dom.minidom.parse('/usr/share/xml/iso-codes/iso_639.xml')
for entry in dom.getElementsByTagName('iso_639_entry'):
    x = entry.attributes.get('iso_639_1_code', entry.attributes['iso_639_2B_code']).nodeValue
    iso639[entry.attributes.get('iso_639_1_code',
        entry.attributes['iso_639_2B_code']).nodeValue] = \
        entry.attributes['name'].nodeValue

dom = xml.dom.minidom.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
for entry in dom.getElementsByTagName('iso_639_3_entry'):
    iso639[entry.attributes.get('id',).nodeValue] = \
        entry.attributes['name'].nodeValue

write_sorted_dict('maps/languages', iso639)

# ISO-3166: List of countries, create maps/countries
iso3166 = {}
dom = xml.dom.minidom.parse('/usr/share/xml/iso-codes/iso_3166.xml')
for entry in dom.getElementsByTagName('iso_3166_entry'):
    iso3166[entry.attributes['alpha_2_code'].nodeValue] = \
        entry.attributes['name'].nodeValue

write_sorted_dict('maps/countries', iso3166)

# ISO-15924: List of scripts, create maps/scripts
iso15924 = {}
dom = xml.dom.minidom.parse('/usr/share/xml/iso-codes/iso_15924.xml')
for entry in dom.getElementsByTagName('iso_15924_entry'):
    iso15924[entry.attributes['alpha_4_code'].nodeValue] = \
        entry.attributes['name'].nodeValue

write_sorted_dict('maps/scripts', iso15924)

# supported locales
with open('/usr/share/i18n/SUPPORTED') as src:
    with open('maps/supported-locales', 'w') as dest:
        for l in src:
            if l.startswith('#'):
                continue
            fields = l.split()
            if len(fields) == 2 and fields[1] == 'UTF-8':
                dest.write(l)