diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2011-02-08 18:53:55 (GMT) |
---|---|---|
committer | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2011-02-17 13:35:17 (GMT) |
commit | 2f880e66c2dedc8b072dca4672035fc5b2e659bc (patch) | |
tree | a34049c3c2ba8ddf7fbf5076e484d2a70508f776 /util/local_database | |
parent | 8a74c5112aa81c5638b14d8b5285e6ef8474a195 (diff) | |
download | Qt-2f880e66c2dedc8b072dca4672035fc5b2e659bc.zip Qt-2f880e66c2dedc8b072dca4672035fc5b2e659bc.tar.gz Qt-2f880e66c2dedc8b072dca4672035fc5b2e659bc.tar.bz2 |
Added currency support for QLocale
REMARK: This commit is missing updated static cldr data in
src/corelib/tools/qlocale_data_p.h that has been left out
intentionally to avoid repository bloating.
Before compiling make sure to update that file using the scripts
util/local_database/cldr2qlocalexml.py and
util/local_database/qlocalexml2cpp.py.
Otherwise you will most likely experience segmentation faults.
Task-number: QTBUG-17100
Reviewed-by: Zeno Albisser
Diffstat (limited to 'util/local_database')
-rwxr-xr-x | util/local_database/cldr2qlocalexml.py | 92 | ||||
-rwxr-xr-x | util/local_database/qlocalexml2cpp.py | 49 | ||||
-rw-r--r-- | util/local_database/xpathlite.py | 42 |
3 files changed, 180 insertions, 3 deletions
diff --git a/util/local_database/cldr2qlocalexml.py b/util/local_database/cldr2qlocalexml.py index 8b5ec16..311cf4e 100755 --- a/util/local_database/cldr2qlocalexml.py +++ b/util/local_database/cldr2qlocalexml.py @@ -50,6 +50,37 @@ import re findEntry = xpathlite.findEntry findEntryInFile = xpathlite._findEntryInFile +findTagsInFile = xpathlite.findTagsInFile + +def parse_number_format(patterns, data): + # this is a very limited parsing of the number format for currency only. + def skip_repeating_pattern(x): + p = x.replace('0', '#').replace(',', '').replace('.', '') + seen = False + result = '' + for c in p: + if c == '#': + if seen: + continue + seen = True + else: + seen = False + result = result + c + return result + patterns = patterns.split(';') + result = [] + for pattern in patterns: + pattern = skip_repeating_pattern(pattern) + pattern = pattern.replace('#', "%1") + # according to http://www.unicode.org/reports/tr35/#Number_Format_Patterns + # there can be doubled or trippled currency sign, however none of the + # locales use that. + pattern = pattern.replace(u'\xa4', "%2") + pattern = pattern.replace("''", "###").replace("'", '').replace("###", "'") + pattern = pattern.replace('-', data['minus']) + pattern = pattern.replace('+', data['plus']) + result.append(pattern) + return result def ordStr(c): if len(c) == 1: @@ -123,11 +154,36 @@ def generateLocaleInfo(path): result['language_id'] = language_id result['country_id'] = country_id + supplementalPath = dir_name + "/../supplemental/supplementalData.xml" + currencies = findTagsInFile(supplementalPath, "currencyData/region[iso3166=%s]"%country_code); + result['currencyIsoCode'] = '' + result['currencyDigits'] = 2 + result['currencyRounding'] = 1 + if currencies: + for e in currencies: + if e[0] == 'currency': + tender = True + t = filter(lambda x: x[0] == 'tender', e[1]) + if t and t[0][1] == 'false': + tender = False; + if tender and not filter(lambda x: x[0] == 'to', e[1]): + result['currencyIsoCode'] = filter(lambda x: x[0] == 'iso4217', e[1])[0][1] + break + if result['currencyIsoCode']: + t = findTagsInFile(supplementalPath, "currencyData/fractions/info[iso4217=%s]"%result['currencyIsoCode']); + if t and t[0][0] == 'info': + result['currencyDigits'] = int(filter(lambda x: x[0] == 'digits', t[0][1])[0][1]) + result['currencyRounding'] = int(filter(lambda x: x[0] == 'rounding', t[0][1])[0][1]) numbering_system = None try: numbering_system = findEntry(path, "numbers/defaultNumberingSystem") except: pass + def findEntryDef(path, xpath, value=''): + try: + return findEntry(path, xpath) + except xpathlite.Error: + return value def get_number_in_system(path, xpath, numbering_system): if numbering_system: try: @@ -150,6 +206,27 @@ def generateLocaleInfo(path): result['longTimeFormat'] = convert_date(findEntry(path, "dates/calendars/calendar[gregorian]/timeFormats/timeFormatLength[full]/timeFormat/pattern")) result['shortTimeFormat'] = convert_date(findEntry(path, "dates/calendars/calendar[gregorian]/timeFormats/timeFormatLength[short]/timeFormat/pattern")) + currency_format = get_number_in_system(path, "numbers/currencyFormats/currencyFormatLength/currencyFormat/pattern", numbering_system) + currency_format = parse_number_format(currency_format, result) + result['currencyFormat'] = currency_format[0] + result['currencyNegativeFormat'] = '' + if len(currency_format) > 1: + result['currencyNegativeFormat'] = currency_format[1] + + result['currencySymbol'] = '' + result['currencyDisplayName'] = '' + if result['currencyIsoCode']: + result['currencySymbol'] = findEntryDef(path, "numbers/currencies/currency[%s]/symbol" % result['currencyIsoCode']) + display_name_path = "numbers/currencies/currency[%s]/displayName" % result['currencyIsoCode'] + result['currencyDisplayName'] \ + = findEntryDef(path, display_name_path) + ";" \ + + findEntryDef(path, display_name_path + "[count=zero]") + ";" \ + + findEntryDef(path, display_name_path + "[count=one]") + ";" \ + + findEntryDef(path, display_name_path + "[count=two]") + ";" \ + + findEntryDef(path, display_name_path + "[count=few]") + ";" \ + + findEntryDef(path, display_name_path + "[count=many]") + ";" \ + + findEntryDef(path, display_name_path + "[count=other]") + ";" + standalone_long_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[stand-alone]/monthWidth[wide]/month" result['standaloneLongMonths'] \ = findEntry(path, standalone_long_month_path + "[1]") + ";" \ @@ -300,7 +377,6 @@ def generateLocaleInfo(path): + findEntry(path, standalone_narrow_day_path + "[fri]") + ";" \ + findEntry(path, standalone_narrow_day_path + "[sat]") + ";" - return result def addEscapes(s): @@ -536,6 +612,13 @@ print \ <standaloneLongDays>Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;</standaloneLongDays>\n\ <standaloneShortDays>Sun;Mon;Tue;Wed;Thu;Fri;Sat;</standaloneShortDays>\n\ <standaloneNarrowDays>S;M;T;W;T;F;S;</standaloneNarrowDays>\n\ + <currencyIsoCode></currencyIsoCode>\n\ + <currencySymbol></currencySymbol>\n\ + <currencyDisplayName>;;;;;;;</currencyDisplayName>\n\ + <currencyDigits>2</currencyDigits>\n\ + <currencyRounding>1</currencyRounding>\n\ + <currencyFormat>%1%2</currencyFormat>\n\ + <currencyNegativeFormat></currencyNegativeFormat>\n\ </locale>" for key in locale_keys: @@ -573,6 +656,13 @@ for key in locale_keys: print " <standaloneLongDays>" + l['standaloneLongDays'].encode('utf-8') + "</standaloneLongDays>" print " <standaloneShortDays>" + l['standaloneShortDays'].encode('utf-8') + "</standaloneShortDays>" print " <standaloneNarrowDays>" + l['standaloneNarrowDays'].encode('utf-8') + "</standaloneNarrowDays>" + print " <currencyIsoCode>" + l['currencyIsoCode'].encode('utf-8') + "</currencyIsoCode>" + print " <currencySymbol>" + l['currencySymbol'].encode('utf-8') + "</currencySymbol>" + print " <currencyDisplayName>" + l['currencyDisplayName'].encode('utf-8') + "</currencyDisplayName>" + print " <currencyDigits>" + str(l['currencyDigits']) + "</currencyDigits>" + print " <currencyRounding>" + str(l['currencyRounding']) + "</currencyRounding>" + print " <currencyFormat>" + l['currencyFormat'].encode('utf-8') + "</currencyFormat>" + print " <currencyNegativeFormat>" + l['currencyNegativeFormat'].encode('utf-8') + "</currencyNegativeFormat>" print " </locale>" print " </localeList>" print "</localeDatabase>" diff --git a/util/local_database/qlocalexml2cpp.py b/util/local_database/qlocalexml2cpp.py index 9bc3c7e..494daf2 100755 --- a/util/local_database/qlocalexml2cpp.py +++ b/util/local_database/qlocalexml2cpp.py @@ -223,6 +223,13 @@ class Locale: self.longDays = eltText(firstChildElt(elt, "longDays")) self.shortDays = eltText(firstChildElt(elt, "shortDays")) self.narrowDays = eltText(firstChildElt(elt, "narrowDays")) + self.currencyIsoCode = eltText(firstChildElt(elt, "currencyIsoCode")) + self.currencySymbol = eltText(firstChildElt(elt, "currencySymbol")) + self.currencyDisplayName = eltText(firstChildElt(elt, "currencyDisplayName")) + self.currencyDigits = int(eltText(firstChildElt(elt, "currencyDigits"))) + self.currencyRounding = int(eltText(firstChildElt(elt, "currencyRounding"))) + self.currencyFormat = eltText(firstChildElt(elt, "currencyFormat")) + self.currencyNegativeFormat = eltText(firstChildElt(elt, "currencyNegativeFormat")) def loadLocaleMap(doc, language_map, country_map): result = {} @@ -336,6 +343,11 @@ def printEscapedString(s): print escapedString(s); +def currencyIsoCodeData(s): + if s: + return ",".join(map(lambda x: str(ord(x)), s)) + return "0,0,0" + def main(): doc = xml.dom.minidom.parse("locale.xml") language_map = loadLanguageMap(doc) @@ -389,6 +401,9 @@ def main(): days_data = StringData() am_data = StringData() pm_data = StringData() + currency_symbol_data = StringData() + currency_display_name_data = StringData() + currency_format_data = StringData() # Locale data print "static const QLocalePrivate locale_data[] = {" @@ -402,7 +417,7 @@ def main(): for key in locale_keys: l = locale_map[key] - print " { %6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%6d }, // %s/%s" \ + print " { %6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, {%s}, %s,%s,%s,%s,%6d,%6d,%6d }, // %s/%s" \ % (key[0], key[1], l.decimal, l.group, @@ -430,10 +445,17 @@ def main(): days_data.append(l.narrowDays), am_data.append(l.am), pm_data.append(l.pm), + currencyIsoCodeData(l.currencyIsoCode), + currency_symbol_data.append(l.currencySymbol), + currency_display_name_data.append(l.currencyDisplayName), + currency_format_data.append(l.currencyFormat), + currency_format_data.append(l.currencyNegativeFormat), + l.currencyDigits, + l.currencyRounding, l.firstDayOfWeek, l.language, l.country) - print " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0 } // trailing 0s" + print " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, {0,0,0}, 0,0, 0,0, 0,0, 0,0, 0, 0, 0 } // trailing 0s" print "};" print @@ -494,6 +516,29 @@ def main(): print + # Currency symbol data + #check_static_char_array_length("currency_symbol", currency_symbol_data.data) + print "static const ushort currency_symbol_data[] = {" + print wrap_list(currency_symbol_data.data) + print "};" + + print + + # Currency display name data + #check_static_char_array_length("currency_display_name", currency_display_name_data.data) + print "static const ushort currency_display_name_data[] = {" + print wrap_list(currency_display_name_data.data) + print "};" + + print + + # Currency format data + #check_static_char_array_length("currency_format", currency_format_data.data) + print "static const ushort currency_format_data[] = {" + print wrap_list(currency_format_data.data) + print "};" + + print # Language name list print "static const char language_name_list[] =" print "\"Default\\0\"" diff --git a/util/local_database/xpathlite.py b/util/local_database/xpathlite.py index 95e6711..502d85d 100644 --- a/util/local_database/xpathlite.py +++ b/util/local_database/xpathlite.py @@ -87,6 +87,48 @@ def findChild(parent, tag_name, arg_name=None, arg_value=None, draft=None): return node return False +def findTagsInFile(file, path): + doc = False + if doc_cache.has_key(file): + doc = doc_cache[file] + else: + doc = xml.dom.minidom.parse(file) + doc_cache[file] = doc + + elt = doc.documentElement + tag_spec_list = path.split("/") + last_entry = None + for i in range(len(tag_spec_list)): + tag_spec = tag_spec_list[i] + tag_name = tag_spec + arg_name = 'type' + arg_value = '' + left_bracket = tag_spec.find('[') + if left_bracket != -1: + tag_name = tag_spec[:left_bracket] + arg_value = tag_spec[left_bracket+1:-1].split("=") + if len(arg_value) == 2: + arg_name = arg_value[0] + arg_value = arg_value[1] + else: + arg_value = arg_value[0] + elt = findChild(elt, tag_name, arg_name, arg_value) + if not elt: + return None + ret = [] + if elt.childNodes: + for node in elt.childNodes: + if node.attributes: + element = [node.nodeName, None] + element[1] = node.attributes.items() + ret.append(element) + else: + if elt.attributes: + element = [elt.nodeName, None] + element[1] = elt.attributes.items() + ret.append(element) + return ret + def _findEntryInFile(file, path, draft=None, attribute=None): doc = False if doc_cache.has_key(file): |