• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = await 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  async getTimeZoneDetect() {
57    this.timeZoneDetect = await 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 TextView($$: Data) {
73    Text($$.text)
74      .fontSize(20)
75      .fontColor($r('app.color.gray'))
76      .width('95%')
77      .margin({ top: 20 })
78  }
79
80  build() {
81    Column() {
82      TitleBar({ hasBackPress: true, title: $r('app.string.format_example') })
83      this.TextView(new Data(this.language))
84      Column() {
85        this.TextView(new Data(this.date))
86        this.TextView(new Data(this.time))
87        ForEach(this.numberFormat, (item: string, index: number) => {
88          this.TextView(new Data(item));
89        }, (item: string) => item)
90        this.TextView(new Data(this.pluralFormat))
91        this.TextView(new Data(this.timeZoneDetect))
92        this.TextView(new Data(this.normalizer))
93        this.TextView(new Data(this.mDirection))
94      }
95      .width('95%')
96      .padding({ bottom: 20, left: 10, right: 10 })
97      .backgroundColor($r('app.color.white'))
98      .margin({ top: 5 })
99      .border({ color: $r('app.color.white'), width: 1, radius: 15 })
100    }
101    .width('100%')
102    .height('100%')
103    .backgroundColor($r('app.color.f5f5f5'))
104  }
105}
106
107class Data {
108  text: string = '';
109
110  constructor(text: string) {
111    this.text = text;
112  }
113}