#! /usr/bin/python
import os
import sys
import enumdata
import xpathlite
from xpathlite import DraftResolution
import re
findEntry = xpathlite.findEntry
def ordStr(c):
if len(c) == 1:
return str(ord(c))
return "##########"
# the following functions are supposed to fix the problem with QLocale
# returning a character instead of strings for QLocale::exponential()
# and others. So we fallback to default values in these cases.
def fixOrdStrExp(c):
if len(c) == 1:
return str(ord(c))
return str(ord('e'))
def fixOrdStrPercent(c):
if len(c) == 1:
return str(ord(c))
return str(ord('%'))
def fixOrdStrList(c):
if len(c) == 1:
return str(ord(c))
return str(ord(';'))
def generateLocaleInfo(path):
(dir_name, file_name) = os.path.split(path)
exp = re.compile(r"([a-z]+)_([A-Z]{2})\.xml")
m = exp.match(file_name)
if not m:
return {}
language_code = m.group(1)
country_code = m.group(2)
language_id = enumdata.languageCodeToId(language_code)
if language_id == -1:
sys.stderr.write("unnknown language code \"" + language_code + "\"\n")
return {}
language = enumdata.language_list[language_id][0]
country_id = enumdata.countryCodeToId(country_code)
if country_id == -1:
sys.stderr.write("unnknown country code \"" + country_code + "\"\n")
return {}
country = enumdata.country_list[country_id][0]
base = dir_name + "/" + language_code + "_" + country_code
result = {}
result['base'] = base
result['language'] = language
result['country'] = country
result['language_id'] = language_id
result['country_id'] = country_id
result['decimal'] = findEntry(base, "numbers/symbols/decimal")
result['group'] = findEntry(base, "numbers/symbols/group")
result['list'] = findEntry(base, "numbers/symbols/list")
result['percent'] = findEntry(base, "numbers/symbols/percentSign")
result['zero'] = findEntry(base, "numbers/symbols/nativeZeroDigit")
result['minus'] = findEntry(base, "numbers/symbols/minusSign")
result['plus'] = findEntry(base, "numbers/symbols/plusSign")
result['exp'] = findEntry(base, "numbers/symbols/exponential").lower()
result['am'] = findEntry(base, "dates/calendars/calendar[gregorian]/am", draft=DraftResolution.approved)
result['pm'] = findEntry(base, "dates/calendars/calendar[gregorian]/pm", draft=DraftResolution.approved)
result['longDateFormat'] = findEntry(base, "dates/calendars/calendar[gregorian]/dateFormats/dateFormatLength[full]/dateFormat/pattern")
result['shortDateFormat'] = findEntry(base, "dates/calendars/calendar[gregorian]/dateFormats/dateFormatLength[short]/dateFormat/pattern")
result['longTimeFormat'] = findEntry(base, "dates/calendars/calendar[gregorian]/timeFormats/timeFormatLength[full]/timeFormat/pattern")
result['shortTimeFormat'] = findEntry(base, "dates/calendars/calendar[gregorian]/timeFormats/timeFormatLength[short]/timeFormat/pattern")
standalone_long_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[stand-alone]/monthWidth[wide]/month"
result['standaloneLongMonths'] \
= findEntry(base, standalone_long_month_path + "[1]") + ";" \
+ findEntry(base, standalone_long_month_path + "[2]") + ";" \
+ findEntry(base, standalone_long_month_path + "[3]") + ";" \
+ findEntry(base, standalone_long_month_path + "[4]") + ";" \
+ findEntry(base, standalone_long_month_path + "[5]") + ";" \
+ findEntry(base, standalone_long_month_path + "[6]") + ";" \
+ findEntry(base, standalone_long_month_path + "[7]") + ";" \
+ findEntry(base, standalone_long_month_path + "[8]") + ";" \
+ findEntry(base, standalone_long_month_path + "[9]") + ";" \
+ findEntry(base, standalone_long_month_path + "[10]") + ";" \
+ findEntry(base, standalone_long_month_path + "[11]") + ";" \
+ findEntry(base, standalone_long_month_path + "[12]") + ";"
standalone_short_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[stand-alone]/monthWidth[abbreviated]/month"
result['standaloneShortMonths'] \
= findEntry(base, standalone_short_month_path + "[1]") + ";" \
+ findEntry(base, standalone_short_month_path + "[2]") + ";" \
+ findEntry(base, standalone_short_month_path + "[3]") + ";" \
+ findEntry(base, standalone_short_month_path + "[4]") + ";" \
+ findEntry(base, standalone_short_month_path + "[5]") + ";" \
+ findEntry(base, standalone_short_month_path + "[6]") + ";" \
+ findEntry(base, standalone_short_month_path + "[7]") + ";" \
+ findEntry(base, standalone_short_month_path + "[8]") + ";" \
+ findEntry(base, standalone_short_month_path + "[9]") + ";" \
+ findEntry(base, standalone_short_month_path + "[10]") + ";" \
+ findEntry(base, standalone_short_month_path + "[11]") + ";" \
+ findEntry(base, standalone_short_month_path + "[12]") + ";"
standalone_narrow_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[stand-alone]/monthWidth[narrow]/month"
result['standaloneNarrowMonths'] \
= findEntry(base, standalone_narrow_month_path + "[1]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[2]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[3]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[4]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[5]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[6]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[7]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[8]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[9]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[10]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[11]") + ";" \
+ findEntry(base, standalone_narrow_month_path + "[12]") + ";"
long_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[format]/monthWidth[wide]/month"
result['longMonths'] \
= findEntry(base, long_month_path + "[1]") + ";" \
+ findEntry(base, long_month_path + "[2]") + ";" \
+ findEntry(base, long_month_path + "[3]") + ";" \
+ findEntry(base, long_month_path + "[4]") + ";" \
+ findEntry(base, long_month_path + "[5]") + ";" \
+ findEntry(base, long_month_path + "[6]") + ";" \
+ findEntry(base, long_month_path + "[7]") + ";" \
+ findEntry(base, long_month_path + "[8]") + ";" \
+ findEntry(base, long_month_path + "[9]") + ";" \
+ findEntry(base, long_month_path + "[10]") + ";" \
+ findEntry(base, long_month_path + "[11]") + ";" \
+ findEntry(base, long_month_path + "[12]") + ";"
short_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[format]/monthWidth[abbreviated]/month"
result['shortMonths'] \
= findEntry(base, short_month_path + "[1]") + ";" \
+ findEntry(base, short_month_path + "[2]") + ";" \
+ findEntry(base, short_month_path + "[3]") + ";" \
+ findEntry(base, short_month_path + "[4]") + ";" \
+ findEntry(base, short_month_path + "[5]") + ";" \
+ findEntry(base, short_month_path + "[6]") + ";" \
+ findEntry(base, short_month_path + "[7]") + ";" \
+ findEntry(base, short_month_path + "[8]") + ";" \
+ findEntry(base, short_month_path + "[9]") + ";" \
+ findEntry(base, short_month_path + "[10]") + ";" \
+ findEntry(base, short_month_path + "[11]") + ";" \
+ findEntry(base, short_month_path + "[12]") + ";"
narrow_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[format]/monthWidth[narrow]/month"
result['narrowMonths'] \
= findEntry(base, narrow_month_path + "[1]") + ";" \
+ findEntry(base, narrow_month_path + "[2]") + ";" \
+ findEntry(base, narrow_month_path + "[3]") + ";" \
+ findEntry(base, narrow_month_path + "[4]") + ";" \
+ findEntry(base, narrow_month_path + "[5]") + ";" \
+ findEntry(base, narrow_month_path + "[6]") + ";" \
+ findEntry(base, narrow_month_path + "[7]") + ";" \
+ findEntry(base, narrow_month_path + "[8]") + ";" \
+ findEntry(base, narrow_month_path + "[9]") + ";" \
+ findEntry(base, narrow_month_path + "[10]") + ";" \
+ findEntry(base, narrow_month_path + "[11]") + ";" \
+ findEntry(base, narrow_month_path + "[12]") + ";"
long_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[format]/dayWidth[wide]/day"
result['longDays'] \
= findEntry(base, long_day_path + "[sun]") + ";" \
+ findEntry(base, long_day_path + "[mon]") + ";" \
+ findEntry(base, long_day_path + "[tue]") + ";" \
+ findEntry(base, long_day_path + "[wed]") + ";" \
+ findEntry(base, long_day_path + "[thu]") + ";" \
+ findEntry(base, long_day_path + "[fri]") + ";" \
+ findEntry(base, long_day_path + "[sat]") + ";"
short_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[format]/dayWidth[abbreviated]/day"
result['shortDays'] \
= findEntry(base, short_day_path + "[sun]") + ";" \
+ findEntry(base, short_day_path + "[mon]") + ";" \
+ findEntry(base, short_day_path + "[tue]") + ";" \
+ findEntry(base, short_day_path + "[wed]") + ";" \
+ findEntry(base, short_day_path + "[thu]") + ";" \
+ findEntry(base, short_day_path + "[fri]") + ";" \
+ findEntry(base, short_day_path + "[sat]") + ";"
narrow_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[format]/dayWidth[narrow]/day"
result['narrowDays'] \
= findEntry(base, narrow_day_path + "[sun]") + ";" \
+ findEntry(base, narrow_day_path + "[mon]") + ";" \
+ findEntry(base, narrow_day_path + "[tue]") + ";" \
+ findEntry(base, narrow_day_path + "[wed]") + ";" \
+ findEntry(base, narrow_day_path + "[thu]") + ";" \
+ findEntry(base, narrow_day_path + "[fri]") + ";" \
+ findEntry(base, narrow_day_path + "[sat]") + ";"
standalone_long_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[stand-alone]/dayWidth[wide]/day"
result['standaloneLongDays'] \
= findEntry(base, standalone_long_day_path + "[sun]") + ";" \
+ findEntry(base, standalone_long_day_path + "[mon]") + ";" \
+ findEntry(base, standalone_long_day_path + "[tue]") + ";" \
+ findEntry(base, standalone_long_day_path + "[wed]") + ";" \
+ findEntry(base, standalone_long_day_path + "[thu]") + ";" \
+ findEntry(base, standalone_long_day_path + "[fri]") + ";" \
+ findEntry(base, standalone_long_day_path + "[sat]") + ";"
standalone_short_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[stand-alone]/dayWidth[abbreviated]/day"
result['standaloneShortDays'] \
= findEntry(base, standalone_short_day_path + "[sun]") + ";" \
+ findEntry(base, standalone_short_day_path + "[mon]") + ";" \
+ findEntry(base, standalone_short_day_path + "[tue]") + ";" \
+ findEntry(base, standalone_short_day_path + "[wed]") + ";" \
+ findEntry(base, standalone_short_day_path + "[thu]") + ";" \
+ findEntry(base, standalone_short_day_path + "[fri]") + ";" \
+ findEntry(base, standalone_short_day_path + "[sat]") + ";"
standalone_narrow_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[stand-alone]/dayWidth[narrow]/day"
result['standaloneNarrowDays'] \
= findEntry(base, standalone_narrow_day_path + "[sun]") + ";" \
+ findEntry(base, standalone_narrow_day_path + "[mon]") + ";" \
+ findEntry(base, standalone_narrow_day_path + "[tue]") + ";" \
+ findEntry(base, standalone_narrow_day_path + "[wed]") + ";" \
+ findEntry(base, standalone_narrow_day_path + "[thu]") + ";" \
+ findEntry(base, standalone_narrow_day_path + "[fri]") + ";" \
+ findEntry(base, standalone_narrow_day_path + "[sat]") + ";"
return result
def addEscapes(s):
result = ''
for c in s:
n = ord(c)
if n < 128:
result += c
else:
result += "\\x"
result += "%02x" % (n)
return result
def unicodeStr(s):
utf8 = s.encode('utf-8')
return "" + str(len(utf8)) + "" + addEscapes(utf8) + ""
def usage():
print "Usage: cldr2qlocalexml.py "
sys.exit()
if len(sys.argv) != 2:
usage()
cldr_dir = sys.argv[1]
if not os.path.isdir(cldr_dir):
usage()
cldr_files = os.listdir(cldr_dir)
locale_database = {}
for file in cldr_files:
l = generateLocaleInfo(cldr_dir + "/" + file)
if not l:
sys.stderr.write("skipping file \"" + file + "\"\n")
continue
locale_database[(l['language_id'], l['country_id'])] = l
locale_keys = locale_database.keys()
locale_keys.sort()
print ""
print " "
for id in enumdata.language_list:
l = enumdata.language_list[id]
print " "
print " " + l[0] + ""
print " " + str(id) + ""
print " " + l[1] + "
"
print " "
print " "
print " "
for id in enumdata.country_list:
l = enumdata.country_list[id]
print " "
print " " + l[0] + ""
print " " + str(id) + ""
print " " + l[1] + "
"
print " "
print " "
print \
" \n\
\n\
Afrikaans\n\
SouthAfrica\n\
\n\
\n\
Afan\n\
Ethiopia\n\
\n\
\n\
Afar\n\
Djibouti\n\
\n\
\n\
Arabic\n\
SaudiArabia\n\
\n\
\n\
Chinese\n\
China\n\
\n\
\n\
Dutch\n\
Netherlands\n\
\n\
\n\
English\n\
UnitedStates\n\
\n\
\n\
French\n\
France\n\
\n\
\n\
German\n\
Germany\n\
\n\
\n\
Greek\n\
Greece\n\
\n\
\n\
Italian\n\
Italy\n\
\n\
\n\
Malay\n\
Malaysia\n\
\n\
\n\
Portuguese\n\
Portugal\n\
\n\
\n\
Russian\n\
RussianFederation\n\
\n\
\n\
Serbian\n\
SerbiaAndMontenegro\n\
\n\
\n\
SerboCroatian\n\
SerbiaAndMontenegro\n\
\n\
\n\
Somali\n\
Somalia\n\
\n\
\n\
Spanish\n\
Spain\n\
\n\
\n\
Swahili\n\
Kenya\n\
\n\
\n\
Swedish\n\
Sweden\n\
\n\
\n\
Tigrinya\n\
Eritrea\n\
\n\
\n\
Uzbek\n\
Uzbekistan\n\
\n\
\n\
Persian\n\
Iran\n\
\n\
"
print " "
print \
" \n\
C\n\
AnyCountry\n\
46\n\
44\n\
59
\n\
37\n\
48\n\
45\n\
43\n\
101\n\
AM\n\
PM\n\
EEEE, d MMMM yyyy\n\
d MMM yyyy\n\
HH:mm:ss z\n\
HH:mm:ss\n\
January;February;March;April;May;June;July;August;September;October;November;December;\n\
Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec;\n\
J;F;M;A;M;J;J;A;S;O;N;D;\n\
January;February;March;April;May;June;July;August;September;October;November;December;\n\
Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec;\n\
1;2;3;4;5;6;7;8;9;10;11;12;\n\
Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;\n\
Sun;Mon;Tue;Wed;Thu;Fri;Sat;\n\
7;1;2;3;4;5;6;\n\
Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;\n\
Sun;Mon;Tue;Wed;Thu;Fri;Sat;\n\
S;M;T;W;T;F;S;\n\
"
for key in locale_keys:
l = locale_database[key]
print " "
# print " "
print " " + l['language'] + ""
print " " + l['country'] + ""
print " " + ordStr(l['decimal']) + ""
print " " + ordStr(l['group']) + ""
print " " + fixOrdStrList(l['list']) + "
"
print " " + fixOrdStrPercent(l['percent']) + ""
print " " + ordStr(l['zero']) + ""
print " " + ordStr(l['minus']) + ""
print " " + ordStr(l['plus']) + ""
print " " + fixOrdStrExp(l['exp']) + ""
print " " + l['am'].encode('utf-8') + ""
print " " + l['pm'].encode('utf-8') + ""
print " " + l['longDateFormat'].encode('utf-8') + ""
print " " + l['shortDateFormat'].encode('utf-8') + ""
print " " + l['longTimeFormat'].encode('utf-8') + ""
print " " + l['shortTimeFormat'].encode('utf-8') + ""
print " " + l['standaloneLongMonths'].encode('utf-8') + ""
print " "+ l['standaloneShortMonths'].encode('utf-8') + ""
print " "+ l['standaloneNarrowMonths'].encode('utf-8') + ""
print " " + l['longMonths'].encode('utf-8') + ""
print " " + l['shortMonths'].encode('utf-8') + ""
print " " + l['narrowMonths'].encode('utf-8') + ""
print " " + l['longDays'].encode('utf-8') + ""
print " " + l['shortDays'].encode('utf-8') + ""
print " " + l['narrowDays'].encode('utf-8') + ""
print " " + l['standaloneLongDays'].encode('utf-8') + ""
print " " + l['standaloneShortDays'].encode('utf-8') + ""
print " " + l['standaloneNarrowDays'].encode('utf-8') + ""
print " "
print " "
print ""