-
Notifications
You must be signed in to change notification settings - Fork 2
Custom Localization
By default, tiex uses the localization mechanism provided by the standard library, such as formatting %A
to a locale-dependent text according to the global std::locale
object. But sometimes you may want a more flexible way to specify your own locale text, like using “周一” instead of “星期一” in Chinese locale.
In such case, you can pass an additional tiex::Locale
argument while calling Format
method. For example:
//Create a formatter that output weekday for any formatted time.
tiex::Formatter formatter = tiex::Formatter::Create("[*, *]{%A}");
//Referenced time and formatted time are trivial in this example,
//just specify the current time.
std::time_t referenced_time = std::time(nullptr);
std::time_t formatted_time = std::time(nullptr);
//Define a locale that returns custom weekday text.
tiex::Locale locale;
locale.get_weekday = [](int weekday, const tiex::WeekdayOptions& options) {
switch (weekday) {
case 0: return "周日";
case 1: return "周一";
case 2: return "周二";
case 3: return "周三";
case 4: return "周四";
case 5: return "周五";
case 6: return "周六";
default: return "";
}
};
//Use the locale to format.
//The value of text is from "周日" to "周六".
auto text = formatter.Format(referenced_time, formatted_time, locale);
As you can see, the output of %A
is overrode by the return value of a function object specified in tiex::Locale::get_weekday
field. Similarly, %a
would be overrode by this field as well, except that the argument options
has difference value. If this field is null, no specifier would be overrode.
More standard specifiers can be overrode in the future. Please refer to the definition of tiex::Locale
for a latest list.