1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "plural_format.h"
17 #include "plural_format_impl.h"
18
19 using namespace OHOS::I18N;
20
PluralFormat(LocaleInfo & locale,I18nStatus & status)21 PluralFormat::PluralFormat(LocaleInfo &locale, I18nStatus &status)
22 {
23 if (locale.GetId() == nullptr) {
24 status = IERROR;
25 return;
26 }
27 mLocale = locale;
28 }
29
~PluralFormat()30 PluralFormat::~PluralFormat()
31 {
32 if (impl != nullptr) {
33 delete impl;
34 impl = nullptr;
35 }
36 }
37
Init()38 bool PluralFormat::Init()
39 {
40 if (impl != nullptr) {
41 delete impl;
42 }
43 I18nStatus status = I18nStatus::ISUCCESS;
44 impl = new PluralFormatImpl(mLocale, status);
45 if (impl == nullptr) {
46 return false;
47 }
48 if (status != I18nStatus::ISUCCESS) {
49 return false;
50 }
51 DataResource resource(&mLocale);
52 bool isSuccess = resource.Init();
53 if (!isSuccess) {
54 return false;
55 }
56 return impl->Init(resource);
57 }
58
GetPluralRuleIndex(int number,I18nStatus status)59 int PluralFormat::GetPluralRuleIndex(int number, I18nStatus status)
60 {
61 if (impl == nullptr) {
62 bool isSuccess = Init();
63 if (!isSuccess) {
64 return -1;
65 }
66 }
67 return impl->GetPluralRuleIndex(number, status);
68 }
69
GetPluralRuleIndex(double number,I18nStatus status)70 int PluralFormat::GetPluralRuleIndex(double number, I18nStatus status)
71 {
72 if (impl == nullptr) {
73 bool isSuccess = Init();
74 if (!isSuccess) {
75 return -1;
76 }
77 }
78 return impl->GetPluralRuleIndex(number, status);
79 }