2017-05-12 09:59:18 +00:00
|
|
|
#!/usr/bin/env python2
|
2011-04-27 10:05:43 +00:00
|
|
|
#############################################################################
|
|
|
|
##
|
2020-01-09 19:47:23 +00:00
|
|
|
## Copyright (C) 2020 The Qt Company Ltd.
|
2016-01-15 12:36:27 +00:00
|
|
|
## Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
##
|
|
|
|
## This file is part of the test suite of the Qt Toolkit.
|
|
|
|
##
|
2016-01-15 12:36:27 +00:00
|
|
|
## $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-19 12:28:29 +00:00
|
|
|
## Commercial License Usage
|
|
|
|
## Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
## accordance with the commercial license agreement provided with the
|
|
|
|
## Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
## a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
## and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
## information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
##
|
2016-01-15 12:36:27 +00:00
|
|
|
## GNU General Public License Usage
|
|
|
|
## Alternatively, this file may be used under the terms of the GNU
|
|
|
|
## General Public License version 3 as published by the Free Software
|
|
|
|
## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
## included in the packaging of this file. Please review the following
|
|
|
|
## information to ensure the GNU General Public License requirements will
|
|
|
|
## be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
##
|
|
|
|
## $QT_END_LICENSE$
|
|
|
|
##
|
|
|
|
#############################################################################
|
2017-05-23 13:24:35 +00:00
|
|
|
"""Script to generate C++ code from CLDR data in qLocaleXML form
|
|
|
|
|
|
|
|
See ``cldr2qlocalexml.py`` for how to generate the qLocaleXML data itself.
|
|
|
|
Pass the output file from that as first parameter to this script; pass
|
|
|
|
the root of the qtbase check-out as second parameter.
|
|
|
|
"""
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
|
2020-02-25 11:30:06 +00:00
|
|
|
from qlocalexml import QLocaleXmlReader
|
2020-02-19 17:22:25 +00:00
|
|
|
from localetools import unicode2hex, wrap_list, Error, Transcriber, SourceFileEditor
|
2017-05-30 13:50:47 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
def compareLocaleKeys(key1, key2):
|
|
|
|
if key1 == key2:
|
|
|
|
return 0
|
|
|
|
|
2020-02-25 11:30:06 +00:00
|
|
|
if key1[0] != key2[0]: # First sort by language:
|
2011-04-27 10:05:43 +00:00
|
|
|
return key1[0] - key2[0]
|
2017-06-08 10:19:23 +00:00
|
|
|
|
2020-02-25 11:30:06 +00:00
|
|
|
defaults = compareLocaleKeys.default_map
|
|
|
|
# maps {(language, script): country} by ID
|
2017-06-08 10:19:23 +00:00
|
|
|
try:
|
2020-02-25 11:30:06 +00:00
|
|
|
country = defaults[key1[:2]]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if key1[2] == country:
|
|
|
|
return -1
|
|
|
|
if key2[2] == country:
|
|
|
|
return 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-25 11:30:06 +00:00
|
|
|
if key1[1] == key2[1]:
|
|
|
|
return key1[2] - key2[2]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-25 11:30:06 +00:00
|
|
|
try:
|
|
|
|
country = defaults[key2[:2]]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2011-04-27 10:05:43 +00:00
|
|
|
else:
|
2020-02-25 11:30:06 +00:00
|
|
|
if key2[2] == country:
|
|
|
|
return 1
|
|
|
|
if key1[2] == country:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
return key1[1] - key2[1]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StringDataToken:
|
2020-01-09 13:48:21 +00:00
|
|
|
def __init__(self, index, length, bits):
|
|
|
|
if index > 0xffff:
|
2020-04-06 23:00:12 +00:00
|
|
|
raise ValueError('Start-index ({}) exceeds the uint16 range!'.format(index))
|
2020-01-09 13:48:21 +00:00
|
|
|
if length >= (1 << bits):
|
2020-04-06 23:00:12 +00:00
|
|
|
raise ValueError('Data size ({}) exceeds the {}-bit range!'.format(length, bits))
|
2020-01-09 13:48:21 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
self.index = index
|
|
|
|
self.length = length
|
|
|
|
|
|
|
|
class StringData:
|
2017-05-31 14:17:54 +00:00
|
|
|
def __init__(self, name):
|
2011-04-27 10:05:43 +00:00
|
|
|
self.data = []
|
|
|
|
self.hash = {}
|
2017-05-31 14:17:54 +00:00
|
|
|
self.name = name
|
2020-01-09 19:47:23 +00:00
|
|
|
self.text = '' # Used in quick-search for matches in data
|
2017-01-14 16:53:31 +00:00
|
|
|
|
2020-04-06 23:00:12 +00:00
|
|
|
def append(self, s, bits = 8):
|
2011-04-27 10:05:43 +00:00
|
|
|
try:
|
2020-01-09 19:47:23 +00:00
|
|
|
token = self.hash[s]
|
|
|
|
except KeyError:
|
2020-01-09 13:48:21 +00:00
|
|
|
token = self.__store(s, bits)
|
2020-01-09 19:47:23 +00:00
|
|
|
self.hash[s] = token
|
2011-04-27 10:05:43 +00:00
|
|
|
return token
|
|
|
|
|
2020-01-09 13:48:21 +00:00
|
|
|
def __store(self, s, bits):
|
2020-01-09 19:47:23 +00:00
|
|
|
"""Add string s to known data.
|
|
|
|
|
|
|
|
Seeks to avoid duplication, where possible.
|
|
|
|
For example, short-forms may be prefixes of long-forms.
|
|
|
|
"""
|
|
|
|
if not s:
|
2020-01-09 13:48:21 +00:00
|
|
|
return StringDataToken(0, 0, bits)
|
2020-01-09 19:47:23 +00:00
|
|
|
ucs2 = unicode2hex(s)
|
|
|
|
try:
|
|
|
|
index = self.text.index(s) - 1
|
|
|
|
matched = 0
|
|
|
|
while matched < len(ucs2):
|
|
|
|
index, matched = self.data.index(ucs2[0], index + 1), 1
|
|
|
|
if index + len(ucs2) >= len(self.data):
|
|
|
|
raise ValueError # not found after all !
|
|
|
|
while matched < len(ucs2) and self.data[index + matched] == ucs2[matched]:
|
|
|
|
matched += 1
|
|
|
|
except ValueError:
|
|
|
|
index = len(self.data)
|
|
|
|
self.data += ucs2
|
|
|
|
self.text += s
|
|
|
|
|
|
|
|
assert index >= 0
|
|
|
|
try:
|
2020-01-09 13:48:21 +00:00
|
|
|
return StringDataToken(index, len(ucs2), bits)
|
2020-01-09 19:47:23 +00:00
|
|
|
except ValueError as e:
|
|
|
|
e.args += (self.name, s)
|
|
|
|
raise
|
|
|
|
|
2017-01-14 16:53:31 +00:00
|
|
|
def write(self, fd):
|
2020-01-09 13:48:21 +00:00
|
|
|
if len(self.data) > 0xffff:
|
2020-04-06 23:00:12 +00:00
|
|
|
raise ValueError('Data is too big ({}) for quint16 index to its end!'
|
|
|
|
.format(len(self.data)),
|
2020-01-09 13:48:21 +00:00
|
|
|
self.name)
|
2020-04-06 23:00:12 +00:00
|
|
|
fd.write("\nstatic const char16_t {}[] = {{\n".format(self.name))
|
2017-01-14 16:53:31 +00:00
|
|
|
fd.write(wrap_list(self.data))
|
|
|
|
fd.write("\n};\n")
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
def currencyIsoCodeData(s):
|
|
|
|
if s:
|
2017-05-12 10:00:55 +00:00
|
|
|
return '{' + ",".join(str(ord(x)) for x in s) + '}'
|
|
|
|
return "{0,0,0}"
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
class LocaleSourceEditor (SourceFileEditor):
|
|
|
|
__upinit = SourceFileEditor.__init__
|
|
|
|
def __init__(self, path, temp, version):
|
|
|
|
self.__upinit(path, temp)
|
|
|
|
self.writer.write("""
|
|
|
|
/*
|
|
|
|
This part of the file was generated on {} from the
|
|
|
|
Common Locale Data Repository v{}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
http://www.unicode.org/cldr/
|
|
|
|
|
|
|
|
Do not edit this section: instead regenerate it using
|
|
|
|
cldr2qlocalexml.py and qlocalexml2cpp.py on updated (or
|
|
|
|
edited) CLDR data; see qtbase/util/locale_database/.
|
|
|
|
*/
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
""".format(datetime.date.today(), version))
|
|
|
|
|
|
|
|
class LocaleDataWriter (LocaleSourceEditor):
|
|
|
|
def likelySubtags(self, likely):
|
|
|
|
self.writer.write('static const QLocaleId likely_subtags[] = {\n')
|
|
|
|
for had, have, got, give, last in likely:
|
|
|
|
self.writer.write(' {{ {:3d}, {:3d}, {:3d} }}'.format(*have))
|
|
|
|
self.writer.write(', {{ {:3d}, {:3d}, {:3d} }}'.format(*give))
|
|
|
|
self.writer.write(' ' if last else ',')
|
|
|
|
self.writer.write(' // {} -> {}\n'.format(had, got))
|
|
|
|
self.writer.write('};\n\n')
|
|
|
|
|
|
|
|
def localeIndex(self, indices):
|
|
|
|
self.writer.write('static const quint16 locale_index[] = {\n')
|
|
|
|
for pair in indices:
|
|
|
|
self.writer.write('{:6d}, // {}\n'.format(*pair))
|
|
|
|
self.writer.write(' 0 // trailing 0\n')
|
|
|
|
self.writer.write('};\n\n')
|
|
|
|
|
|
|
|
def localeData(self, locales, names):
|
|
|
|
list_pattern_part_data = StringData('list_pattern_part_data')
|
2020-04-06 23:00:12 +00:00
|
|
|
single_character_data = StringData('single_character_data')
|
2020-02-19 17:22:25 +00:00
|
|
|
date_format_data = StringData('date_format_data')
|
|
|
|
time_format_data = StringData('time_format_data')
|
|
|
|
days_data = StringData('days_data')
|
|
|
|
am_data = StringData('am_data')
|
|
|
|
pm_data = StringData('pm_data')
|
|
|
|
byte_unit_data = StringData('byte_unit_data')
|
|
|
|
currency_symbol_data = StringData('currency_symbol_data')
|
|
|
|
currency_display_name_data = StringData('currency_display_name_data')
|
|
|
|
currency_format_data = StringData('currency_format_data')
|
|
|
|
endonyms_data = StringData('endonyms_data')
|
|
|
|
|
|
|
|
# Locale data
|
|
|
|
self.writer.write('static const QLocaleData locale_data[] = {\n')
|
|
|
|
# Table headings: keep each label centred in its field, matching line_format:
|
|
|
|
self.writer.write(' // '
|
|
|
|
# Width 6 + comma
|
|
|
|
' lang ' # IDs
|
|
|
|
'script '
|
|
|
|
' terr '
|
2020-04-06 23:00:12 +00:00
|
|
|
|
|
|
|
# Range entries (all start-indices, then all sizes)
|
|
|
|
# Width 5 + comma
|
|
|
|
'lStrt ' # List pattern
|
|
|
|
'lpMid '
|
|
|
|
'lpEnd '
|
|
|
|
'lPair '
|
|
|
|
'lDelm ' # List delimiter
|
|
|
|
# Representing numbers
|
|
|
|
' dec '
|
|
|
|
'group '
|
|
|
|
'prcnt '
|
|
|
|
' zero '
|
|
|
|
'minus '
|
|
|
|
'plus '
|
|
|
|
' exp '
|
|
|
|
# Quotation marks
|
|
|
|
'qtOpn '
|
|
|
|
'qtEnd '
|
|
|
|
'altQO '
|
|
|
|
'altQE '
|
|
|
|
'lDFmt ' # Date format
|
|
|
|
'sDFmt '
|
|
|
|
'lTFmt ' # Time format
|
|
|
|
'sTFmt '
|
|
|
|
'slDay ' # Day names
|
|
|
|
'lDays '
|
|
|
|
'ssDys '
|
|
|
|
'sDays '
|
|
|
|
'snDay '
|
|
|
|
'nDays '
|
|
|
|
' am ' # am/pm indicators
|
|
|
|
' pm '
|
|
|
|
' byte '
|
|
|
|
'siQnt '
|
|
|
|
'iecQn '
|
|
|
|
'crSym ' # Currency formatting
|
|
|
|
'crDsp '
|
|
|
|
'crFmt '
|
|
|
|
'crFNg '
|
|
|
|
'ntLng ' # Name of language in itself, and of territory
|
|
|
|
'ntTer '
|
|
|
|
# Width 3 + comma for each size; no header
|
|
|
|
+ ' ' * 37 +
|
|
|
|
|
|
|
|
# Strays (char array, bit-fields):
|
|
|
|
# Width 10 + 2 spaces + comma
|
2020-02-19 17:22:25 +00:00
|
|
|
' currISO '
|
|
|
|
# Width 6 + comma
|
2020-04-06 23:00:12 +00:00
|
|
|
'curDgt ' # Currency digits
|
|
|
|
'curRnd ' # Currencty rounding (unused: QTBUG-81343)
|
2020-02-19 17:22:25 +00:00
|
|
|
'dow1st ' # First day of week
|
|
|
|
' wknd+ ' # Week-end start/end days
|
2020-01-17 10:00:24 +00:00
|
|
|
' wknd- '
|
|
|
|
'grpTop '
|
|
|
|
'grpMid '
|
|
|
|
'grpEnd'
|
2020-02-19 17:22:25 +00:00
|
|
|
# No trailing space on last entry (be sure to
|
|
|
|
# pad before adding anything after it).
|
|
|
|
'\n')
|
|
|
|
|
|
|
|
formatLine = ''.join((
|
|
|
|
' {{ ',
|
|
|
|
# Locale-identifier
|
|
|
|
'{:6d},' * 3,
|
2020-04-06 23:00:12 +00:00
|
|
|
# List patterns, date/time formats, day names, am/pm
|
2020-02-19 17:22:25 +00:00
|
|
|
# SI/IEC byte-unit abbreviations
|
2020-04-06 23:00:12 +00:00
|
|
|
# Currency and endonyms
|
|
|
|
# Range starts
|
|
|
|
'{:5d},' * 37,
|
|
|
|
# Range sizes
|
|
|
|
'{:3d},' * 37,
|
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
# Currency ISO code
|
|
|
|
' {:>10s}, ',
|
|
|
|
# Currency formatting
|
|
|
|
'{:6d},{:6d}',
|
|
|
|
# Day of week and week-end
|
|
|
|
',{:6d}' * 3,
|
2020-01-17 10:00:24 +00:00
|
|
|
# Number group sizes
|
|
|
|
',{:6d}' * 3,
|
2020-02-19 17:22:25 +00:00
|
|
|
' }}')).format
|
|
|
|
for key in names:
|
|
|
|
locale = locales[key]
|
2020-04-06 23:00:12 +00:00
|
|
|
# Sequence of StringDataToken:
|
|
|
|
ranges = (tuple(list_pattern_part_data.append(p) for p in # 5 entries:
|
|
|
|
(locale.listPatternPartStart, locale.listPatternPartMiddle,
|
|
|
|
locale.listPatternPartEnd, locale.listPatternPartTwo,
|
|
|
|
locale.listDelim)) +
|
|
|
|
tuple(single_character_data.append(p) for p in # 11 entries
|
|
|
|
(locale.decimal, locale.group, locale.percent, locale.zero,
|
|
|
|
locale.minus, locale.plus, locale.exp,
|
|
|
|
locale.quotationStart, locale.quotationEnd,
|
|
|
|
locale.alternateQuotationStart, locale.alternateQuotationEnd)) +
|
|
|
|
tuple (date_format_data.append(f) for f in # 2 entries:
|
|
|
|
(locale.longDateFormat, locale.shortDateFormat)) +
|
|
|
|
tuple(time_format_data.append(f) for f in # 2 entries:
|
|
|
|
(locale.longTimeFormat, locale.shortTimeFormat)) +
|
|
|
|
tuple(days_data.append(d) for d in # 6 entries:
|
|
|
|
(locale.standaloneLongDays, locale.longDays,
|
|
|
|
locale.standaloneShortDays, locale.shortDays,
|
|
|
|
locale.standaloneNarrowDays, locale.narrowDays)) +
|
|
|
|
(am_data.append(locale.am), pm_data.append(locale.pm)) + # 2 entries
|
|
|
|
tuple(byte_unit_data.append(b) for b in # 3 entries:
|
|
|
|
(locale.byte_unit,
|
|
|
|
locale.byte_si_quantified,
|
|
|
|
locale.byte_iec_quantified)) +
|
|
|
|
(currency_symbol_data.append(locale.currencySymbol),
|
|
|
|
currency_display_name_data.append(locale.currencyDisplayName),
|
|
|
|
currency_format_data.append(locale.currencyFormat),
|
|
|
|
currency_format_data.append(locale.currencyNegativeFormat),
|
|
|
|
endonyms_data.append(locale.languageEndonym),
|
|
|
|
endonyms_data.append(locale.countryEndonym)) # 6 entries
|
|
|
|
) # Total: 37 entries
|
|
|
|
assert len(ranges) == 37
|
|
|
|
|
|
|
|
self.writer.write(formatLine(*(
|
|
|
|
key +
|
|
|
|
tuple(r.index for r in ranges) +
|
|
|
|
tuple(r.length for r in ranges) +
|
|
|
|
(currencyIsoCodeData(locale.currencyIsoCode),
|
|
|
|
locale.currencyDigits,
|
|
|
|
locale.currencyRounding, # unused (QTBUG-81343)
|
2020-01-17 10:00:24 +00:00
|
|
|
locale.firstDayOfWeek, locale.weekendStart, locale.weekendEnd,
|
|
|
|
locale.groupTop, locale.groupHigher, locale.groupLeast) ))
|
2020-02-19 17:22:25 +00:00
|
|
|
+ ', // {}/{}/{}\n'.format(
|
|
|
|
locale.language, locale.script, locale.country))
|
|
|
|
self.writer.write(formatLine(*( # All zeros, matching the format:
|
2020-04-06 23:00:12 +00:00
|
|
|
(0,) * 3 + (0,) * 37 * 2
|
2020-02-19 17:22:25 +00:00
|
|
|
+ (currencyIsoCodeData(0),)
|
2020-01-17 10:00:24 +00:00
|
|
|
+ (0,) * 8 ))
|
2020-02-19 17:22:25 +00:00
|
|
|
+ ' // trailing zeros\n')
|
|
|
|
self.writer.write('};\n')
|
|
|
|
|
|
|
|
# StringData tables:
|
2020-04-06 23:00:12 +00:00
|
|
|
for data in (list_pattern_part_data, single_character_data,
|
|
|
|
date_format_data, time_format_data, days_data,
|
2020-02-19 17:22:25 +00:00
|
|
|
byte_unit_data, am_data, pm_data, currency_symbol_data,
|
|
|
|
currency_display_name_data, currency_format_data,
|
|
|
|
endonyms_data):
|
|
|
|
data.write(self.writer)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __writeNameData(out, book, form):
|
|
|
|
out('static const char {}_name_list[] =\n'.format(form))
|
|
|
|
out('"Default\\0"\n')
|
|
|
|
for key, value in book.items():
|
|
|
|
if key == 0:
|
|
|
|
continue
|
|
|
|
out('"' + value[0] + '\\0"\n')
|
|
|
|
out(';\n\n')
|
|
|
|
|
|
|
|
out('static const quint16 {}_name_index[] = {{\n'.format(form))
|
|
|
|
out(' 0, // Any{}\n'.format(form.capitalize()))
|
|
|
|
index = 8
|
|
|
|
for key, value in book.items():
|
|
|
|
if key == 0:
|
|
|
|
continue
|
|
|
|
name = value[0]
|
|
|
|
out('{:6d}, // {}\n'.format(index, name))
|
|
|
|
index += len(name) + 1
|
|
|
|
out('};\n\n')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __writeCodeList(out, book, form, width):
|
|
|
|
out('static const unsigned char {}_code_list[] =\n'.format(form))
|
|
|
|
for key, value in book.items():
|
|
|
|
code = value[1]
|
|
|
|
code += r'\0' * max(width - len(code), 0)
|
|
|
|
out('"{}" // {}\n'.format(code, value[0]))
|
|
|
|
out(';\n\n')
|
|
|
|
|
|
|
|
def languageNames(self, languages):
|
|
|
|
self.__writeNameData(self.writer.write, languages, 'language')
|
|
|
|
|
|
|
|
def scriptNames(self, scripts):
|
|
|
|
self.__writeNameData(self.writer.write, scripts, 'script')
|
|
|
|
|
|
|
|
def countryNames(self, countries):
|
|
|
|
self.__writeNameData(self.writer.write, countries, 'country')
|
|
|
|
|
|
|
|
# TODO: unify these next three into the previous three; kept
|
|
|
|
# separate for now to verify we're not changing data.
|
|
|
|
|
|
|
|
def languageCodes(self, languages):
|
|
|
|
self.__writeCodeList(self.writer.write, languages, 'language', 3)
|
|
|
|
|
|
|
|
def scriptCodes(self, scripts):
|
|
|
|
self.__writeCodeList(self.writer.write, scripts, 'script', 4)
|
|
|
|
|
|
|
|
def countryCodes(self, countries): # TODO: unify with countryNames()
|
|
|
|
self.__writeCodeList(self.writer.write, countries, 'country', 3)
|
|
|
|
|
|
|
|
class CalendarDataWriter (LocaleSourceEditor):
|
2020-04-06 23:00:12 +00:00
|
|
|
formatCalendar = (
|
|
|
|
' {{'
|
|
|
|
+ ','.join(('{:6d}',) * 3 + ('{:5d}',) * 6 + ('{:3d}',) * 6)
|
|
|
|
+ ' }},').format
|
2020-02-19 17:22:25 +00:00
|
|
|
def write(self, calendar, locales, names):
|
|
|
|
months_data = StringData('months_data')
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
self.writer.write('static const QCalendarLocale locale_data[] = {\n')
|
2020-04-06 23:00:12 +00:00
|
|
|
self.writer.write(
|
|
|
|
' //'
|
|
|
|
# IDs, width 7 (6 + comma)
|
|
|
|
' lang '
|
|
|
|
' script'
|
|
|
|
' terr '
|
|
|
|
# Month-name start-indices, width 6 (5 + comma)
|
|
|
|
'sLong '
|
|
|
|
' long '
|
|
|
|
'sShrt '
|
|
|
|
'short '
|
|
|
|
'sNarw '
|
|
|
|
'narow '
|
|
|
|
# No individual headers for the sizes.
|
|
|
|
'Sizes...'
|
|
|
|
'\n')
|
2020-02-19 17:22:25 +00:00
|
|
|
for key in names:
|
|
|
|
locale = locales[key]
|
2020-04-06 23:00:12 +00:00
|
|
|
# Sequence of StringDataToken:
|
|
|
|
try:
|
|
|
|
# Twelve long month names can add up to more than 256 (e.g. kde_TZ: 264)
|
|
|
|
ranges = (tuple(months_data.append(m[calendar], 16) for m in
|
|
|
|
(locale.standaloneLongMonths, locale.longMonths)) +
|
|
|
|
tuple(months_data.append(m[calendar]) for m in
|
|
|
|
(locale.standaloneShortMonths, locale.shortMonths,
|
|
|
|
locale.standaloneNarrowMonths, locale.narrowMonths)))
|
|
|
|
except ValueError as e:
|
|
|
|
e.args += (locale.language, locale.script, locale.country, stem)
|
|
|
|
raise
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
self.writer.write(
|
2020-04-06 23:00:12 +00:00
|
|
|
self.formatCalendar(*(
|
|
|
|
key +
|
|
|
|
tuple(r.index for r in ranges) +
|
|
|
|
tuple(r.length for r in ranges) ))
|
2020-03-17 16:48:08 +00:00
|
|
|
+ '// {}/{}/{}\n'.format(locale.language, locale.script, locale.country))
|
2020-04-06 23:00:12 +00:00
|
|
|
self.writer.write(self.formatCalendar(*( (0,) * (3 + 6 * 2) ))
|
2020-02-19 17:22:25 +00:00
|
|
|
+ '// trailing zeros\n')
|
|
|
|
self.writer.write('};\n')
|
|
|
|
months_data.write(self.writer)
|
|
|
|
|
|
|
|
class LocaleHeaderWriter (SourceFileEditor):
|
|
|
|
__upinit = SourceFileEditor.__init__
|
|
|
|
def __init__(self, path, temp, dupes):
|
|
|
|
self.__upinit(path, temp)
|
|
|
|
self.__dupes = dupes
|
|
|
|
|
|
|
|
def languages(self, languages):
|
|
|
|
self.__enum('Language', languages, self.__language)
|
|
|
|
self.writer.write('\n')
|
|
|
|
|
|
|
|
def countries(self, countries):
|
|
|
|
self.__enum('Country', countries, self.__country)
|
|
|
|
|
|
|
|
def scripts(self, scripts):
|
|
|
|
self.__enum('Script', scripts, self.__script)
|
|
|
|
self.writer.write('\n')
|
|
|
|
|
|
|
|
# Implementation details
|
|
|
|
from enumdata import (language_aliases as __language,
|
|
|
|
country_aliases as __country,
|
|
|
|
script_aliases as __script)
|
|
|
|
|
|
|
|
def __enum(self, name, book, alias):
|
|
|
|
assert book
|
|
|
|
out, dupes = self.writer.write, self.__dupes
|
2020-10-09 10:26:19 +00:00
|
|
|
out(' enum {} : ushort {{\n'.format(name))
|
2020-02-19 17:22:25 +00:00
|
|
|
for key, value in book.items():
|
|
|
|
member = value[0]
|
|
|
|
if name == 'Script':
|
|
|
|
# Don't .capitalize() as some names are already camel-case (see enumdata.py):
|
|
|
|
member = ''.join(word[0].upper() + word[1:] for word in member.split())
|
|
|
|
if not member.endswith('Script'):
|
|
|
|
member += 'Script'
|
|
|
|
if member in dupes:
|
|
|
|
raise Error('The script name "{}" is messy'.format(member))
|
|
|
|
else:
|
|
|
|
member = ''.join(member.split())
|
|
|
|
member = member + name if member in dupes else member
|
|
|
|
out(' {} = {},\n'.format(member, key))
|
|
|
|
|
|
|
|
out('\n '
|
|
|
|
+ ',\n '.join('{} = {}'.format(*pair)
|
|
|
|
for pair in sorted(alias.items()))
|
|
|
|
+ ',\n\n Last{} = {}\n }};\n'.format(name, member))
|
|
|
|
|
|
|
|
def usage(name, err, message = ''):
|
|
|
|
err.write("""Usage: {} path/to/qlocale.xml root/of/qtbase
|
|
|
|
""".format(name)) # TODO: elaborate
|
|
|
|
if message:
|
|
|
|
err.write('\n' + message + '\n')
|
|
|
|
|
|
|
|
def main(args, out, err):
|
|
|
|
# TODO: Make calendars a command-line parameter
|
|
|
|
# map { CLDR name: Qt file name }
|
|
|
|
calendars = {'gregorian': 'roman', 'persian': 'jalali', 'islamic': 'hijri',} # 'hebrew': 'hebrew',
|
|
|
|
|
|
|
|
name = args.pop(0)
|
|
|
|
if len(args) != 2:
|
|
|
|
usage(name, err, 'I expect two arguments')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
qlocalexml = args.pop(0)
|
|
|
|
qtsrcdir = args.pop(0)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-05-31 19:42:11 +00:00
|
|
|
if not (os.path.isdir(qtsrcdir)
|
2019-10-23 14:37:22 +00:00
|
|
|
and all(os.path.isfile(os.path.join(qtsrcdir, 'src', 'corelib', 'text', leaf))
|
2017-05-31 19:42:11 +00:00
|
|
|
for leaf in ('qlocale_data_p.h', 'qlocale.h', 'qlocale.qdoc'))):
|
2020-02-19 17:22:25 +00:00
|
|
|
usage(name, err, 'Missing expected files under qtbase source root ' + qtsrcdir)
|
|
|
|
return 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-25 11:30:06 +00:00
|
|
|
reader = QLocaleXmlReader(qlocalexml)
|
Rework cldr2qlocalexml.py's reading of CLDR data
Move the code out to a CldrReader class in cldr.py, expand CldrAccess
with facilities that needs, expand ldml.py to include support for more
features, finally making xpathlite.py redundant. This initial commit
aims, though, to be bug-for-bug compatible with xpathlite in its
reading of the CLDR data.
It turns out we've been using draftier data than we were aware of
(which might not be a bad thing). The xpathlite code appeared to check
for draft attributes, but these only appear on leaf nodes and most
data were fetched by finding a parent and then scanning its children
without the draft check; only am/pm data was actually being excluded
based on draft values. (We allowed contributed, for am/pm, in
addition to approved, which is all the xpathlite code allows
otherwise.) There are also some less equivocal bugs; I'll deal with
these in later commits.
Simplified number-system data look-ups; the old get_number_in_system()
was taking care of old LDML versions' placement of the number system
attribute; this is no longer needed. (It was also being used for a
currency value to which it was not appropriate, which is now handled
separately; this is one of the bugs mentioned above.) Ditched a
fall-back to nativeZeroDigit, which no longer exists in CLDR.
Change the command-line to take the root of the CLDR data tree, rather
than its common/main/ sub-directory. Support naming the file to which
to write output, as a second command-line argument, instead of always
writing to stdout (which remains the default) and leaving whoever runs
the script to redirect stdout.
Support (internally for now, while adding TODOs to give main() more
command-line options) separating the stderr output into its more and
less interesting parts; for now, continue producing both, but suppress
the least interesting entirely.
Task-number: QTBUG-81344
Change-Id: Ie611b47403a9452b51feaeeaaa0fbc8f7e84dc71
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
2020-02-27 12:58:58 +00:00
|
|
|
locale_map = dict(reader.loadLocaleMap(calendars, err.write))
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
locale_keys = locale_map.keys()
|
2020-02-25 11:30:06 +00:00
|
|
|
compareLocaleKeys.default_map = dict(reader.defaultMap())
|
2011-04-27 10:05:43 +00:00
|
|
|
locale_keys.sort(compareLocaleKeys)
|
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
try:
|
|
|
|
writer = LocaleDataWriter(os.path.join(qtsrcdir, 'src', 'corelib', 'text',
|
|
|
|
'qlocale_data_p.h'),
|
|
|
|
qtsrcdir, reader.cldrVersion)
|
|
|
|
except IOError as e:
|
|
|
|
err.write('Failed to open files to transcribe locale data: ' + (e.message or e.args[1]))
|
|
|
|
return 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
writer.likelySubtags(reader.likelyMap())
|
|
|
|
writer.localeIndex(reader.languageIndices(tuple(k[0] for k in locale_map)))
|
|
|
|
writer.localeData(locale_map, locale_keys)
|
|
|
|
writer.writer.write('\n')
|
|
|
|
writer.languageNames(reader.languages)
|
|
|
|
writer.scriptNames(reader.scripts)
|
|
|
|
writer.countryNames(reader.countries)
|
|
|
|
# TODO: merge the next three into the previous three
|
|
|
|
writer.languageCodes(reader.languages)
|
|
|
|
writer.scriptCodes(reader.scripts)
|
|
|
|
writer.countryCodes(reader.countries)
|
|
|
|
except Error as e:
|
|
|
|
writer.cleanup()
|
|
|
|
err.write('\nError updating locale data: ' + e.message + '\n')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
writer.close()
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-14 16:53:31 +00:00
|
|
|
# Generate calendar data
|
|
|
|
for calendar, stem in calendars.items():
|
2020-02-19 17:22:25 +00:00
|
|
|
try:
|
|
|
|
writer = CalendarDataWriter(os.path.join(qtsrcdir, 'src', 'corelib', 'time',
|
|
|
|
'q{}calendar_data_p.h'.format(stem)),
|
|
|
|
qtsrcdir, reader.cldrVersion)
|
|
|
|
except IOError as e:
|
|
|
|
err.write('Failed to open files to transcribe ' + calendar
|
|
|
|
+ ' data ' + (e.message or e.args[1]))
|
|
|
|
return 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
writer.write(calendar, locale_map, locale_keys)
|
|
|
|
except Error as e:
|
|
|
|
writer.cleanup()
|
|
|
|
err.write('\nError updating ' + calendar + ' locale data: ' + e.message + '\n')
|
|
|
|
return 1
|
2020-01-09 13:48:21 +00:00
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
writer.close()
|
2017-01-14 16:53:31 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
# qlocale.h
|
2020-02-19 17:22:25 +00:00
|
|
|
try:
|
|
|
|
writer = LocaleHeaderWriter(os.path.join(qtsrcdir, 'src', 'corelib', 'text', 'qlocale.h'),
|
|
|
|
qtsrcdir, reader.dupes)
|
|
|
|
except IOError as e:
|
|
|
|
err.write('Failed to open files to transcribe qlocale.h: ' + (e.message or e.args[1]))
|
|
|
|
return 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-19 17:22:25 +00:00
|
|
|
try:
|
|
|
|
writer.languages(reader.languages)
|
|
|
|
writer.scripts(reader.scripts)
|
|
|
|
writer.countries(reader.countries)
|
|
|
|
except Error as e:
|
|
|
|
writer.cleanup()
|
|
|
|
err.write('\nError updating qlocale.h: ' + e.message + '\n')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
writer.close()
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
# qlocale.qdoc
|
2020-02-19 17:22:25 +00:00
|
|
|
try:
|
|
|
|
writer = Transcriber(os.path.join(qtsrcdir, 'src', 'corelib', 'text', 'qlocale.qdoc'),
|
|
|
|
qtsrcdir)
|
|
|
|
except IOError as e:
|
|
|
|
err.write('Failed to open files to transcribe qlocale.qdoc: ' + (e.message or e.args[1]))
|
|
|
|
return 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-08-13 12:32:18 +00:00
|
|
|
DOCSTRING = " QLocale's data is based on Common Locale Data Repository "
|
2020-02-19 17:22:25 +00:00
|
|
|
try:
|
|
|
|
for line in writer.reader:
|
|
|
|
if DOCSTRING in line:
|
|
|
|
writer.writer.write(DOCSTRING + 'v' + reader.cldrVersion + '.\n')
|
|
|
|
else:
|
|
|
|
writer.writer.write(line)
|
|
|
|
except Error as e:
|
|
|
|
writer.cleanup()
|
|
|
|
err.write('\nError updating qlocale.qdoc: ' + e.message + '\n')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
writer.close()
|
|
|
|
return 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-02-19 17:22:25 +00:00
|
|
|
import sys
|
|
|
|
sys.exit(main(sys.argv, sys.stdout, sys.stderr))
|