comparison scripts/import_cldr.py @ 392:34c0a25b1ed7

Preliminary support for timedelta formatting (see #126), and import/expose the locale plural rules from the CLDR.
author cmlenz
date Mon, 14 Jul 2008 22:13:44 +0000
parents 88e3589ca8df
children 17515f1efee0
comparison
equal deleted inserted replaced
390:d21f92883d82 392:34c0a25b1ed7
25 25
26 # Make sure we're using Babel source, and not some previously installed version 26 # Make sure we're using Babel source, and not some previously installed version
27 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..')) 27 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..'))
28 28
29 from babel import dates, numbers 29 from babel import dates, numbers
30 from babel.plural import PluralRule
30 from babel.localedata import Alias 31 from babel.localedata import Alias
31 32
32 weekdays = {'mon': 0, 'tue': 1, 'wed': 2, 'thu': 3, 'fri': 4, 'sat': 5, 33 weekdays = {'mon': 0, 'tue': 1, 'wed': 2, 'thu': 3, 'fri': 4, 'sat': 5,
33 'sun': 6} 34 'sun': 6}
34 35
129 containers = territory_containment.setdefault(territory, set([])) 130 containers = territory_containment.setdefault(territory, set([]))
130 if group in territory_containment: 131 if group in territory_containment:
131 containers |= territory_containment[group] 132 containers |= territory_containment[group]
132 containers.add(group) 133 containers.add(group)
133 134
135 # prepare the per-locale plural rules definitions
136 plural_rules = {}
137 prsup = parse(os.path.join(srcdir, 'supplemental', 'plurals.xml'))
138 for elem in prsup.findall('//plurals/pluralRules'):
139 rules = []
140 for rule in elem.findall('pluralRule'):
141 rules.append((rule.attrib['count'], unicode(rule.text)))
142 pr = PluralRule(rules)
143 for locale in elem.attrib['locales'].split():
144 plural_rules[locale] = pr
145
134 filenames = os.listdir(os.path.join(srcdir, 'main')) 146 filenames = os.listdir(os.path.join(srcdir, 'main'))
135 filenames.remove('root.xml') 147 filenames.remove('root.xml')
136 filenames.sort(lambda a,b: len(a)-len(b)) 148 filenames.sort(lambda a,b: len(a)-len(b))
137 filenames.insert(0, 'root.xml') 149 filenames.insert(0, 'root.xml')
138 150
158 else: 170 else:
159 territory = '001' # world 171 territory = '001' # world
160 print>>sys.stderr, ' Territory: %r' % territory 172 print>>sys.stderr, ' Territory: %r' % territory
161 regions = territory_containment.get(territory, []) 173 regions = territory_containment.get(territory, [])
162 print>>sys.stderr, ' Regions: %r' % regions 174 print>>sys.stderr, ' Regions: %r' % regions
175
176 # plural rules
177 locale_id = '_'.join(filter(None, [
178 language,
179 territory != '001' and territory or None
180 ]))
181 if locale_id in plural_rules:
182 data['plural_form'] = plural_rules[locale_id]
163 183
164 # <localeDisplayNames> 184 # <localeDisplayNames>
165 185
166 territories = data.setdefault('territories', {}) 186 territories = data.setdefault('territories', {})
167 for elem in tree.findall('//territories/territory'): 187 for elem in tree.findall('//territories/territory'):
451 symbol = elem.find('symbol') 471 symbol = elem.find('symbol')
452 if symbol is not None and 'draft' not in symbol.attrib \ 472 if symbol is not None and 'draft' not in symbol.attrib \
453 and 'choice' not in symbol.attrib: 473 and 'choice' not in symbol.attrib:
454 currency_symbols[code] = unicode(symbol.text) 474 currency_symbols[code] = unicode(symbol.text)
455 475
476 # <units>
477
478 unit_patterns = data.setdefault('unit_patterns', {})
479 for elem in tree.findall('//units/unit'):
480 unit_type = elem.attrib['type']
481 unit_pattern = unit_patterns.setdefault(unit_type, {})
482 for pattern in elem.findall('unitPattern'):
483 unit_patterns[unit_type][pattern.attrib['count']] = \
484 unicode(pattern.text)
485
456 outfile = open(os.path.join(destdir, 'localedata', stem + '.dat'), 'wb') 486 outfile = open(os.path.join(destdir, 'localedata', stem + '.dat'), 'wb')
457 try: 487 try:
458 pickle.dump(data, outfile, 2) 488 pickle.dump(data, outfile, 2)
459 finally: 489 finally:
460 outfile.close() 490 outfile.close()
Copyright (C) 2012-2017 Edgewall Software