1 /*
2  * Copyright (c) 2021-2022 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 "base/i18n/localization.h"
17 
18 namespace OHOS::Ace {
19 namespace {
20 constexpr int32_t DAY_COUNTS_OF_WEEK = 7;
21 constexpr int32_t MONTH_COUNTS_OF_YEAR = 12;
22 } // namespace
23 
24 struct LocaleProxy final {};
25 Localization::~Localization() = default;
26 
SetLocaleImpl(const std::string & language,const std::string & countryOrRegion,const std::string & script,const std::string & selectLanguage,const std::string & keywordsAndValues)27 void Localization::SetLocaleImpl(const std::string& language, const std::string& countryOrRegion,
28     const std::string& script, const std::string& selectLanguage, const std::string& keywordsAndValues)
29 {
30     languageTag_ = language;
31 }
32 
GetFontLocale()33 std::string Localization::GetFontLocale()
34 {
35     return fontLocale_;
36 }
37 
GetInstance()38 std::shared_ptr<Localization> Localization::GetInstance()
39 {
40     static auto instance = std::make_shared<Localization>();
41     return instance;
42 }
43 
GetDateOrder(std::vector<std::string> & outOrder)44 bool Localization::GetDateOrder(std::vector<std::string>& outOrder)
45 {
46     outOrder.clear();
47     if (languageTag_ == "ug") {
48         outOrder.emplace_back("month");
49         outOrder.emplace_back("day");
50         outOrder.emplace_back("year");
51     } else if (languageTag_ == "zh") {
52         outOrder.emplace_back("year");
53         outOrder.emplace_back("month");
54         outOrder.emplace_back("day");
55     } else if (languageTag_ == "false") {
56         return false;
57     }
58     return true;
59 }
60 
FormatDuration(uint32_t duration,const std::string & format)61 std::string Localization::FormatDuration(uint32_t duration, const std::string& format)
62 {
63     return "08:00:00";
64 }
65 
FormatDateTime(DateTime dateTime,const std::string & format)66 const std::string Localization::FormatDateTime(DateTime dateTime, const std::string& format)
67 {
68     return "08:00:00";
69 }
70 
FormatDateTime(DateTime dateTime,DateTimeStyle dateStyle,DateTimeStyle timeStyle)71 const std::string Localization::FormatDateTime(DateTime dateTime, DateTimeStyle dateStyle, DateTimeStyle timeStyle)
72 {
73     return "08:00:00";
74 }
75 
GetEntryLetters(const std::string & lettersIndex)76 std::string Localization::GetEntryLetters(const std::string& lettersIndex)
77 {
78     return "";
79 }
80 
GetErrorDescription(const std::string & errorIndex)81 std::string Localization::GetErrorDescription(const std::string& errorIndex)
82 {
83     return "";
84 }
85 
FormatDuration(uint32_t duration,bool needShowHour)86 const std::string Localization::FormatDuration(uint32_t duration, bool needShowHour)
87 {
88     return "08:00:00";
89 }
90 
GetMonths(bool isShortType,const std::string & calendarType)91 std::vector<std::string> Localization::GetMonths(bool isShortType, const std::string& calendarType)
92 {
93     std::vector<std::string> months;
94     for (int32_t i = 0; i < MONTH_COUNTS_OF_YEAR; i++) {
95         months.push_back(std::to_string(i));
96     }
97     return months;
98 }
99 
GetLanguage()100 std::string Localization::GetLanguage()
101 {
102     return "Chinese";
103 }
104 
GetLunarMonth(uint32_t month,bool isLeapMonth)105 std::string Localization::GetLunarMonth(uint32_t month, bool isLeapMonth)
106 {
107     return "";
108 }
109 
GetLunarDay(uint32_t dayOfMonth)110 std::string Localization::GetLunarDay(uint32_t dayOfMonth)
111 {
112     return "";
113 }
114 
GetLunarDate(Date date)115 LunarDate Localization::GetLunarDate(Date date)
116 {
117     LunarDate dateRet;
118     dateRet.year = date.year;
119     dateRet.month = date.month;
120     dateRet.day = date.day;
121     return dateRet;
122 }
123 
GetAmPmStrings()124 std::vector<std::string> Localization::GetAmPmStrings()
125 {
126     std::vector<std::string> amPms;
127     return amPms;
128 }
129 
GetHourFormat(bool & isAmPm,bool & hasZero)130 bool Localization::GetHourFormat(bool& isAmPm, bool& hasZero)
131 {
132     return false;
133 }
134 
135 // Mock get weekdays, 7 days in a week.
GetWeekdays(bool isShortType)136 std::vector<std::string> Localization::GetWeekdays(bool isShortType)
137 {
138     std::vector<std::string> weekdays;
139     for (int32_t i = 0; i < DAY_COUNTS_OF_WEEK; i++) {
140         weekdays.push_back(std::to_string(i));
141     }
142     return weekdays;
143 }
144 
NumberFormat(double number)145 std::string Localization::NumberFormat(double number)
146 {
147     return std::to_string(number);
148 }
149 } // namespace OHOS::Ace
150