harmony 鸿蒙Setting User Preferences (For System Applications Only)
Setting User Preferences (For System Applications Only)
Use Cases
In addition to system locales and application preferred languages, the system supports setting of user preferences. Currently, the system supports two user preferences: whether to use local digits and whether to use the 12⁄24-hour format. User preference settings are saved to system locales and application preferred languages as part of the internationalization feature.
How to Develop
For details about how to use the APIs, see setUsingLocalDigit and set24HourClock.
Import the intl module.
import { i18n, intl } from '@kit.LocalizationKit'; import { BusinessError } from '@kit.BasicServicesKit';
Obtain the preferred language of an application.
// Obtain the preferred language of an application. let appPreferredLanguage: string = i18n.System.getAppPreferredLanguage();
Enable display of local digits on the application page.
try { i18n.System.setUsingLocalDigit(true); // Enable use of local digits. } catch (error) { let err: BusinessError = error as BusinessError; console.error(`call System.setUsingLocalDigit failed, error code: ${err.code}, message: ${err.message}.`); } let date: Date = new Date(2023, 9, 25); // The date is 2023-10-25. let appPreferredLanguage: string = 'ar'; let dateTimeFmt: intl.DateTimeFormat = new intl.DateTimeFormat(appPreferredLanguage); let formattedTime: string = dateTimeFmt.format(date); // formattedTime = '٢٠٢٣/١٠/٢٥' (represented by localized numbers in Arabic)
Set the 24-hour clock format.
try { i18n.System.set24HourClock (true); // Set the system time to the 24-hour clock. } catch (error) { let err: BusinessError = error as BusinessError; console.error(`call System.set24HourClock failed, error code: ${err.code}, message: ${err.message}.`); } let date: Date = new Date(2023, 9, 25, 16, 48, 0); // The date and time is 2023-10-25 16:48:00. let appPreferredLanguage: string = 'zh'; let dateTimeFmt: intl.DateTimeFormat = new intl.DateTimeFormat(appPreferredLanguage, { timeStyle: 'medium' }); let formattedTime: string = dateTimeFmt.format(date); // formattedTime = '16:48:00'
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Character Processing
harmony 鸿蒙Overview of Internationalization and Localization
harmony 鸿蒙Language and Locale Name Localization
harmony 鸿蒙Locale ID and Cultural Habit Division
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦