From 2a5d99be5007d6579329408a57370afd490b315a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 10 May 2019 11:27:28 +0200 Subject: [libcalamares] Start on a locale-data subdirectory - Continuing the notion that libcalamares should provide (non-GUI) services for modules, add a locale service. - This will, unfortunately, roughly duplicate Qt's QLocale database, but in a form that is public and more readable. --- src/libcalamares/locale/cldr-extractor.py | 140 ++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/libcalamares/locale/cldr-extractor.py diff --git a/src/libcalamares/locale/cldr-extractor.py b/src/libcalamares/locale/cldr-extractor.py new file mode 100644 index 000000000..6c4061fcc --- /dev/null +++ b/src/libcalamares/locale/cldr-extractor.py @@ -0,0 +1,140 @@ +#! /usr/bin/env python3 +# +# Python3 script to scrape some data out of ICU CLDR supplemental data. +# +# To use this script, you must have downloaded the CLDR data, e.g. +# http://unicode.org/Public/cldr/35.1/, and extracted the zip file. +# Run the script from **inside** the common/ durectory that is created +# (or fix the hard-coded path). +# +# The script tries to print C++ code that compiles; if there are encoding +# problems, it will print some kind of representation of the problematic +# lines. +# +# To avoid having to cross-reference multiple XML files, the script +# cheats: it reads the comments as well to get names. So it looks for +# pairs of lines like this: +# +# +# +# +# It extracts the 2-character country code "BQ" from the sub-tag, and +# parses the comment to get a language and country name (instead of looking up +# "pap" and "BQ" in other tables). This may be considered a hack. +# +# A large collection of exceptions can be found in the two *_mapper tables, +# which massage the CLDR names to Qt enum values. + + +import sys + +# These are languages listed in CLDR that don't match +# the enum-values in QLocale::Language. +language_mapper = { + "?" : "AnyLanguage", + "Bangla" : "Bengali", + "Kalaallisut" : "Greenlandic", + "Haitian Creole" : "Haitian", + "Kyrgyz" : "Kirghiz", + "Norwegian Bokmål" : "NorwegianBokmal", + "Tokelau" : "TokelauLanguage", + "Tuvalu" : "TuvaluLanguage", + } + +country_mapper = { + "Åland Islands" : "AlandIslands", + "St. Barthélemy" : "SaintBarthelemy", + "Côte d’Ivoire" : "IvoryCoast", + "Curaçao" : "CuraSao", + "Réunion" : "Reunion", + "São Tomé & Príncipe" : "SaoTomeAndPrincipe", + "Bosnia & Herzegovina" : "BosniaAndHerzegowina", + "Czechia" : "CzechRepublic", + "St. Pierre & Miquelon" : "SaintPierreAndMiquelon", + "Vatican City" : "VaticanCityState", + "South Georgia & South Sandwich Islands" : "SouthGeorgiaAndTheSouthSandwichIslands", + "Timor-Leste" : "EastTimor", + "Wallis & Futuna" : "WallisAndFutunaIslands", + "Myanmar (Burma)" : "Myanmar", + "Svalbard & Jan Mayen" : "SvalbardAndJanMayenIslands", + "St. Martin" : "SaintMartin", + "North Macedonia" : "Macedonia", + "Hong Kong SAR China" : "HongKong", + "Macao SAR China" : "Macau", + "Eurozone" : "AnyCountry", # Not likely for GeoIP + "Caribbean Netherlands" : "Bonaire", # Bonaire, Saba, St.Eustatius + } + +def extricate(l1, l2): + if '"und_' not in l1: + return + if '{ ?; ?;' not in l2: + return + + # This is extremely crude "parsing" which chops up the string + # by delimiter and then extracts some substring. + l1_parts = l1.split("und_") + l2_parts = l2.split(";") + + l1_first_quote = l1_parts[1].find('"') + l1_code = l1_parts[1][:l1_first_quote] + if len(l1_code) != 2: + return + + l2_brace = l2_parts[2].find("{") + l2_language = l2_parts[2][l2_brace+1:].strip() + l2_brace = l2_parts[2].find("}") + l2_country = l2_parts[2][:l2_brace-1].strip() + + # Handle mapped cases + l2_language = language_mapper.get(l2_language, l2_language) + l2_language = l2_language.replace(" ", "") + + # Handle mapped cases and then do a bunch of standard replacements. + l2_country = country_mapper.get(l2_country, l2_country) + l2_country = l2_country.replace(" ", "").replace("-", "").replace(".","").replace("&","And") + + # There shouldn't be any UTF-8 left in there. + try: + print("{!s} QLocale::Language::{!s}, QLocale::Country::{!s}, '{!s}', '{!s}' {!s},".format( + "{", + l2_language, + l2_country, + l1_code[0], + l1_code[1], + "}")) + except UnicodeEncodeError: + print(list(map(lambda x : '?' if x > 128 else chr(x), map(lambda x:ord(x), l2_country)))) + raise + +print("""// Generated from CLDR data +#include +struct CountryData +{ + QLocale::Language l; + QLocale::Country c; + char cc1; + char cc2; +}; + +static const CountryData countryMap[] = { +""") + +with open("supplemental/likelySubtags.xml", "rt", encoding="UTF-8") as f: + l1 = "a line" + while l1: + l1 = f.readline() + if ' Date: Fri, 10 May 2019 11:46:21 +0200 Subject: [libcalamares] Tidy up the CLDR script, add license, make more pythonic --- src/libcalamares/locale/cldr-extractor.py | 254 +++++++++++++++++++++++------- 1 file changed, 196 insertions(+), 58 deletions(-) diff --git a/src/libcalamares/locale/cldr-extractor.py b/src/libcalamares/locale/cldr-extractor.py index 6c4061fcc..06fb69e3b 100644 --- a/src/libcalamares/locale/cldr-extractor.py +++ b/src/libcalamares/locale/cldr-extractor.py @@ -1,30 +1,67 @@ #! /usr/bin/env python3 # +# === This file is part of Calamares - === +# # Python3 script to scrape some data out of ICU CLDR supplemental data. # -# To use this script, you must have downloaded the CLDR data, e.g. -# http://unicode.org/Public/cldr/35.1/, and extracted the zip file. -# Run the script from **inside** the common/ durectory that is created -# (or fix the hard-coded path). +### BEGIN LICENSES +# +# Copyright 2019 Adriaan de Groot # -# The script tries to print C++ code that compiles; if there are encoding -# problems, it will print some kind of representation of the problematic -# lines. +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: # -# To avoid having to cross-reference multiple XML files, the script -# cheats: it reads the comments as well to get names. So it looks for -# pairs of lines like this: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. # -# -# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. # -# It extracts the 2-character country code "BQ" from the sub-tag, and -# parses the comment to get a language and country name (instead of looking up -# "pap" and "BQ" in other tables). This may be considered a hack. +### END LICENSES + +### BEGIN USAGE # -# A large collection of exceptions can be found in the two *_mapper tables, -# which massage the CLDR names to Qt enum values. +""" +Python3 script to scrape some data out of ICU CLDR supplemental data. + +To use this script, you must have downloaded the CLDR data, e.g. +http://unicode.org/Public/cldr/35.1/, and extracted the zip file. +Run the script from **inside** the common/ durectory that is created +(or fix the hard-coded path). + +The script tries to print C++ code that compiles; if there are encoding +problems, it will print some kind of representation of the problematic +lines. + +To avoid having to cross-reference multiple XML files, the script +cheats: it reads the comments as well to get names. So it looks for +pairs of lines like this: + + + +It extracts the 2-character country code "BQ" from the sub-tag, and +parses the comment to get a language and country name (instead of looking up +"pap" and "BQ" in other tables). This may be considered a hack. + +A large collection of exceptions can be found in the two *_mapper tables, +which massage the CLDR names to Qt enum values. +""" +# +### END USAGE import sys @@ -64,9 +101,64 @@ country_mapper = { "Eurozone" : "AnyCountry", # Not likely for GeoIP "Caribbean Netherlands" : "Bonaire", # Bonaire, Saba, St.Eustatius } + +class CountryData: + def __init__(self, country_code, language_name, country_name): + """ + Takes a 2-letter country name, and enum names from + QLocale::Language and QLocale::Country. An empty + @p country code is acceptable, for the terminating + entry in the data array (and yields a 0,0 code). + """ + if country_code: + assert len(country_code) == 2 + self.country_code = country_code + self.language_enum = language_name + self.country_enum = country_name + else: + self.country_code = "" + self.language_enum = "AnyLanguage" + self.country_enum = "AnyCountry" -def extricate(l1, l2): - if '"und_' not in l1: + def __str__(self): + if self.country_code: + char0 = "'{!s}'".format(self.country_code[0]) + char1 = "'{!s}'".format(self.country_code[1]) + else: + char0 = "0" + char1 = "0" + + return "{!s} QLocale::Language::{!s}, QLocale::Country::{!s}, {!s}, {!s} {!s},".format( + "{", + self.language_enum, + self.country_enum, + char0, + char1, + "}") + + # Must match type name below + cpp_classname = "CountryData" + + # Must match the output format of __str__ above + cpp_declaration = """ +struct CountryData +{ + QLocale::Language l; + QLocale::Country c; + char cc1; + char cc2; +}; +""" + + +def extricate_subtags(l1, l2): + """ + Given two lines @p l1 and @p l2 which are the element-line + and the comment-line underneath it, return a CountryData for them, + or None if the two lines are not relevant (e.g. not the right subtag from, + or 3-letter country codes. + """ + if 'from="und_' not in l1: return if '{ ?; ?;' not in l2: return @@ -94,47 +186,93 @@ def extricate(l1, l2): l2_country = country_mapper.get(l2_country, l2_country) l2_country = l2_country.replace(" ", "").replace("-", "").replace(".","").replace("&","And") - # There shouldn't be any UTF-8 left in there. - try: - print("{!s} QLocale::Language::{!s}, QLocale::Country::{!s}, '{!s}', '{!s}' {!s},".format( - "{", - l2_language, - l2_country, - l1_code[0], - l1_code[1], - "}")) - except UnicodeEncodeError: - print(list(map(lambda x : '?' if x > 128 else chr(x), map(lambda x:ord(x), l2_country)))) - raise - -print("""// Generated from CLDR data -#include -struct CountryData -{ - QLocale::Language l; - QLocale::Country c; - char cc1; - char cc2; -}; + return CountryData(l1_code, l2_language, l2_country) + + +def read_subtags_file(): + """ + Returns a list of CountryData objects from the likelySubtags file. + """ + data = [] + + with open("supplemental/likelySubtags.xml", "rt", encoding="UTF-8") as f: + l1 = "a line" + while l1: + l1 = f.readline() + if ' === +* +* This file is derived from CLDR data from Unicode, Inc. Applicable terms: +* +* A. Unicode Copyright +* 1. Copyright © 1991-2019 Unicode, Inc. All rights reserved. +* B. Definitions +* Unicode Data Files ("DATA FILES") include all data files under the directories: +* https://www.unicode.org/Public/ +* C. Terms of Use +* 2. Any person is hereby authorized, without fee, to view, use, reproduce, +* and distribute all documents and files, subject to the Terms and +* Conditions herein. +*/ + +// BEGIN Generated from CLDR data + +""" + +cpp_footer_comment = """ +// END Generated from CLDR data +""" + + +def make_identifier(classname): + """ + Given a class name (e.g. CountryData) return an identifer + for the data-table for that class. + """ + identifier = [ classname[0].lower() ] + for c in classname[1:]: + if c.isupper(): + identifier.extend(["_", c.lower()]) + else: + identifier.append(c) - extricate(l1, l2) + identifier.append("_table") + return "".join(identifier) -print("""{ QLocale::Language::AnyLanguage, QLocale::Country::AnyCountry, 0, 0 } // Terminator -}; -// END Generated from CLDR data -""") + +def export_class(cls, data): + """ + Given a @p cls and a list of @p data objects from that class, + print (to stdout) a C++ file for that data. + """ + with open("{!s}_p.cpp".format(cls.cpp_classname), "wt", encoding="UTF-8") as f: + f.write(cpp_header_comment) + f.write(cls.cpp_declaration) + f.write("\nstatic const {!s} {!s}[] = {!s}\n".format( + cls.cpp_classname, + make_identifier(cls.cpp_classname), + "{")) + for d in data: + f.write(str(d)) + f.write("\n") + f.write("};\n\n"); + f.write(cpp_footer_comment) + + +if __name__ == "__main__": + export_class(CountryData, read_subtags_file()) -- cgit v1.2.2 From 07a9052fcadd29e6e26c8d5c965598f1c71c8646 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 10 May 2019 12:41:35 +0200 Subject: [libcalamares] Also output size of locale-tables --- src/libcalamares/locale/cldr-extractor.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/libcalamares/locale/cldr-extractor.py b/src/libcalamares/locale/cldr-extractor.py index 06fb69e3b..4e279337b 100644 --- a/src/libcalamares/locale/cldr-extractor.py +++ b/src/libcalamares/locale/cldr-extractor.py @@ -251,7 +251,6 @@ def make_identifier(classname): else: identifier.append(c) - identifier.append("_table") return "".join(identifier) @@ -260,17 +259,27 @@ def export_class(cls, data): Given a @p cls and a list of @p data objects from that class, print (to stdout) a C++ file for that data. """ + identifier = make_identifier(cls.cpp_classname) + with open("{!s}_p.cpp".format(cls.cpp_classname), "wt", encoding="UTF-8") as f: f.write(cpp_header_comment) f.write(cls.cpp_declaration) - f.write("\nstatic const {!s} {!s}[] = {!s}\n".format( + f.write("\nstatic constexpr int const {!s}_size = {!s};\n".format( + identifier, + len(data))) + f.write("\nstatic const {!s} {!s}_table[] = {!s}\n".format( cls.cpp_classname, - make_identifier(cls.cpp_classname), + identifier, "{")) for d in data: f.write(str(d)) f.write("\n") f.write("};\n\n"); + f.write("static_assert( (sizeof({!s}_table) / sizeof({!s})) == {!s}_size, \"Table size mismatch for {!s}\" );\n\n".format( + identifier, + cls.cpp_classname, + identifier, + cls.cpp_classname)) f.write(cpp_footer_comment) -- cgit v1.2.2 From 529d93c58f336a3580937fa27b97af0461b2b00d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 10 May 2019 12:45:54 +0200 Subject: [libcalamares] Add the first locale-data C++ infra - Lookup country data based on enum or 2-letter code - No data yet, so return only stubs - The (generated) data tables are not listed as sources because they are #include'd by the API implementation; they're full of otherwise-unused static tables, so don't make sense to compile separately. - While here, tidy up the CMakeLists a bit to reduce the number of superfluous variables. --- src/libcalamares/CMakeLists.txt | 13 ++++++---- src/libcalamares/locale/Lookup.cpp | 51 +++++++++++++++++++++++++++++++++++++ src/libcalamares/locale/Lookup.h | 52 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 src/libcalamares/locale/Lookup.cpp create mode 100644 src/libcalamares/locale/Lookup.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index bc3ce0511..c65fb6d39 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -21,11 +21,14 @@ set( libSources JobQueue.cpp ProcessJob.cpp Settings.cpp -) -set( partSources + + # Locale-data service + locale/Lookup.cpp + + # Partition service partition/PartitionSize.cpp -) -set( utilsSources + + # Utility service utils/CalamaresUtilsSystem.cpp utils/CommandList.cpp utils/Dirs.cpp @@ -75,7 +78,7 @@ if( WITH_PYTHON ) ) endif() -add_library( calamares SHARED ${libSources} ${kdsagSources} ${partSources} ${utilsSources} ) +add_library( calamares SHARED ${libSources} ${kdsagSources} ) set_target_properties( calamares PROPERTIES VERSION ${CALAMARES_VERSION_SHORT} diff --git a/src/libcalamares/locale/Lookup.cpp b/src/libcalamares/locale/Lookup.cpp new file mode 100644 index 000000000..4ab8a79c4 --- /dev/null +++ b/src/libcalamares/locale/Lookup.cpp @@ -0,0 +1,51 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "Lookup.h" + +// No data yet + +namespace Calamares +{ + +QLocale::Country countryForCode(const QString& code) +{ + return QLocale::Country::AnyCountry; +} + +QLocale countryLocale(const QString& code) +{ + return QLocale(); +} + +QLocale::Language languageForCountry(const QString& code) +{ + return QLocale::Language::AnyLanguage; +} + +QLocale::Language languageForCountry(QLocale::Country country) +{ + return QLocale::Language::AnyLanguage; +} + +QPair countryData(const QString& code) +{ + return qMakePair( QLocale::Country::AnyCountry, QLocale::Language::AnyLanguage ); +} + +} // namespace diff --git a/src/libcalamares/locale/Lookup.h b/src/libcalamares/locale/Lookup.h new file mode 100644 index 000000000..976c4dc21 --- /dev/null +++ b/src/libcalamares/locale/Lookup.h @@ -0,0 +1,52 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef LOCALE_LOOKUP_H +#define LOCALE_LOOKUP_H + +#include "DllMacro.h" + +#include +#include + +namespace Calamares +{ + /* All the functions in this file do lookups of locale data + * based on CLDR tables; these are lookups that you can't (easily) + * do with just QLocale (e.g. from 2-letter country code to a likely + * locale). + */ + + /// @brief Map a 2-letter code to a Country, or AnyCountry if not found + DLLEXPORT QLocale::Country countryForCode( const QString& code ); + /** @brief Map a Country to a Language, or AnyLanguage if not found + * + * This is a *likely* language for the given country, based on the + * CLDR tables. For instance, this maps Belgium to Dutch. + */ + DLLEXPORT QLocale::Language languageForCountry( QLocale::Country country ); + /// @brief Map a 2-letter code to a Language, or AnyLanguage if not found + DLLEXPORT QLocale::Language languageForCountry( const QString& code ); + + /// @brief Get both Country and Language for a 2-letter code + DLLEXPORT QPair< QLocale::Country, QLocale::Language > countryData( const QString& code ); + /// @brief Get a likely locale for a 2-letter country code + DLLEXPORT QLocale countryLocale( const QString& code ); +} // namespace + +#endif -- cgit v1.2.2 From 0dfef341602dc35fb5c479075626287bdf922a19 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 10 May 2019 12:54:23 +0200 Subject: [libcalamares] Add generated locale data tables --- src/libcalamares/locale/CountryData_p.cpp | 235 ++++++++++++++++++++++++++++++ src/libcalamares/locale/Lookup.cpp | 2 +- 2 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 src/libcalamares/locale/CountryData_p.cpp diff --git a/src/libcalamares/locale/CountryData_p.cpp b/src/libcalamares/locale/CountryData_p.cpp new file mode 100644 index 000000000..a6e9b3a95 --- /dev/null +++ b/src/libcalamares/locale/CountryData_p.cpp @@ -0,0 +1,235 @@ +/* GENERATED FILE DO NOT EDIT +* +* === This file is part of Calamares - === +* +* This file is derived from CLDR data from Unicode, Inc. Applicable terms: +* +* A. Unicode Copyright +* 1. Copyright © 1991-2019 Unicode, Inc. All rights reserved. +* B. Definitions +* Unicode Data Files ("DATA FILES") include all data files under the directories: +* https://www.unicode.org/Public/ +* C. Terms of Use +* 2. Any person is hereby authorized, without fee, to view, use, reproduce, +* and distribute all documents and files, subject to the Terms and +* Conditions herein. +*/ + +// BEGIN Generated from CLDR data + + +struct CountryData +{ + QLocale::Language l; + QLocale::Country c; + char cc1; + char cc2; +}; + +static constexpr int const country_data_size = 198; + +static const CountryData country_data_table[] = { +{ QLocale::Language::Catalan, QLocale::Country::Andorra, 'A', 'D' }, +{ QLocale::Language::Arabic, QLocale::Country::UnitedArabEmirates, 'A', 'E' }, +{ QLocale::Language::Persian, QLocale::Country::Afghanistan, 'A', 'F' }, +{ QLocale::Language::Albanian, QLocale::Country::Albania, 'A', 'L' }, +{ QLocale::Language::Armenian, QLocale::Country::Armenia, 'A', 'M' }, +{ QLocale::Language::Portuguese, QLocale::Country::Angola, 'A', 'O' }, +{ QLocale::Language::AnyLanguage, QLocale::Country::Antarctica, 'A', 'Q' }, +{ QLocale::Language::Spanish, QLocale::Country::Argentina, 'A', 'R' }, +{ QLocale::Language::Samoan, QLocale::Country::AmericanSamoa, 'A', 'S' }, +{ QLocale::Language::German, QLocale::Country::Austria, 'A', 'T' }, +{ QLocale::Language::Dutch, QLocale::Country::Aruba, 'A', 'W' }, +{ QLocale::Language::Swedish, QLocale::Country::AlandIslands, 'A', 'X' }, +{ QLocale::Language::Azerbaijani, QLocale::Country::Azerbaijan, 'A', 'Z' }, +{ QLocale::Language::Bosnian, QLocale::Country::BosniaAndHerzegowina, 'B', 'A' }, +{ QLocale::Language::Bengali, QLocale::Country::Bangladesh, 'B', 'D' }, +{ QLocale::Language::Dutch, QLocale::Country::Belgium, 'B', 'E' }, +{ QLocale::Language::French, QLocale::Country::BurkinaFaso, 'B', 'F' }, +{ QLocale::Language::Bulgarian, QLocale::Country::Bulgaria, 'B', 'G' }, +{ QLocale::Language::Arabic, QLocale::Country::Bahrain, 'B', 'H' }, +{ QLocale::Language::Rundi, QLocale::Country::Burundi, 'B', 'I' }, +{ QLocale::Language::French, QLocale::Country::Benin, 'B', 'J' }, +{ QLocale::Language::French, QLocale::Country::SaintBarthelemy, 'B', 'L' }, +{ QLocale::Language::Malay, QLocale::Country::Brunei, 'B', 'N' }, +{ QLocale::Language::Spanish, QLocale::Country::Bolivia, 'B', 'O' }, +{ QLocale::Language::Papiamento, QLocale::Country::Bonaire, 'B', 'Q' }, +{ QLocale::Language::Portuguese, QLocale::Country::Brazil, 'B', 'R' }, +{ QLocale::Language::Dzongkha, QLocale::Country::Bhutan, 'B', 'T' }, +{ QLocale::Language::AnyLanguage, QLocale::Country::BouvetIsland, 'B', 'V' }, +{ QLocale::Language::Belarusian, QLocale::Country::Belarus, 'B', 'Y' }, +{ QLocale::Language::Swahili, QLocale::Country::CongoKinshasa, 'C', 'D' }, +{ QLocale::Language::French, QLocale::Country::CentralAfricanRepublic, 'C', 'F' }, +{ QLocale::Language::French, QLocale::Country::CongoBrazzaville, 'C', 'G' }, +{ QLocale::Language::German, QLocale::Country::Switzerland, 'C', 'H' }, +{ QLocale::Language::French, QLocale::Country::IvoryCoast, 'C', 'I' }, +{ QLocale::Language::Spanish, QLocale::Country::Chile, 'C', 'L' }, +{ QLocale::Language::French, QLocale::Country::Cameroon, 'C', 'M' }, +{ QLocale::Language::Chinese, QLocale::Country::China, 'C', 'N' }, +{ QLocale::Language::Spanish, QLocale::Country::Colombia, 'C', 'O' }, +{ QLocale::Language::AnyLanguage, QLocale::Country::ClippertonIsland, 'C', 'P' }, +{ QLocale::Language::Spanish, QLocale::Country::CostaRica, 'C', 'R' }, +{ QLocale::Language::Spanish, QLocale::Country::Cuba, 'C', 'U' }, +{ QLocale::Language::Portuguese, QLocale::Country::CapeVerde, 'C', 'V' }, +{ QLocale::Language::Papiamento, QLocale::Country::CuraSao, 'C', 'W' }, +{ QLocale::Language::Greek, QLocale::Country::Cyprus, 'C', 'Y' }, +{ QLocale::Language::Czech, QLocale::Country::CzechRepublic, 'C', 'Z' }, +{ QLocale::Language::German, QLocale::Country::Germany, 'D', 'E' }, +{ QLocale::Language::Afar, QLocale::Country::Djibouti, 'D', 'J' }, +{ QLocale::Language::Danish, QLocale::Country::Denmark, 'D', 'K' }, +{ QLocale::Language::Spanish, QLocale::Country::DominicanRepublic, 'D', 'O' }, +{ QLocale::Language::Arabic, QLocale::Country::Algeria, 'D', 'Z' }, +{ QLocale::Language::Spanish, QLocale::Country::CeutaAndMelilla, 'E', 'A' }, +{ QLocale::Language::Spanish, QLocale::Country::Ecuador, 'E', 'C' }, +{ QLocale::Language::Estonian, QLocale::Country::Estonia, 'E', 'E' }, +{ QLocale::Language::Arabic, QLocale::Country::Egypt, 'E', 'G' }, +{ QLocale::Language::Arabic, QLocale::Country::WesternSahara, 'E', 'H' }, +{ QLocale::Language::Tigrinya, QLocale::Country::Eritrea, 'E', 'R' }, +{ QLocale::Language::Spanish, QLocale::Country::Spain, 'E', 'S' }, +{ QLocale::Language::Amharic, QLocale::Country::Ethiopia, 'E', 'T' }, +{ QLocale::Language::English, QLocale::Country::EuropeanUnion, 'E', 'U' }, +{ QLocale::Language::German, QLocale::Country::AnyCountry, 'E', 'Z' }, +{ QLocale::Language::Finnish, QLocale::Country::Finland, 'F', 'I' }, +{ QLocale::Language::Faroese, QLocale::Country::FaroeIslands, 'F', 'O' }, +{ QLocale::Language::French, QLocale::Country::France, 'F', 'R' }, +{ QLocale::Language::French, QLocale::Country::Gabon, 'G', 'A' }, +{ QLocale::Language::Georgian, QLocale::Country::Georgia, 'G', 'E' }, +{ QLocale::Language::French, QLocale::Country::FrenchGuiana, 'G', 'F' }, +{ QLocale::Language::Akan, QLocale::Country::Ghana, 'G', 'H' }, +{ QLocale::Language::Greenlandic, QLocale::Country::Greenland, 'G', 'L' }, +{ QLocale::Language::French, QLocale::Country::Guinea, 'G', 'N' }, +{ QLocale::Language::French, QLocale::Country::Guadeloupe, 'G', 'P' }, +{ QLocale::Language::Spanish, QLocale::Country::EquatorialGuinea, 'G', 'Q' }, +{ QLocale::Language::Greek, QLocale::Country::Greece, 'G', 'R' }, +{ QLocale::Language::AnyLanguage, QLocale::Country::SouthGeorgiaAndTheSouthSandwichIslands, 'G', 'S' }, +{ QLocale::Language::Spanish, QLocale::Country::Guatemala, 'G', 'T' }, +{ QLocale::Language::Portuguese, QLocale::Country::GuineaBissau, 'G', 'W' }, +{ QLocale::Language::Chinese, QLocale::Country::HongKong, 'H', 'K' }, +{ QLocale::Language::AnyLanguage, QLocale::Country::HeardAndMcDonaldIslands, 'H', 'M' }, +{ QLocale::Language::Spanish, QLocale::Country::Honduras, 'H', 'N' }, +{ QLocale::Language::Croatian, QLocale::Country::Croatia, 'H', 'R' }, +{ QLocale::Language::Haitian, QLocale::Country::Haiti, 'H', 'T' }, +{ QLocale::Language::Hungarian, QLocale::Country::Hungary, 'H', 'U' }, +{ QLocale::Language::Spanish, QLocale::Country::CanaryIslands, 'I', 'C' }, +{ QLocale::Language::Indonesian, QLocale::Country::Indonesia, 'I', 'D' }, +{ QLocale::Language::Hebrew, QLocale::Country::Israel, 'I', 'L' }, +{ QLocale::Language::Hindi, QLocale::Country::India, 'I', 'N' }, +{ QLocale::Language::Arabic, QLocale::Country::Iraq, 'I', 'Q' }, +{ QLocale::Language::Persian, QLocale::Country::Iran, 'I', 'R' }, +{ QLocale::Language::Icelandic, QLocale::Country::Iceland, 'I', 'S' }, +{ QLocale::Language::Italian, QLocale::Country::Italy, 'I', 'T' }, +{ QLocale::Language::Arabic, QLocale::Country::Jordan, 'J', 'O' }, +{ QLocale::Language::Japanese, QLocale::Country::Japan, 'J', 'P' }, +{ QLocale::Language::Swahili, QLocale::Country::Kenya, 'K', 'E' }, +{ QLocale::Language::Kirghiz, QLocale::Country::Kyrgyzstan, 'K', 'G' }, +{ QLocale::Language::Khmer, QLocale::Country::Cambodia, 'K', 'H' }, +{ QLocale::Language::Arabic, QLocale::Country::Comoros, 'K', 'M' }, +{ QLocale::Language::Korean, QLocale::Country::NorthKorea, 'K', 'P' }, +{ QLocale::Language::Korean, QLocale::Country::SouthKorea, 'K', 'R' }, +{ QLocale::Language::Arabic, QLocale::Country::Kuwait, 'K', 'W' }, +{ QLocale::Language::Russian, QLocale::Country::Kazakhstan, 'K', 'Z' }, +{ QLocale::Language::Lao, QLocale::Country::Laos, 'L', 'A' }, +{ QLocale::Language::Arabic, QLocale::Country::Lebanon, 'L', 'B' }, +{ QLocale::Language::German, QLocale::Country::Liechtenstein, 'L', 'I' }, +{ QLocale::Language::Sinhala, QLocale::Country::SriLanka, 'L', 'K' }, +{ QLocale::Language::SouthernSotho, QLocale::Country::Lesotho, 'L', 'S' }, +{ QLocale::Language::Lithuanian, QLocale::Country::Lithuania, 'L', 'T' }, +{ QLocale::Language::French, QLocale::Country::Luxembourg, 'L', 'U' }, +{ QLocale::Language::Latvian, QLocale::Country::Latvia, 'L', 'V' }, +{ QLocale::Language::Arabic, QLocale::Country::Libya, 'L', 'Y' }, +{ QLocale::Language::Arabic, QLocale::Country::Morocco, 'M', 'A' }, +{ QLocale::Language::French, QLocale::Country::Monaco, 'M', 'C' }, +{ QLocale::Language::Romanian, QLocale::Country::Moldova, 'M', 'D' }, +{ QLocale::Language::Serbian, QLocale::Country::Montenegro, 'M', 'E' }, +{ QLocale::Language::French, QLocale::Country::SaintMartin, 'M', 'F' }, +{ QLocale::Language::Malagasy, QLocale::Country::Madagascar, 'M', 'G' }, +{ QLocale::Language::Macedonian, QLocale::Country::Macedonia, 'M', 'K' }, +{ QLocale::Language::Bambara, QLocale::Country::Mali, 'M', 'L' }, +{ QLocale::Language::Burmese, QLocale::Country::Myanmar, 'M', 'M' }, +{ QLocale::Language::Mongolian, QLocale::Country::Mongolia, 'M', 'N' }, +{ QLocale::Language::Chinese, QLocale::Country::Macau, 'M', 'O' }, +{ QLocale::Language::French, QLocale::Country::Martinique, 'M', 'Q' }, +{ QLocale::Language::Arabic, QLocale::Country::Mauritania, 'M', 'R' }, +{ QLocale::Language::Maltese, QLocale::Country::Malta, 'M', 'T' }, +{ QLocale::Language::Morisyen, QLocale::Country::Mauritius, 'M', 'U' }, +{ QLocale::Language::Divehi, QLocale::Country::Maldives, 'M', 'V' }, +{ QLocale::Language::Spanish, QLocale::Country::Mexico, 'M', 'X' }, +{ QLocale::Language::Malay, QLocale::Country::Malaysia, 'M', 'Y' }, +{ QLocale::Language::Portuguese, QLocale::Country::Mozambique, 'M', 'Z' }, +{ QLocale::Language::Afrikaans, QLocale::Country::Namibia, 'N', 'A' }, +{ QLocale::Language::French, QLocale::Country::NewCaledonia, 'N', 'C' }, +{ QLocale::Language::Hausa, QLocale::Country::Niger, 'N', 'E' }, +{ QLocale::Language::Spanish, QLocale::Country::Nicaragua, 'N', 'I' }, +{ QLocale::Language::Dutch, QLocale::Country::Netherlands, 'N', 'L' }, +{ QLocale::Language::NorwegianBokmal, QLocale::Country::Norway, 'N', 'O' }, +{ QLocale::Language::Nepali, QLocale::Country::Nepal, 'N', 'P' }, +{ QLocale::Language::Arabic, QLocale::Country::Oman, 'O', 'M' }, +{ QLocale::Language::Spanish, QLocale::Country::Panama, 'P', 'A' }, +{ QLocale::Language::Spanish, QLocale::Country::Peru, 'P', 'E' }, +{ QLocale::Language::French, QLocale::Country::FrenchPolynesia, 'P', 'F' }, +{ QLocale::Language::TokPisin, QLocale::Country::PapuaNewGuinea, 'P', 'G' }, +{ QLocale::Language::Filipino, QLocale::Country::Philippines, 'P', 'H' }, +{ QLocale::Language::Urdu, QLocale::Country::Pakistan, 'P', 'K' }, +{ QLocale::Language::Polish, QLocale::Country::Poland, 'P', 'L' }, +{ QLocale::Language::French, QLocale::Country::SaintPierreAndMiquelon, 'P', 'M' }, +{ QLocale::Language::Spanish, QLocale::Country::PuertoRico, 'P', 'R' }, +{ QLocale::Language::Arabic, QLocale::Country::PalestinianTerritories, 'P', 'S' }, +{ QLocale::Language::Portuguese, QLocale::Country::Portugal, 'P', 'T' }, +{ QLocale::Language::Palauan, QLocale::Country::Palau, 'P', 'W' }, +{ QLocale::Language::Guarani, QLocale::Country::Paraguay, 'P', 'Y' }, +{ QLocale::Language::Arabic, QLocale::Country::Qatar, 'Q', 'A' }, +{ QLocale::Language::English, QLocale::Country::OutlyingOceania, 'Q', 'O' }, +{ QLocale::Language::French, QLocale::Country::Reunion, 'R', 'E' }, +{ QLocale::Language::Romanian, QLocale::Country::Romania, 'R', 'O' }, +{ QLocale::Language::Serbian, QLocale::Country::Serbia, 'R', 'S' }, +{ QLocale::Language::Russian, QLocale::Country::Russia, 'R', 'U' }, +{ QLocale::Language::Kinyarwanda, QLocale::Country::Rwanda, 'R', 'W' }, +{ QLocale::Language::Arabic, QLocale::Country::SaudiArabia, 'S', 'A' }, +{ QLocale::Language::French, QLocale::Country::Seychelles, 'S', 'C' }, +{ QLocale::Language::Arabic, QLocale::Country::Sudan, 'S', 'D' }, +{ QLocale::Language::Swedish, QLocale::Country::Sweden, 'S', 'E' }, +{ QLocale::Language::Slovenian, QLocale::Country::Slovenia, 'S', 'I' }, +{ QLocale::Language::NorwegianBokmal, QLocale::Country::SvalbardAndJanMayenIslands, 'S', 'J' }, +{ QLocale::Language::Slovak, QLocale::Country::Slovakia, 'S', 'K' }, +{ QLocale::Language::Italian, QLocale::Country::SanMarino, 'S', 'M' }, +{ QLocale::Language::French, QLocale::Country::Senegal, 'S', 'N' }, +{ QLocale::Language::Somali, QLocale::Country::Somalia, 'S', 'O' }, +{ QLocale::Language::Dutch, QLocale::Country::Suriname, 'S', 'R' }, +{ QLocale::Language::Portuguese, QLocale::Country::SaoTomeAndPrincipe, 'S', 'T' }, +{ QLocale::Language::Spanish, QLocale::Country::ElSalvador, 'S', 'V' }, +{ QLocale::Language::Arabic, QLocale::Country::Syria, 'S', 'Y' }, +{ QLocale::Language::French, QLocale::Country::Chad, 'T', 'D' }, +{ QLocale::Language::French, QLocale::Country::FrenchSouthernTerritories, 'T', 'F' }, +{ QLocale::Language::French, QLocale::Country::Togo, 'T', 'G' }, +{ QLocale::Language::Thai, QLocale::Country::Thailand, 'T', 'H' }, +{ QLocale::Language::Tajik, QLocale::Country::Tajikistan, 'T', 'J' }, +{ QLocale::Language::TokelauLanguage, QLocale::Country::Tokelau, 'T', 'K' }, +{ QLocale::Language::Portuguese, QLocale::Country::EastTimor, 'T', 'L' }, +{ QLocale::Language::Turkmen, QLocale::Country::Turkmenistan, 'T', 'M' }, +{ QLocale::Language::Arabic, QLocale::Country::Tunisia, 'T', 'N' }, +{ QLocale::Language::Tongan, QLocale::Country::Tonga, 'T', 'O' }, +{ QLocale::Language::Turkish, QLocale::Country::Turkey, 'T', 'R' }, +{ QLocale::Language::TuvaluLanguage, QLocale::Country::Tuvalu, 'T', 'V' }, +{ QLocale::Language::Chinese, QLocale::Country::Taiwan, 'T', 'W' }, +{ QLocale::Language::Swahili, QLocale::Country::Tanzania, 'T', 'Z' }, +{ QLocale::Language::Ukrainian, QLocale::Country::Ukraine, 'U', 'A' }, +{ QLocale::Language::Swahili, QLocale::Country::Uganda, 'U', 'G' }, +{ QLocale::Language::Spanish, QLocale::Country::Uruguay, 'U', 'Y' }, +{ QLocale::Language::Uzbek, QLocale::Country::Uzbekistan, 'U', 'Z' }, +{ QLocale::Language::Italian, QLocale::Country::VaticanCityState, 'V', 'A' }, +{ QLocale::Language::Spanish, QLocale::Country::Venezuela, 'V', 'E' }, +{ QLocale::Language::Vietnamese, QLocale::Country::Vietnam, 'V', 'N' }, +{ QLocale::Language::Bislama, QLocale::Country::Vanuatu, 'V', 'U' }, +{ QLocale::Language::French, QLocale::Country::WallisAndFutunaIslands, 'W', 'F' }, +{ QLocale::Language::Samoan, QLocale::Country::Samoa, 'W', 'S' }, +{ QLocale::Language::Albanian, QLocale::Country::Kosovo, 'X', 'K' }, +{ QLocale::Language::Arabic, QLocale::Country::Yemen, 'Y', 'E' }, +{ QLocale::Language::French, QLocale::Country::Mayotte, 'Y', 'T' }, +{ QLocale::Language::Shona, QLocale::Country::Zimbabwe, 'Z', 'W' }, +{ QLocale::Language::AnyLanguage, QLocale::Country::AnyCountry, 0, 0 }, +}; + +static_assert( (sizeof(country_data_table) / sizeof(CountryData)) == country_data_size, "Table size mismatch for CountryData" ); + + +// END Generated from CLDR data diff --git a/src/libcalamares/locale/Lookup.cpp b/src/libcalamares/locale/Lookup.cpp index 4ab8a79c4..849668742 100644 --- a/src/libcalamares/locale/Lookup.cpp +++ b/src/libcalamares/locale/Lookup.cpp @@ -18,7 +18,7 @@ #include "Lookup.h" -// No data yet +#include "CountryData_p.cpp" namespace Calamares { -- cgit v1.2.2 From b51e22b520226eb54cf8dcbef3baeec5fc7805a9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 10 May 2019 13:46:35 +0200 Subject: [libcalamares] Implement locale lookups for 2-character codes --- src/libcalamares/locale/Lookup.cpp | 57 ++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/libcalamares/locale/Lookup.cpp b/src/libcalamares/locale/Lookup.cpp index 849668742..d38d417d4 100644 --- a/src/libcalamares/locale/Lookup.cpp +++ b/src/libcalamares/locale/Lookup.cpp @@ -22,30 +22,69 @@ namespace Calamares { + +struct TwoChar +{ + TwoChar( const QString& code ) + : cc1(0) + , cc2(0) + { + if ( code.length() == 2 ) + { + cc1 = code[0].toLatin1(); + cc2 = code[1].toLatin1(); + } + } + + char cc1; + char cc2; +}; + +static const CountryData* lookup( TwoChar c ) +{ + if ( !c.cc1 ) + return nullptr; + + const CountryData* p = std::find_if(country_data_table, country_data_table + country_data_size, + [c=c]( const CountryData& d ){ return (d.cc1 == c.cc1) && (d.cc2 == c.cc2); } + ); + if ( p == country_data_table + country_data_size ) + return nullptr; + return p; +} QLocale::Country countryForCode(const QString& code) { - return QLocale::Country::AnyCountry; + const CountryData* p = lookup( TwoChar( code ) ); + return p ? p->c : QLocale::Country::AnyCountry; } -QLocale countryLocale(const QString& code) +QLocale::Language languageForCountry(const QString& code) { - return QLocale(); + const CountryData* p = lookup( TwoChar( code ) ); + return p ? p->l : QLocale::Language::AnyLanguage; } -QLocale::Language languageForCountry(const QString& code) +QPair countryData(const QString& code) { - return QLocale::Language::AnyLanguage; + const CountryData* p = lookup( TwoChar( code ) ); + return p ? qMakePair( p->c, p->l ) : qMakePair( QLocale::Country::AnyCountry, QLocale::Language::AnyLanguage ); } -QLocale::Language languageForCountry(QLocale::Country country) +QLocale countryLocale(const QString& code) { - return QLocale::Language::AnyLanguage; + auto p = countryData( code ); + return QLocale( p.second, p.first ); } -QPair countryData(const QString& code) +QLocale::Language languageForCountry(QLocale::Country country) { - return qMakePair( QLocale::Country::AnyCountry, QLocale::Language::AnyLanguage ); + const CountryData* p = std::find_if(country_data_table, country_data_table + country_data_size, + [c=country]( const CountryData& d ){ return d.c == c; } + ); + if ( p == country_data_table + country_data_size ) + return QLocale::Language::AnyLanguage; + return p->l; } } // namespace -- cgit v1.2.2