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 router from '@ohos.router'; 17import Logger from '../../utils/Logger'; 18import OperationListView from '../../components/OperationListView'; 19import TitleBar from '../../components/TitleBar'; 20 21const TAG: string = 'Country'; 22 23@Entry 24@Component 25struct Region { 26 private params = router.getParams() as Record<string, string>; 27 private country: string = this.params.currentCountry as string; 28 private localLanguage: string = this.params.language; 29 private suggestIds: Array<string> = []; 30 private countryIds: Array<string> = []; 31 @State countries: Array<Countrie> = []; 32 @State suggestCounties: Array<SuggestCountie> = []; 33 34 aboutToAppear() { 35 Logger.info(TAG, `this.country = ${this.country}`); 36 try { 37 this.countryIds = i18n.System.getSystemCountries(this.localLanguage); 38 } catch (err) { 39 Logger.error(`getSystemCountries failed, code is ${err.code}, message is ${err.message}`); 40 } 41 Logger.info(TAG, `systemCountryIds = ${JSON.stringify(this.countryIds)}`); 42 this.countryIds.forEach(id => { 43 let country = i18n.System.getDisplayCountry(id, this.localLanguage); 44 this.countries.push({ key: country, value: '' }); 45 let isSuggested: boolean = false; 46 try { 47 isSuggested = i18n.System.isSuggested(this.localLanguage, id); 48 } catch (err) { 49 Logger.error(`isSuggested failed, code is ${err.code}, message is ${err.message}`); 50 } 51 if (isSuggested) { 52 this.suggestIds.push(id); 53 this.suggestCounties.push({ key: country, value: '' }); 54 } 55 }) 56 } 57 58 setSuggestRegion = (index: number) => { 59 let region = this.suggestIds[index]; 60 try { 61 i18n.System.setSystemRegion(region); 62 } catch (err) { 63 Logger.error(`setSystemRegion failed, code is ${err.code}, message is ${err.message}`); 64 } 65 router.back(); 66 } 67 setRegion = (index: number) => { 68 let region = this.countryIds[index]; 69 try { 70 i18n.System.setSystemRegion(region); 71 } catch (err) { 72 Logger.error(`setSystemRegion failed, code is ${err.code}, message is ${err.message}`); 73 } 74 router.back(); 75 } 76 77 @Builder 78 CurrentCountryView() { 79 Row() { 80 Text(this.country) 81 .fontSize(22) 82 Blank() 83 Image($r('app.media.check')) 84 .width(25).height(50) 85 .objectFit(ImageFit.Contain) 86 } 87 .size({ width: '95%' }) 88 .padding(10) 89 .backgroundColor($r('app.color.white')) 90 .margin({ top: 10 }) 91 .border({ color: $r('app.color.white'), width: 1, radius: 15 }) 92 } 93 94 build() { 95 Column() { 96 TitleBar({ hasBackPress: true, title: $r('app.string.select_region') }) 97 Scroll() { 98 Column() { 99 Text($r('app.string.current_region')) 100 .fontSize(20).fontColor($r('app.color.gray')) 101 .width('95%') 102 .margin({ left: 10, top: 15 }) 103 this.CurrentCountryView() 104 OperationListView({ 105 title: $r('app.string.suggest_region'), 106 operations: this.suggestCounties, 107 handleItemClick: this.setSuggestRegion 108 }) 109 OperationListView({ 110 title: $r('app.string.all_region'), 111 operations: this.countries, 112 handleItemClick: this.setRegion 113 }) 114 } 115 } 116 .layoutWeight(1) 117 } 118 .width('100%') 119 .height('100%') 120 .backgroundColor($r('app.color.f5f5f5')) 121 } 122} 123 124class Countrie { 125 key: string = ''; 126 value: string = ''; 127} 128 129class SuggestCountie { 130 key: string = ''; 131 value: string = ''; 132}