1 /* 2 * Copyright (c) 2021-2024 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 */ 15 16 package ohos.global.i18n; 17 18 import java.util.HashMap; 19 import java.io.BufferedReader; 20 import java.io.IOException; 21 import java.io.InputStreamReader; 22 import java.io.File; 23 import java.io.FileInputStream; 24 import java.net.URISyntaxException; 25 import java.nio.charset.StandardCharsets; 26 import java.util.logging.Logger; 27 import java.util.logging.Level; 28 29 /** 30 * This class is used to extract plural data related to a locale 31 * 32 * @since 2022-8-22 33 */ 34 public class PluralFetcher { 35 private static PluralFetcher plural = new PluralFetcher(); 36 private static final Logger logger = Logger.getLogger("PluralFetcher"); 37 38 static { plural.init()39 plural.init(); 40 } 41 42 private HashMap<String, String> map; 43 private HashMap<String, String> decimalMap; 44 PluralFetcher()45 private PluralFetcher() {} 46 47 /** 48 * Return the singleton instance 49 * 50 * @return plural 51 */ getInstance()52 public static PluralFetcher getInstance() { 53 return plural; 54 } 55 init()56 private void init() { 57 try (BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream( 58 new File(MeasureFormatPatternFetcher.class.getResource("/resource/plural.txt").toURI())), 59 StandardCharsets.UTF_8))) { 60 map = new HashMap<>(); 61 String line = ""; 62 while ((line = fin.readLine()) != null) { 63 String[] temp = getPluralItems(line); 64 map.put(temp[0], temp[1]); 65 } 66 } catch (IOException | URISyntaxException e) { 67 logger.log(Level.SEVERE, "Init error"); 68 } 69 try (BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream( 70 new File(MeasureFormatPatternFetcher.class.getResource("/resource/decimalPlurals.txt").toURI())), 71 StandardCharsets.UTF_8))) { 72 decimalMap = new HashMap<>(); 73 String line = ""; 74 while ((line = fin.readLine()) != null) { 75 String[] temp = getPluralItems(line); 76 decimalMap.put(temp[0], temp[1]); 77 } 78 } catch (IOException | URISyntaxException e) { 79 logger.log(Level.SEVERE, "Init error"); 80 } 81 } 82 83 /** 84 * Get plural data related to lan 85 * 86 * @param lan Indicates which language's data to be retrieved 87 * @return Language related to this PluralFetcher instance 88 */ get(String lan)89 public String get(String lan) { 90 String out = map.get(lan); 91 if (out == null) { 92 out = ""; 93 } 94 return out; 95 } 96 97 /** 98 * Get plural data related to lan 99 * 100 * @param lan Indicates which language's data to be retrieved 101 * @return Language related to this PluralFetcher instance 102 */ getDecimal(String lan)103 public String getDecimal(String lan) { 104 String out = decimalMap.get(lan); 105 if (out == null) { 106 out = ""; 107 } 108 return out; 109 } 110 getPluralItems(String line)111 private static String[] getPluralItems(String line) { 112 String[] ret = new String[2]; 113 String trimedLine = line.trim(); 114 String[] splits = trimedLine.split(" ", 2); // Split into 2 parts 115 if (splits.length != 2) { 116 logger.log(Level.SEVERE, "Init error"); 117 return new String[0]; 118 } 119 String languageTag = splits[0]; 120 if (!languageTag.contains("-")) { 121 ret[0] = languageTag; 122 } else { 123 String[] tags = languageTag.split("-"); 124 ret[0] = tags[0]; 125 } 126 String[] resources = splits[1].split(", "); 127 StringBuilder sb = new StringBuilder(); 128 for (int i = 0; i < resources.length; ++i) { 129 if (resources[i].length() > 2) { // 2 means skip "" 130 int length = resources[i].length(); 131 sb.append(resources[i].substring(1, length - 1)); 132 } 133 if (i != resources.length) { 134 sb.append(FileConfig.SEP); 135 } 136 } 137 ret[1] = sb.toString(); 138 return ret; 139 } 140 } 141