1/* 2 * Copyright (c) 2023 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 */ 15import i18n from '@ohos.i18n'; 16import IntlUtil from '../../utils/IntlUtil'; 17import TitleBar from '../../components/TitleBar'; 18import ResourceUtil from '../../utils/ResourceUtil'; 19 20const LONGITUDE = 116.20; 21const LATITUDE = 39.56; 22 23@Entry 24@Component 25struct Examples { 26 @State date: string = ''; 27 @State time: string = ''; 28 @State language: string = ''; 29 @State numberFormat: Array<string> = []; 30 @State pluralFormat: string = ''; 31 @State mDirection: string = ''; 32 @State timeZoneDetect: string = ''; 33 @State normalizer: string = ''; 34 35 aboutToAppear() { 36 this.getLanguage(); 37 this.getTimeDisplay(); 38 this.getTimeZoneDetect(); 39 this.getNormalizer(); 40 this.numberFormat = IntlUtil.getNumberFormatString(); 41 this.getPluralFormat(); 42 } 43 44 async getPluralFormat() { 45 this.pluralFormat = await ResourceUtil.getPluralString($r('app.plural.eat_apple').id, 5); 46 this.mDirection = ResourceUtil.getString($r('app.string.screen_direction').id); 47 this.mDirection += await ResourceUtil.getDirection(); 48 } 49 50 getTimeDisplay() { 51 let timeInfo = IntlUtil.getDateString(); 52 this.date = timeInfo[0]; 53 this.time = timeInfo[1]; 54 } 55 56 getTimeZoneDetect() { 57 this.timeZoneDetect = ResourceUtil.getString($r('app.string.coordinate').id); 58 this.timeZoneDetect += '(' + LONGITUDE + ',' + LATITUDE + ') ' + 59 i18n.TimeZone.getTimezonesByLocation(LONGITUDE, LATITUDE)[0].getDisplayName(); 60 } 61 62 getNormalizer() { 63 let nl = i18n.Normalizer.getInstance(i18n.NormalizerMode.NFC); 64 this.normalizer = '\u1100 +\u1161 +\u11A8 = ' + nl.normalize('\u1100\u1161\u11A8'); 65 } 66 67 getLanguage() { 68 let systemLanguage = i18n.System.getSystemLanguage(); 69 this.language = i18n.System.getDisplayLanguage(systemLanguage, systemLanguage); 70 } 71 72 @Builder 73 TextView($$: Data) { 74 Text($$.text) 75 .fontSize(20) 76 .fontColor($r('app.color.gray')) 77 .width('95%') 78 .margin({ top: 20 }) 79 } 80 81 build() { 82 Column() { 83 TitleBar({ hasBackPress: true, title: $r('app.string.format_example') }) 84 this.TextView({ text: this.language }) 85 Column() { 86 this.TextView({ text: this.date }) 87 this.TextView({ text: this.time }) 88 ForEach(this.numberFormat, (item: string, index: number) => { 89 this.TextView({ text: item }); 90 }, (item: string) => item) 91 this.TextView({ text: this.pluralFormat }) 92 this.TextView({ text: this.timeZoneDetect }) 93 this.TextView({ text: this.normalizer }) 94 this.TextView({ text: this.mDirection }) 95 } 96 .width('95%') 97 .padding({ bottom: 20, left: 10, right: 10 }) 98 .backgroundColor($r('app.color.white')) 99 .margin({ top: 5 }) 100 .border({ color: $r('app.color.white'), width: 1, radius: 15 }) 101 } 102 .width('100%') 103 .height('100%') 104 .backgroundColor($r('app.color.f5f5f5')) 105 } 106} 107 108class Data { 109 text: string = ''; 110}