1 /* 2 * Copyright (c) 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 #ifndef MEASUREFORMAT_H 17 #define MEASUREFORMAT_H 18 /** 19 * @addtogroup I18N 20 * @{ 21 * 22 * @brief Provides functions related to internationalization (i18n), with which you can format measure. 23 * 24 * @since 2.2 25 * @version 1.0 26 */ 27 28 /** 29 * @file measure_format.h 30 * 31 * @brief Declares functions for formatting measures. 32 * 33 * Example code: \n 34 * Creating a <b>LocaleInfo</b> instance: \n 35 * {@code LocaleInfo locale("en", "Latn", "");} 36 * Creating a <b>MeasureFormat</b> instance: \n 37 * {@code 38 * int status = 0; 39 * MeasureFormat formatter(locale, status); 40 * Formatting data: \n 41 * {@code 42 * std::string out = formatter.Format(12, "kcal", MeasureType::MEDIUM);} 43 * Output: \n 44 * 12 kcal 45 * 46 * @since 2.2 47 * @version 1.0 48 */ 49 50 #include "locale_info.h" 51 #include "measure_format_impl.h" 52 #include "string.h" 53 #include "types.h" 54 55 namespace OHOS { 56 namespace I18N { 57 class MeasureFormat { 58 public: 59 /** 60 * @brief A constructor used to create a <b>MeasureFormat</b> instance with specified locale information. 61 * 62 * @param localeinfo Indicates the specified locale information. 63 * @param status Specifies whether a <b>MeasureFormat</b> instance is created. 64 * The value <b>0</b> indicates that a <b>NumberFormat</b> instance is created, 65 * and the value <b>1</b> indicates the opposite case. 66 * @since 2.2 67 * @version 1.0 68 */ 69 MeasureFormat(LocaleInfo &localeinfo, I18nStatus &status); 70 71 /** 72 * @brief A destructor used to delete the <b>MeasureFormat</b> instance. 73 * 74 * @since 2.2 75 * @version 1.0 76 */ 77 virtual ~MeasureFormat(); 78 79 /** 80 * @brief Formats a measure. 81 * 82 * @param num Indicates the int number to format. 83 * @param unit Indicates the measure unit to format. 84 * @param status Specifies whether the formatting is successful. 85 * The value <b>0</b> indicates that the formatting is successful, 86 * and <b>1</b> indicates that the formatting fails. 87 * @param type Indicates the type the int number is formatted into. 88 * The value can be <b>SHORT</b>, <b>MEDIUM</b>, <b>LONG</b>, <b>FULL</b>. 89 * @return Returns a string representation of the formatted measure. 90 * @since 2.2 91 * @version 1.0 92 */ 93 std::string Format(int num, std::string unit, I18nStatus &status, 94 MeasureFormatType type = MeasureFormatType::MEASURE_SHORT); 95 96 /** 97 * @brief Formats a number with measure. 98 * 99 * @param num Indicates the double number to format. 100 * @param unit Indicates the measure unit to format. 101 * @param status Specifies whether the formatting is successful. 102 * The value <b>0</b> indicates that the formatting is successful, 103 * and <b>1</b> indicates that the formatting fails. 104 * @param type Indicates the type the double number is formatted into. 105 * The value can be <b>SHORT</b>, <b>MEDIUM</b>, <b>LONG</b>, <b>FULL</b>. 106 * @return Returns a string representation of the formatted measure. 107 * @since 2.2 108 * @version 1.0 109 */ 110 std::string Format(double num, std::string unit, I18nStatus &status, 111 MeasureFormatType type = MeasureFormatType::MEASURE_SHORT); 112 113 /** 114 * @brief Performs an initialization to load data. 115 * 116 * @return Returns <b>true</b> if the initialization is successful; returns <b>false</b> otherwise. 117 * @since 2.2 118 * @version 1.0 119 */ 120 bool Init(); 121 122 private: 123 bool ReInitImpl(); 124 MeasureFormatImpl *measureFormatImpl = nullptr; 125 LocaleInfo locale; 126 }; 127 } // namespace I18N 128 } // namespace OHOS 129 /** @} */ 130 #endif