支持语言国家名称配置

Signed-off-by: zhangdd_ewan <zhangdongdong50@huawei.com>
This commit is contained in:
zhangdd_ewan 2023-06-05 12:18:33 +08:00
parent 9c74edab32
commit c63002c8d8
7 changed files with 259 additions and 13 deletions

View File

@ -111,17 +111,21 @@ ohos_shared_library("intl_util") {
deps = [
":config_locales_xml",
":config_regions_xml",
":dialect_languages_xml",
":en_Latn_lang_xml",
":en_Latn_region_xml",
":forbidden_languages_xml",
":forbidden_regions_xml",
":lang_supported_locales",
":language_config.para",
":language_config.para.dac",
":region_supported_locales_xml",
":root_timezone_xml",
":timezones_xml",
":tz_pixel_dat",
":white_languages_xml",
":zh_Hans_lang_xml",
":zh_Hans_region_xml",
":zh_Hans_timezone_xml",
"//third_party/icu/icu4c:ohos_icudat",
"//third_party/icu/icu4c:shared_icui18n",
@ -258,3 +262,31 @@ ohos_prebuilt_etc("tz_pixel_dat") {
part_name = "i18n"
subsystem_name = "global"
}
ohos_prebuilt_etc("region_supported_locales_xml") {
source = "//base/global/i18n/frameworks/intl/etc/region/supported_regions.xml"
module_install_dir = "usr/ohos_locale_config/region/"
part_name = "i18n"
subsystem_name = "global"
}
ohos_prebuilt_etc("zh_Hans_region_xml") {
source = "//base/global/i18n/frameworks/intl/etc/region/zh-Hans.xml"
module_install_dir = "usr/ohos_locale_config/region/"
part_name = "i18n"
subsystem_name = "global"
}
ohos_prebuilt_etc("en_Latn_region_xml") {
source = "//base/global/i18n/frameworks/intl/etc/region/en-Latn-US.xml"
module_install_dir = "usr/ohos_locale_config/region/"
part_name = "i18n"
subsystem_name = "global"
}
ohos_prebuilt_etc("dialect_languages_xml") {
source = "//base/global/i18n/frameworks/intl/etc/dialect_languages.xml"
module_install_dir = "usr/ohos_locale_config/"
part_name = "i18n"
subsystem_name = "global"
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2021-2022 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<dialect_langs>
<item>zh</item>
<item>ro</item>
<item>fa</item>
</dialect_langs>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2022 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<regions>
<region>
<id>HK</id>
<display_name>Hong Kong, China</display_name>
</region>
<region>
<id>MO</id>
<display_name>Macao, China</display_name>
</region>
<region>
<id>TW</id>
<display_name>Taiwan, China</display_name>
</region>
</regions>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2021 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<supported_regions>
<item>en-Latn</item>
<item>zh-Hans</item>
</supported_regions>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2022 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<regions>
<region>
<id>HK</id>
<display_name>中国香港</display_name>
</region>
<region>
<id>MO</id>
<display_name>中国澳门</display_name>
</region>
<region>
<id>TW</id>
<display_name>中国台湾</display_name>
</region>
</regions>

View File

@ -83,13 +83,25 @@ private:
static const char *FORBIDDEN_LANGUAGES_PATH;
static const char *FORBIDDEN_LANGUAGES_NAME;
static const char *SUPPORT_LOCALES_PATH;
static const char *SUPPORT_LOCALES_NAME;
static const char *DEFAULT_LOCALE;
static const char *supportLocalesTag;
static const char *LANG_PATH;
static const char *rootTag;
static const char *secondRootTag;
static const char *rootRegion;
static const char *secondRootRegion;
static const uint32_t ELEMENT_NUM = 2;
static const char *SUPPORTED_LANGUAGE_EN_LATN_PATH;
static const char *SUPPORTED_LANGUAGE_EN_LATN_NAME;
static const char *OVERRIDE_SUPPORTED_REGIONS_NAME;
static const char *OVERRIDE_SUPPORTED_REGIONS_PATH;
static const char *DIALECT_LANGS_PATH;
static const char *DIALECT_LANGS_NAME;
static const char *REGION_PATH;
static const std::unordered_set<std::string>& GetSupportedLocales();
static const std::unordered_set<std::string>& GetForbiddenRegions();
static const std::unordered_set<std::string>& GetSupportedRegions();
@ -101,11 +113,16 @@ private:
static void Expunge(std::unordered_set<std::string> &src, const std::unordered_set<std::string> &another);
static std::string GetMainLanguage(const std::string &language);
static std::string GetDsiplayLanguageWithDialect(const std::string &language, const std::string &displayLocale);
static std::string GetDisplayOverrideRegion(const std::string &region, const std::string &displayLocale);
static std::string ComputeLocale(const std::string &displayLocale);
static void ReadLangData(const char *langDataPath);
static void ReadRegionData(const char *regionDataPath);
static void ProcessForbiddenRegions(const std::unordered_set<std::string> &forbiddenRegions);
static std::unordered_set<std::string> supportedLocales;
static std::unordered_set<std::string> supportedRegions;
static std::unordered_set<std::string> supportLocales;
static std::unordered_set<std::string> dialectLang;
static std::unordered_set<std::string> overrideSupportedRegions;
static std::unordered_set<std::string> blockedLanguages;
static std::unordered_set<std::string> blockedRegions;
static std::unordered_map<std::string, std::unordered_set<std::string>> blockedLanguageRegions;
@ -114,14 +131,16 @@ private:
static std::unordered_map<std::string, std::string> dialectMap;
static std::unordered_map<std::string, std::string> localDigitMap;
static std::map<std::string, std::string> locale2DisplayName;
static std::map<std::string, std::string> region2DisplayName;
static std::string currentDialectLocale;
static std::string currentOverrideRegion;
// static std::string currentRegion;
static std::set<std::string> validCaTag;
static std::set<std::string> validCoTag;
static std::set<std::string> validKnTag;
static std::set<std::string> validKfTag;
static std::set<std::string> validNuTag;
static std::set<std::string> validHcTag;
static std::set<std::string> dialectLang;
static bool listsInitialized;
static bool InitializeLists();
};

View File

@ -66,13 +66,25 @@ const char *LocaleConfig::SUPPORTED_LOCALES_PATH = "/system/usr/ohos_locale_conf
const char *LocaleConfig::SUPPORTED_REGIONS_PATH = "/system/usr/ohos_locale_config/supported_regions.xml";
const char *LocaleConfig::WHITE_LANGUAGES_PATH = "/system/usr/ohos_locale_config/white_languages.xml";
const char *LocaleConfig::SUPPORT_LOCALES_PATH = "/etc/ohos_lang_config/supported_locales.xml";
const char *LocaleConfig::SUPPORT_LOCALES_NAME = "supported_locales";
const char *LocaleConfig::DIALECT_LANGS_PATH = "/system/usr/ohos_locale_config/dialect_languages.xml";
const char *LocaleConfig::DIALECT_LANGS_NAME = "dialect_langs";
const char *LocaleConfig::OVERRIDE_SUPPORTED_REGIONS_NAME = "supported_regions";
const char *LocaleConfig::OVERRIDE_SUPPORTED_REGIONS_PATH =
"/system/usr/ohos_locale_config/region/supported_regions.xml";
const char *LocaleConfig::DEFAULT_LOCALE = "en-Latn";
const char *LocaleConfig::supportLocalesTag = "supported_locales";
const char *LocaleConfig::LANG_PATH = "/etc/ohos_lang_config/";
const char *LocaleConfig::REGION_PATH = "/system/usr/ohos_locale_config/region/";
const char *LocaleConfig::rootTag = "languages";
const char *LocaleConfig::secondRootTag = "lang";
const char *LocaleConfig::rootRegion = "regions";
const char *LocaleConfig::secondRootRegion = "region";
unordered_set<string> LocaleConfig::supportedLocales;
unordered_set<string> LocaleConfig::supportLocales;
unordered_set<string> LocaleConfig::supportedRegions;
unordered_set<string> LocaleConfig::overrideSupportedRegions;
unordered_set<string> LocaleConfig::dialectLang;
unordered_set<string> LocaleConfig::blockedLanguages;
unordered_set<string> LocaleConfig::blockedRegions;
unordered_map<string, unordered_set<string>> LocaleConfig::blockedLanguageRegions;
@ -117,7 +129,9 @@ unordered_map<string, string> LocaleConfig::localDigitMap {
std::map<std::string, std::string> LocaleConfig::supportedDialectLocales;
std::map<string, string> LocaleConfig::locale2DisplayName {};
std::map<string, string> LocaleConfig::region2DisplayName {};
std::string LocaleConfig::currentDialectLocale = "";
std::string LocaleConfig::currentOverrideRegion = "";
set<std::string> LocaleConfig::validCaTag {
"buddhist",
@ -186,11 +200,11 @@ set<std::string> LocaleConfig::validHcTag {
"h11",
"h24",
};
set<std::string> LocaleConfig::dialectLang {
"zh",
"ro",
"fa",
};
// set<std::string> LocaleConfig::dialectLang {
// "zh",
// "ro",
// "fa",
// };
static unordered_map<string, string> g_languageMap = {
{ "zh-Hans", "zh-Hans" },
@ -627,6 +641,9 @@ void LocaleConfig::GetListFromFile(const char *path, const char *resourceName, u
break;
}
}
for (auto it = ret.begin(); it != ret.end(); it++) {
HiLog::Info(LABEL, "dyh ret: %{public}s", it->c_str());
}
xmlFreeDoc(doc);
}
@ -671,6 +688,9 @@ bool LocaleConfig::InitializeLists()
GetListFromFile(FORBIDDEN_LANGUAGES_PATH, FORBIDDEN_LANGUAGES_NAME, blockedLanguages);
Expunge(whiteLanguages, blockedLanguages);
GetListFromFile(SUPPORTED_LOCALES_PATH, SUPPORTED_LOCALES_NAME, supportedLocales);
GetListFromFile(SUPPORT_LOCALES_PATH, SUPPORT_LOCALES_NAME, supportLocales);
GetListFromFile(OVERRIDE_SUPPORTED_REGIONS_PATH, OVERRIDE_SUPPORTED_REGIONS_NAME, overrideSupportedRegions);
GetListFromFile(DIALECT_LANGS_PATH, DIALECT_LANGS_NAME, dialectLang);
return true;
}
@ -849,6 +869,52 @@ void LocaleConfig::ReadLangData(const char *langDataPath)
xmlFreeDoc(doc);
}
void LocaleConfig::ReadRegionData(const char *regionDataPath)
{
xmlKeepBlanksDefault(0);
if (regionDataPath == nullptr) {
return;
}
xmlDocPtr doc = xmlParseFile(regionDataPath);
if (!doc) {
HiLog::Info(LABEL, "can not open region data file");
return;
}
xmlNodePtr cur = xmlDocGetRootElement(doc);
if (cur) {
HiLog::Info(LABEL, "cur pointer is true");
}
if (!cur || xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>(rootRegion))) {
xmlFreeDoc(doc);
HiLog::Info(LABEL, "parse region data file failed");
return;
}
cur = cur->xmlChildrenNode;
while (cur != nullptr && !xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>(secondRootRegion))) {
xmlChar *regionContents[ELEMENT_NUM] = { 0 };
xmlNodePtr regionValue = cur->xmlChildrenNode;
for (size_t i = 0; i < ELEMENT_NUM; i++) {
if (regionValue != nullptr) {
regionContents[i] = xmlNodeGetContent(regionValue);
regionValue = regionValue->next;
} else {
break;
}
}
// 0 represents langid index, 1 represents displayname index
region2DisplayName.insert(
std::make_pair<std::string, std::string>(reinterpret_cast<const char *>(regionContents[0]),
reinterpret_cast<const char *>(regionContents[1])));
for (size_t i = 0; i < ELEMENT_NUM; i++) {
if (regionContents[i] != nullptr) {
xmlFree(regionContents[i]);
}
}
cur = cur->next;
}
xmlFreeDoc(doc);
}
string LocaleConfig::GetDsiplayLanguageWithDialect(const std::string &localeStr, const std::string &displayLocale)
{
std::string finalLocale = ComputeLocale(displayLocale);
@ -884,6 +950,37 @@ string LocaleConfig::GetDsiplayLanguageWithDialect(const std::string &localeStr,
return "";
}
string LocaleConfig::GetDisplayOverrideRegion(const std::string &region, const std::string &displayLocale)
{
UErrorCode status = U_ZERO_ERROR;
icu::Locale originLocale;
icu::UnicodeString displayRegion;
if (displayLocale.compare(currentOverrideRegion) != 0) {
std::string xmlPath = REGION_PATH + displayLocale + ".xml";
region2DisplayName.clear();
ReadRegionData(xmlPath.c_str());
currentOverrideRegion = displayLocale;
}
if (region2DisplayName.find(region) != region2DisplayName.end()) {
return region2DisplayName.at(region);
} else {
icu::Locale locale = icu::Locale::forLanguageTag(displayLocale, status);
if (status != U_ZERO_ERROR) {
return "";
}
if (IsValidRegion(region)) {
icu::LocaleBuilder builder = icu::LocaleBuilder().setRegion(region);
originLocale = builder.build(status);
} else {
originLocale = icu::Locale::forLanguageTag(region, status);
}
originLocale.getDisplayCountry(locale, displayRegion);
std::string result;
displayRegion.toUTF8String(result);
return result;
}
}
string LocaleConfig::GetDisplayRegion(const string &region, const string &displayLocale, bool sentenceCase)
{
UErrorCode status = U_ZERO_ERROR;
@ -905,15 +1002,20 @@ string LocaleConfig::GetDisplayRegion(const string &region, const string &displa
if (status != U_ZERO_ERROR) {
return "";
}
icu::UnicodeString unistr;
icu::UnicodeString displayRegion;
originLocale.getDisplayCountry(locale, displayRegion);
if (sentenceCase) {
UChar32 ch = ucase_toupper(displayRegion.char32At(0));
displayRegion.replace(0, 1, ch);
std::string result;
if (overrideSupportedRegions.find(displayLocale) != overrideSupportedRegions.end()) {
result = GetDisplayOverrideRegion(region, displayLocale);
} else {
originLocale.getDisplayCountry(locale, displayRegion);
displayRegion.toUTF8String(result);
}
string temp;
displayRegion.toUTF8String(temp);
return temp;
if (sentenceCase) {
char ch = static_cast<char>(toupper(result[0]));
return result.replace(0, 1, 1, ch);
}
return result;
}
bool LocaleConfig::IsRTL(const string &locale)