The internationalization module, namely, i18n, provides i18n capabilities such as formatting the date and time, obtaining the month and week names, and formatting numbers.
The directory structure for the i18n module is as follows:
/base/global/
├── i18n_lite # Code repository for the i18n framework
│ ├── frameworks # Core code of the i18n framework
│ │ ├── i18n # i18n module
│ │ │ ├── include # Header files of external APIs
│ │ │ ├── src # Implementation code
│ │ │ └── test # Test cases
│ ├── interfaces # i18n framework APIs
│ │ ├── kits # Application APIs
│ │ │ ├── i18n # C/C++ i18n APIs
│ │ │ └── js # C/C++ support for JavaScript APIs
Programming language: C/C++
Supported languages: See the Appendix.
#include "date_time_format.h"
using namespace OHOS::I18N
LocaleInfo locale("zh", "Hans", "CN"); // Obtain the locale.
DateTimeFormat formatter(AvailableDateTimeFormatPattern::HOUR_MINUTE, locale); // Initialize the example date and time and obtain the data required to format the date and time for the specified locale. The first parameter specifies the formatting pattern. For details about the supported formatting patterns, see the types.h file.
time_t time = 3600 * 3; // Obtain the time to be formatted.
std::string zoneInfo = "+1:00"; // Set the time zone to be UTC+0 plus 1 hour.
std::string out; // Store the formatting result in the out field.
Ii8nStatus status = Ii8nStatus::ISUCCESS;
formatter.Format(time, zoneInfo, out, status); // Check the status field for the formatting result.
output: 4:00
#include "number_format.h"
using namespace OHOS::I18N
LocaleInfo locale("en", "US");
int status = 0;
NumberFormat formatter(locale, status); // Initialize the number formatting instance and obtain the data required to format numbers for the specified locale. The value of status indicates the initialization result. If the value is 1, the initialization has failed.
int num = 1234
std::string out = formatter.Format(num, status); // Check the status field for the initialization result.
output: 1,234
#include "date_time_format.h"
using namespace OHOS::I18N
LocaleInfo locale("en", "US"); // Obtain the locale.
DateTimeFormat formatter(AvailableDateTimeFormatPattern::HOUR_MINUTE, locale);
std::string month = formatter.GetMonthName(0, DateTimeDataType::FORMAT_WIDE); //: Obtain the month name in the wide format.
output: January
Languages vary in how they handle plurals of nouns. For example, there can be "one apple" or "two apples" in English. Singular and plural forms of nouns are generally classified into six plural rule types: zero, one, two, a few, many, and others. Supported plural rule types vary depending on languages. For example, Chinese supports only others, English supports one and others, and Arabic supports all the six plural rule types. The sample code is as follows:
#include "plural_format.h"
using namespace OHOS::I18N
LocaleInfo locale("en", "US"); // Obtain the locale.
Ii8nStatus status = Ii8nStatus::ISUCCESS;
PluralFormatter formatter = PluralFormatter(locale, status); // Check the status field for the i18n status of the locale.
int out = formatter.GetPluralFormatter(1, status); // Check the status field for the plural rule type.
output: 1 // Value 1 indicates plural rule type 1.
global_i18n_lite
Supported languages
Sign in to post a comment
Activity
Community
Health
Trend
Influence
:Code submit frequency
:React/respond to issue & PR etc.
:Well-balanced team members and collaboration
:Recent popularity of project
:Star counts, download counts etc.
Repository Comments ( 0 )