Ticket #7980: import_cldr.py

File import_cldr.py, 2.8 KB (added by Marc Garcia, 16 years ago)

First draft of a script for importing data from CLDR to django

Line 
1import sys, os
2
3try:
4 from lxml.etree import parse
5except ImportError:
6 print 'You need to install `python-lxml` to run this script'
7 sys.exit(1)
8
9convert_time = lambda x: x
10
11# Output structure definition
12output_structure = {
13 "LC_TIME": {
14 "d_t_fmt": {
15 "pattern" : "/ldml/dates/calendars/calendar[@type='gregorian']/dateFormats/dateFormatLength[@type='long']/dateFormat/pattern",
16 "conversion": convert_time,
17 "xml_position": 'text',
18 },
19 "d_fmt": {
20 "pattern" : "/ldml/dates/calendars/calendar[@type='gregorian']/dateFormats/dateFormatLength[@type='medium']/dateFormat/pattern",
21 "conversion": convert_time,
22 "xml_position": 'text',
23 },
24 "t_fmt": {
25 "pattern" : "/ldml/dates/calendars/calendar[@type='gregorian']/timeFormats/timeFormatLength[@type='medium']/timeFormat/pattern",
26 "conversion": convert_time,
27 "xml_position": 'text',
28 },
29 "first_weekday": {},
30 }
31
32}
33
34def main(cldr_dir):
35 """
36 For every locale defined in Django, get from the CLDR locale file all
37 settings defined in output_structure, and write the result to the
38 locale directories on Django.
39 """
40 locale_dir = os.path.join('..', 'conf', 'locale')
41 for locale_name in os.listdir(locale_dir):
42 locale_file = os.path.join(cldr_dir, locale_name + '.xml')
43 if os.path.isfile(locale_file):
44 tree = parse(locale_file)
45 result = {}
46 for locale_category, keys in output_structure.iteritems():
47 for key, opts in keys.iteritems():
48 if opts.get('pattern', None):
49 value = tree.xpath(opts['pattern'])
50 if len(value):
51 value = getattr(value[0], opts.get('xml_position', 'text'))
52 value = opts.get('conversion', lambda x: x)(value)
53 else:
54 value = None
55 else:
56 value = None
57 result[key] = value or u''
58
59 # output the result to the destination file
60 output_dir = os.path.join(locale_dir, locale_name, locale_category)
61 if not os.path.isdir(output_dir):
62 os.mkdir(output_dir)
63 output_file = open(os.path.join(output_dir, 'django.po'), 'w')
64 for msgstr, msgid in result.iteritems():
65 output_file.write('msgstr "%s"\nmsgid "%s"\n\n' % (msgstr, msgid.encode('utf-8')))
66
67 else:
68 print 'Omitting "%s" locale' % locale_name
69
70if __name__ == '__main__':
71 if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]):
72 main(sys.argv[1])
73 else:
74 print 'Usage: %s <cldr_main_dir>' % sys.argv[0]
75
Back to Top