add collator and plural_rules to i18n

Signed-off-by: sunyaozu <sunyaozu@huawei.com>
This commit is contained in:
sunyaozu 2021-11-04 15:15:52 +08:00
parent a201c5c893
commit 975aa2569a
7 changed files with 2441 additions and 1270 deletions

View File

@ -46,11 +46,13 @@ ohos_shared_library("intl_util") {
"//third_party/protobuf/src",
]
sources = [
"src/collator.cpp",
"src/date_time_format.cpp",
"src/locale_config.cpp",
"src/locale_info.cpp",
"src/number_format.cpp",
"src/phone_number_format.cpp",
"src/plural_rules.cpp",
]
cflags_cc = [
"-Wall",

View File

@ -0,0 +1,68 @@
/*
* 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.
*/
#ifndef GLOBAL_I18N_STANDARD_COLLATOR_H
#define GLOBAL_I18N_STANDARD_COLLATOR_H
#include <string>
#include <map>
#include <vector>
#include <set>
#include "unicode/locid.h"
#include "unicode/coll.h"
#include "locale_info.h"
namespace OHOS {
namespace Global {
namespace I18n {
class Collator {
public:
Collator(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options);
~Collator();
int32_t Compare(const std::string &first, const std::string &second);
void ResolvedOptions(std::map<std::string, std::string> &options);
private:
std::string localeStr;
std::string localeMatcher;
std::string usage;
std::string sensitivity;
std::string ignorePunctuation;
std::string numeric;
std::string caseFirst;
std::string collation;
LocaleInfo *localeInfo;
icu::Locale locale;
icu::Collator *collatorPtr;
std::set<std::string> GetValidLocales();
std::string ParseOption(std::map<std::string, std::string> &options, const std::string &key);
void ParseAllOptions(std::map<std::string, std::string> &options);
bool IsValidCollation(std::string &collation, UErrorCode &status);
void SetCollation(UErrorCode &status);
void SetUsage(UErrorCode &status);
void SetNumeric(UErrorCode &status);
void SetCaseFirst(UErrorCode &status);
void SetSensitivity(UErrorCode &status);
void SetIgnorePunctuation(UErrorCode &status);
bool InitCollator();
};
}
}
}
#endif // GLOBAL_I18N_STANDARD_COLLATOR_H

View File

@ -0,0 +1,63 @@
/*
* 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.
*/
#ifndef GLOBAL_I18N_STANDARD_PLURAL_RULES_H
#define GLOBAL_I18N_STANDARD_PLURAL_RULES_H
#include <vector>
#include <string>
#include <map>
#include <set>
#include "unicode/locid.h"
#include "unicode/plurrule.h"
#include "unicode/numberformatter.h"
#include "locale_info.h"
namespace OHOS {
namespace Global {
namespace I18n {
class PluralRules {
public:
PluralRules(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options);
~PluralRules();
std::string Select(double number);
private:
std::string localeStr;
LocaleInfo *localeInfo;
icu::Locale locale;
icu::PluralRules *pluralRules;
icu::number::LocalizedNumberFormatter numberFormatter;
std::string localeMatcher;
std::string type;
int minInteger;
int minFraction;
int maxFraction;
int minSignificant;
int maxSignificant;
std::set<std::string> GetValidLocales();
std::string ParseOption(std::map<std::string, std::string> &options, const std::string &key);
void ParseAllOptions(std::map<std::string, std::string> &options);
void InitPluralRules(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options);
void InitNumberFormatter();
};
}
}
}
#endif // GLOBAL_I18N_STANDARD_PLURAL_RULES_H

View File

@ -0,0 +1,280 @@
//
// Created by s00619675 on 2021/10/25.
//
/*
* 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.
*/
#include "collator.h"
#include <stringpiece.h>
#include "unicode/ucol.h"
#include "unicode/errorcode.h"
#include "unicode/uloc.h"
namespace OHOS {
namespace Global {
namespace I18n {
std::string Collator::ParseOption(std::map<std::string, std::string> &options, const std::string &key)
{
std::map<std::string, std::string>::iterator it = options.find(key);
if (it != options.end()) {
return it->second;
} else {
return "";
}
}
void Collator::ParseAllOptions(std::map<std::string, std::string> &options)
{
localeMatcher = ParseOption(options, "localeMatcher");
if (localeMatcher == "") {
localeMatcher = "best fit";
}
usage = ParseOption(options, "usage");
if (usage == "") {
usage = "sort";
}
sensitivity = ParseOption(options, "sensitivity");
if (sensitivity == "") {
sensitivity = "variant";
}
ignorePunctuation = ParseOption(options, "ignorePunctuation");
if (ignorePunctuation == "") {
ignorePunctuation = "false";
}
numeric = ParseOption(options, "numeric");
caseFirst = ParseOption(options, "caseFirst");
collation = ParseOption(options, "collation");
}
std::set<std::string> Collator::GetValidLocales()
{
int32_t validCount = 1;
const icu::Locale *validLocaleArray = icu::Locale::getAvailableLocales(validCount);
std::set<std::string> allValidLocales;
for (int i = 0; i < validCount; i++) {
allValidLocales.insert(validLocaleArray[i].getLanguage());
}
return allValidLocales;
}
Collator::Collator(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options)
{
ParseAllOptions(options);
UErrorCode status = UErrorCode::U_ZERO_ERROR;
std::set<std::string> allValidLocales = GetValidLocales();
for (size_t i = 0; i < localeTags.size(); i++) {
std::string curLocale = localeTags[i];
locale = icu::Locale::forLanguageTag(icu::StringPiece(curLocale), status);
if (allValidLocales.count(locale.getLanguage()) > 0) {
localeInfo = new LocaleInfo(curLocale, options);
locale = localeInfo->GetLocale();
localeStr = localeInfo->GetBaseName();
bool createSuccess = InitCollator();
if (!createSuccess) {
continue;
}
break;
}
}
if (localeTags.size() == 0) {
icu::Locale defaultLocale;
localeInfo = new LocaleInfo(defaultLocale.getName(), options);
locale = localeInfo->GetLocale();
localeStr = localeInfo->GetBaseName();
InitCollator();
}
}
bool Collator::IsValidCollation(std::string &collation, UErrorCode &status)
{
const char *currentCollation = uloc_toLegacyType("collation", collation.c_str());
std::unique_ptr<icu::StringEnumeration> enumeration(
icu::Collator::getKeywordValuesForLocale("collation", icu::Locale(locale.getBaseName()), false, status));
int length;
const char *validCollations = enumeration->next(&length, status);
while (validCollations != nullptr) {
if (strcmp(validCollations, currentCollation) == 0) {
return true;
}
validCollations = enumeration->next(&length, status);
}
return false;
}
void Collator::SetCollation(UErrorCode &status)
{
if (collation != "") {
if (IsValidCollation(collation, status)) {
locale.setUnicodeKeywordValue("co", collation, status);
} else {
collation = "default";
locale.setUnicodeKeywordValue("co", nullptr, status);
}
} else {
collation = localeInfo->GetCollation();
if (collation != "") {
if (IsValidCollation(collation, status)) {
locale.setUnicodeKeywordValue("co", collation, status);
} else {
locale.setUnicodeKeywordValue("co", nullptr, status);
collation = "default";
}
} else {
locale.setUnicodeKeywordValue("co", nullptr, status);
collation = "default";
}
}
}
void Collator::SetUsage(UErrorCode &status)
{
if (usage == "search") {
collation = "default";
locale.setUnicodeKeywordValue("co", nullptr, status);
}
}
void Collator::SetNumeric(UErrorCode &status)
{
if (numeric == "") {
numeric = localeInfo->GetNumeric();
if (numeric != "true" && numeric != "false") {
numeric = "false";
}
}
if (numeric == "true") {
collatorPtr->setAttribute(UColAttribute::UCOL_NUMERIC_COLLATION,
UColAttributeValue::UCOL_ON, status);
} else {
collatorPtr->setAttribute(UColAttribute::UCOL_NUMERIC_COLLATION,
UColAttributeValue::UCOL_OFF, status);
}
}
void Collator::SetCaseFirst(UErrorCode &status)
{
if (caseFirst == "") {
caseFirst = localeInfo->GetCaseFirst();
if (caseFirst != "upper" && caseFirst != "lower" && caseFirst != "false") {
caseFirst = "false";
}
}
if (caseFirst == "upper") {
collatorPtr->setAttribute(UColAttribute::UCOL_CASE_FIRST,
UColAttributeValue::UCOL_UPPER_FIRST, status);
} else if (caseFirst == "lower") {
collatorPtr->setAttribute(UColAttribute::UCOL_CASE_FIRST,
UColAttributeValue::UCOL_LOWER_FIRST, status);
} else {
collatorPtr->setAttribute(UColAttribute::UCOL_CASE_FIRST,
UColAttributeValue::UCOL_OFF, status);
}
}
void Collator::SetSensitivity(UErrorCode &status)
{
if (sensitivity == "base") {
collatorPtr->setStrength(icu::Collator::PRIMARY);
} else if (sensitivity == "accent") {
collatorPtr->setStrength(icu::Collator::SECONDARY);
} else if (sensitivity == "case") {
collatorPtr->setStrength(icu::Collator::PRIMARY);
collatorPtr->setAttribute(UColAttribute::UCOL_CASE_LEVEL,
UColAttributeValue::UCOL_ON, status);
} else {
collatorPtr->setStrength(icu::Collator::TERTIARY);
}
}
void Collator::SetIgnorePunctuation(UErrorCode &status)
{
if (ignorePunctuation == "true") {
collatorPtr->setAttribute(UColAttribute::UCOL_ALTERNATE_HANDLING,
UColAttributeValue::UCOL_SHIFTED, status);
}
}
bool Collator::InitCollator()
{
UErrorCode status = UErrorCode::U_ZERO_ERROR;
SetCollation(status);
SetUsage(status);
collatorPtr = icu::Collator::createInstance(locale, status);
SetNumeric(status);
SetSensitivity(status);
SetIgnorePunctuation(status);
if (collatorPtr == nullptr) {
if (localeInfo != nullptr) {
delete localeInfo;
localeInfo = nullptr;
}
return false;
}
return true;
}
Collator::~Collator()
{
if (localeInfo != nullptr) {
delete localeInfo;
localeInfo = nullptr;
}
if (collatorPtr != nullptr) {
delete collatorPtr;
collatorPtr = nullptr;
}
}
int32_t Collator::Compare(const std::string &first, const std::string &second)
{
icu::Collator::EComparisonResult result = collatorPtr->compare(icu::UnicodeString(first.data(), first.length()),
icu::UnicodeString(second.data(), second.length()));
if (result == icu::Collator::EComparisonResult::LESS) {
return -1;
} else if (result == icu::Collator::EComparisonResult::EQUAL) {
return 0;
} else {
return 1;
}
}
void Collator::ResolvedOptions(std::map<std::string, std::string> &options)
{
options.insert(
{
{ "locale", localeStr },
{ "usage", usage },
{ "sensitivity", sensitivity },
{ "ignorePunctuation", ignorePunctuation },
{ "numeric", numeric },
{ "caseFirst", caseFirst },
{ "collation", collation }
}
);
}
}
}
}

View File

@ -0,0 +1,175 @@
/*
* 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.
*/
#include <stringpiece.h>
#include "hilog/log.h"
#include "unicode/unistr.h"
#include "plural_rules.h"
namespace OHOS {
namespace Global {
namespace I18n {
static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "IntlJs" };
using namespace OHOS::HiviewDFX;
std::set<std::string> PluralRules::GetValidLocales()
{
int32_t validCount = 1;
const icu::Locale *validLocales = icu::Locale::getAvailableLocales(validCount);
std::set<std::string> allValidLocales;
for (int i = 0; i < validCount; i++) {
allValidLocales.insert(validLocales[i].getLanguage());
}
return allValidLocales;
}
std::string PluralRules::ParseOption(std::map<std::string, std::string> &options, const std::string &key)
{
std::map<std::string, std::string>::iterator it = options.find(key);
if (it != options.end()) {
return it->second;
} else {
return "";
}
}
void PluralRules::ParseAllOptions(std::map<std::string, std::string> &options)
{
localeMatcher = ParseOption(options, "localeMatcher");
localeMatcher = (localeMatcher == "") ? "best fit" : localeMatcher;
type = ParseOption(options, "type");
type = (type == "") ? "cardinal" : type;
std::string minIntegerStr = ParseOption(options, "minimumIntegerDigits");
minInteger = (minIntegerStr == "") ? 1 : std::stoi(minIntegerStr);
minFraction = 0;
maxFraction = 0;
std::string minFractionStr = ParseOption(options, "minimumFractionDigits");
std::string maxFractionStr = ParseOption(options, "maximumFractionDigits");
std::string minSignificantStr = ParseOption(options, "minimumSignificantDigits");
std::string maxSignificantStr = ParseOption(options, "maximumSignificantDigits");
if (minSignificantStr != "" || maxSignificantStr != "") {
// 1 is the default value of minSignificant
minSignificant = (minSignificantStr == "") ? 1 : std::stoi(minSignificantStr);
// 21 is the default value of maxSignificant
maxSignificant = (maxSignificantStr == "") ? 21 : std::stoi(maxSignificantStr);
} else {
minSignificant = 0;
maxSignificant = 0;
if (minFractionStr != "" || maxFractionStr != "") {
minFraction = (minFractionStr == "") ? 0 : std::stoi(minFractionStr);
int maxFractionDefault = std::max(3, minFraction);
maxFraction = (maxFractionStr == "") ? maxFractionDefault : std::stoi(maxFractionStr);
if (minFraction > maxFraction) {
HiLog::Error(LABEL, "minimumFractionDigits is greater than maximumFractionDigits");
return;
}
} else {
minFraction = 0; // 0 is the default value of minFraction.
maxFraction = 3; // 3 is the default value of maxFraction
}
}
}
void PluralRules::InitPluralRules(std::vector<std::string> &localeTags,
std::map<std::string, std::string> &options)
{
UPluralType uPluralType = (type == "cardinal") ? UPLURAL_TYPE_CARDINAL : UPLURAL_TYPE_ORDINAL;
UErrorCode status = UErrorCode::U_ZERO_ERROR;
std::set<std::string> allValidLocales = GetValidLocales();
for (size_t i = 0; i < localeTags.size(); i++) {
std::string curLocale = localeTags[i];
locale = icu::Locale::forLanguageTag(icu::StringPiece(curLocale), status);
if (allValidLocales.count(locale.getLanguage()) > 0) {
localeInfo = new LocaleInfo(curLocale, options);
locale = localeInfo->GetLocale();
localeStr = localeInfo->GetBaseName();
pluralRules = icu::PluralRules::forLocale(locale, uPluralType, status);
if (status != UErrorCode::U_ZERO_ERROR || pluralRules == nullptr) {
continue;
}
}
}
if (localeTags.size() == 0) {
icu::Locale defaultLocale;
localeInfo = new LocaleInfo(defaultLocale.getName(), options);
locale = localeInfo->GetLocale();
localeStr = localeInfo->GetBaseName();
pluralRules = icu::PluralRules::forLocale(locale, uPluralType, status);
}
if (status != UErrorCode::U_ZERO_ERROR || pluralRules == nullptr) {
HiLog::Error(LABEL, "PluralRules object created failed");
return;
}
}
void PluralRules::InitNumberFormatter()
{
numberFormatter = icu::number::NumberFormatter::withLocale(locale).roundingMode(UNUM_ROUND_HALFUP);
if (minInteger > 1) {
numberFormatter = numberFormatter.integerWidth(icu::number::IntegerWidth::zeroFillTo(minInteger));
}
if (minSignificant >= 0) {
if (minSignificant > 0) {
icu::number::Precision precision = icu::number::Precision::minMaxSignificantDigits(minSignificant,
maxSignificant);
numberFormatter.precision(precision);
} else {
icu::number::Precision precision = icu::number::Precision::minMaxFraction(minFraction, maxFraction);
numberFormatter.precision(precision);
}
}
}
PluralRules::PluralRules(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options)
{
ParseAllOptions(options);
InitPluralRules(localeTags, options);
InitNumberFormatter();
}
PluralRules::~PluralRules()
{
if (localeInfo != nullptr) {
delete localeInfo;
localeInfo = nullptr;
}
if (pluralRules == nullptr) {
delete pluralRules;
pluralRules = nullptr;
}
}
std::string PluralRules::Select(double number)
{
UErrorCode status = UErrorCode::U_ZERO_ERROR;
icu::number::FormattedNumber formattedNumber = numberFormatter.formatDouble(number, status);
if (status != UErrorCode::U_ZERO_ERROR) {
status = UErrorCode::U_ZERO_ERROR;
formattedNumber = numberFormatter.formatDouble(number, status);
}
icu::UnicodeString unicodeString = pluralRules->select(formattedNumber, status);
std::string result;
unicodeString.toUTF8String(result);
return result;
}
}
}
}

223
interfaces/js/kits/include/intl_addon.h Executable file → Normal file
View File

@ -1,103 +1,122 @@
/*
* 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.
*/
#ifndef INTL_ADDON_H
#define INTL_ADDON_H
#include <string>
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "locale_info.h"
#include "date_time_format.h"
#include "number_format.h"
namespace OHOS {
namespace Global {
namespace I18n {
static void GetLocaleTags(napi_env env, napi_value rawLocaleTag, std::vector<std::string> &localeTags);
static void GetOptionValue(napi_env env, napi_value options, const std::string &optionName,
std::map<std::string, std::string> &map);
static void GetBoolOptionValue(napi_env env, napi_value options, const std::string &optionName,
std::map<std::string, std::string> &map);
static void GetIntegerOptionValue(napi_env env, napi_value options, const std::string &optionName,
std::map<std::string, std::string> &map);
static void GetDateOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
static void GetNumberOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
static void SetOptionProperties(napi_env env, napi_value &result, std::map<std::string, std::string> &options,
const std::string &option);
static void SetIntegerOptionProperties(napi_env env, napi_value &result,
std::map<std::string, std::string> &options, const std::string &option);
static void SetBooleanOptionProperties(napi_env env, napi_value &result,
std::map<std::string, std::string> &options, const std::string &option);
class IntlAddon {
public:
static napi_value InitLocale(napi_env env, napi_value exports);
static napi_value InitDateTimeFormat(napi_env env, napi_value exports);
static napi_value InitNumberFormat(napi_env env, napi_value exports);
static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
IntlAddon();
virtual ~IntlAddon();
private:
static napi_value DateTimeFormatConstructor(napi_env env, napi_callback_info info);
static napi_value NumberFormatConstructor(napi_env env, napi_callback_info info);
static napi_value LocaleConstructor(napi_env env, napi_callback_info info);
static napi_value GetLanguage(napi_env env, napi_callback_info info);
static napi_value GetScript(napi_env env, napi_callback_info info);
static napi_value GetRegion(napi_env env, napi_callback_info info);
static napi_value GetBaseName(napi_env env, napi_callback_info info);
static napi_value GetCalendar(napi_env env, napi_callback_info info);
static napi_value GetCollation(napi_env env, napi_callback_info info);
static napi_value GetHourCycle(napi_env env, napi_callback_info info);
static napi_value GetNumberingSystem(napi_env env, napi_callback_info info);
static napi_value GetNumeric(napi_env env, napi_callback_info info);
static napi_value GetCaseFirst(napi_env env, napi_callback_info info);
static napi_value ToString(napi_env env, napi_callback_info info);
static napi_value Maximize(napi_env env, napi_callback_info info);
static napi_value Minimize(napi_env env, napi_callback_info info);
static napi_value FormatDateTime(napi_env env, napi_callback_info info);
static napi_value FormatDateTimeRange(napi_env env, napi_callback_info info);
static napi_value GetDateTimeResolvedOptions(napi_env env, napi_callback_info info);
static napi_value GetNumberResolvedOptions(napi_env env, napi_callback_info info);
static napi_value FormatNumber(napi_env env, napi_callback_info info);
static int64_t GetYear(napi_env env, napi_value *argv, int index);
static int64_t GetMonth(napi_env env, napi_value *argv, int index);
static int64_t GetDay(napi_env env, napi_value *argv, int index);
static int64_t GetHour(napi_env env, napi_value *argv, int index);
static int64_t GetMinute(napi_env env, napi_value *argv, int index);
static int64_t GetSecond(napi_env env, napi_value *argv, int index);
bool InitLocaleContext(napi_env env, napi_callback_info info, const std::string localeTag,
std::map<std::string, std::string> &map);
bool InitDateTimeFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
std::map<std::string, std::string> &map);
bool InitNumberFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
std::map<std::string, std::string> &map);
napi_env env_;
napi_ref wrapper_;
std::unique_ptr<LocaleInfo> locale_;
std::unique_ptr<DateTimeFormat> datefmt_;
std::unique_ptr<NumberFormat> numberfmt_;
};
} // namespace I18n
} // namespace Global
} // namespace OHOS
/*
* 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.
*/
#ifndef INTL_ADDON_H
#define INTL_ADDON_H
#include <string>
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "locale_info.h"
#include "date_time_format.h"
#include "number_format.h"
#include "collator.h"
#include "plural_rules.h"
namespace OHOS {
namespace Global {
namespace I18n {
static void GetLocaleTags(napi_env env, napi_value rawLocaleTag, std::vector<std::string> &localeTags);
static void GetOptionValue(napi_env env, napi_value options, const std::string &optionName,
std::map<std::string, std::string> &map);
static void GetBoolOptionValue(napi_env env, napi_value options, const std::string &optionName,
std::map<std::string, std::string> &map);
static void GetIntegerOptionValue(napi_env env, napi_value options, const std::string &optionName,
std::map<std::string, std::string> &map);
static void GetDateOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
static void GetNumberOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
static void GetCollatorOptionValue(napi_env env, napi_value options, std::map<std::string, std::string> &map);
static void GetPluralRulesOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
static void SetOptionProperties(napi_env env, napi_value &result, std::map<std::string, std::string> &options,
const std::string &option);
static void SetIntegerOptionProperties(napi_env env, napi_value &result,
std::map<std::string, std::string> &options, const std::string &option);
static void SetBooleanOptionProperties(napi_env env, napi_value &result,
std::map<std::string, std::string> &options, const std::string &option);
class IntlAddon {
public:
static napi_value InitLocale(napi_env env, napi_value exports);
static napi_value InitDateTimeFormat(napi_env env, napi_value exports);
static napi_value InitNumberFormat(napi_env env, napi_value exports);
static napi_value InitCollator(napi_env env, napi_value exports);
static napi_value InitPluralRules(napi_env env, napi_value exports);
static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
IntlAddon();
virtual ~IntlAddon();
private:
static napi_value DateTimeFormatConstructor(napi_env env, napi_callback_info info);
static napi_value NumberFormatConstructor(napi_env env, napi_callback_info info);
static napi_value LocaleConstructor(napi_env env, napi_callback_info info);
static napi_value GetLanguage(napi_env env, napi_callback_info info);
static napi_value GetScript(napi_env env, napi_callback_info info);
static napi_value GetRegion(napi_env env, napi_callback_info info);
static napi_value GetBaseName(napi_env env, napi_callback_info info);
static napi_value GetCalendar(napi_env env, napi_callback_info info);
static napi_value GetCollation(napi_env env, napi_callback_info info);
static napi_value GetHourCycle(napi_env env, napi_callback_info info);
static napi_value GetNumberingSystem(napi_env env, napi_callback_info info);
static napi_value GetNumeric(napi_env env, napi_callback_info info);
static napi_value GetCaseFirst(napi_env env, napi_callback_info info);
static napi_value ToString(napi_env env, napi_callback_info info);
static napi_value Maximize(napi_env env, napi_callback_info info);
static napi_value Minimize(napi_env env, napi_callback_info info);
static napi_value FormatDateTime(napi_env env, napi_callback_info info);
static napi_value FormatDateTimeRange(napi_env env, napi_callback_info info);
static napi_value GetDateTimeResolvedOptions(napi_env env, napi_callback_info info);
static napi_value GetNumberResolvedOptions(napi_env env, napi_callback_info info);
static napi_value FormatNumber(napi_env env, napi_callback_info info);
static napi_value CollatorConstructor(napi_env env, napi_callback_info info);
static napi_value CompareString(napi_env env, napi_callback_info info);
static napi_value GetCollatorResolvedOptions(napi_env env, napi_callback_info info);
static napi_value PluralRulesConstructor(napi_env env, napi_callback_info info);
static napi_value Select(napi_env env, napi_callback_info info);
static int64_t GetYear(napi_env env, napi_value *argv, int index);
static int64_t GetMonth(napi_env env, napi_value *argv, int index);
static int64_t GetDay(napi_env env, napi_value *argv, int index);
static int64_t GetHour(napi_env env, napi_value *argv, int index);
static int64_t GetMinute(napi_env env, napi_value *argv, int index);
static int64_t GetSecond(napi_env env, napi_value *argv, int index);
bool InitLocaleContext(napi_env env, napi_callback_info info, const std::string localeTag,
std::map<std::string, std::string> &map);
bool InitDateTimeFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
std::map<std::string, std::string> &map);
bool InitNumberFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
std::map<std::string, std::string> &map);
bool InitCollatorContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
std::map<std::string, std::string> &map);
bool InitPluralRulesContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
std::map<std::string, std::string> &map);
napi_env env_;
napi_ref wrapper_;
std::unique_ptr<LocaleInfo> locale_;
std::unique_ptr<DateTimeFormat> datefmt_;
std::unique_ptr<NumberFormat> numberfmt_;
std::unique_ptr<Collator> collator_;
std::unique_ptr<PluralRules> pluralrules_;
};
} // namespace I18n
} // namespace Global
} // namespace OHOS
#endif

2900
interfaces/js/kits/src/intl_addon.cpp Executable file → Normal file

File diff suppressed because it is too large Load Diff