1 package com.jsql.util;
2
3 import com.jsql.model.exception.JSqlRuntimeException;
4 import org.apache.commons.lang3.StringUtils;
5 import org.apache.logging.log4j.LogManager;
6 import org.apache.logging.log4j.Logger;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.StringReader;
11 import java.math.BigDecimal;
12 import java.math.RoundingMode;
13 import java.nio.charset.StandardCharsets;
14 import java.util.Arrays;
15 import java.util.Locale;
16 import java.util.Properties;
17 import java.util.concurrent.atomic.AtomicInteger;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 public class PropertiesUtil {
22
23 private static final Logger LOGGER = LogManager.getRootLogger();
24
25 private final Properties properties = new Properties();
26
27 public PropertiesUtil() {
28 var filename = "config.properties";
29 try (InputStream input = PropertiesUtil.class.getClassLoader().getResourceAsStream(filename)) {
30 if (input == null) {
31 LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Properties file {} not found", filename);
32 return;
33 }
34 this.properties.load(input);
35 } catch (IOException e) {
36 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
37 }
38 }
39
40 public void displayI18nStatus(Locale newLocale) {
41 if (!Arrays.asList(StringUtils.EMPTY, "en").contains(newLocale.getLanguage())) {
42 AtomicInteger countGui = new AtomicInteger();
43 var bundleRoot = PropertiesUtil.getProperties("/i18n/jsql.properties");
44 var bundleUser = PropertiesUtil.getProperties("/i18n/jsql_" + newLocale.getLanguage() + ".properties");
45 bundleRoot.entrySet().stream().filter(
46 key -> bundleUser.isEmpty() || !bundleUser.containsKey(key.getKey())
47 ).forEach(
48 key -> countGui.getAndIncrement()
49 );
50 if (countGui.get() > 0) {
51 LOGGER.log(
52 LogLevelUtil.CONSOLE_SUCCESS,
53 "Switched to {} with {}% translated, contribute and translate any of remaining {} items in menu Community",
54 () -> newLocale.getDisplayLanguage(newLocale),
55 () -> BigDecimal.valueOf(
56 100.0 - countGui.get() * 100.0 / bundleRoot.size()
57 ).setScale(1, RoundingMode.HALF_UP).doubleValue(),
58 countGui::get
59 );
60 }
61 }
62 }
63
64 private static Properties getProperties(String name) {
65 var properties = new Properties();
66 try (InputStream in = PropertiesUtil.class.getResourceAsStream(name)) {
67 if (in != null) {
68 byte[] root = in.readAllBytes();
69 var rootI18n = new String(root, StandardCharsets.UTF_8);
70 String rootI18nFixed = Pattern.compile("\\\\[\n\r]+")
71 .matcher(Matcher.quoteReplacement(rootI18n))
72 .replaceAll("a");
73 properties.load(new StringReader(rootI18nFixed));
74 } else {
75 throw new JSqlRuntimeException("Resource not found: "+ name);
76 }
77 } catch (IOException e) {
78 throw new JSqlRuntimeException(e);
79 }
80 return properties;
81 }
82
83 public String getVersionJsql() {
84 return this.properties.getProperty("jsql.version");
85 }
86
87 public String getProperty(String property) {
88 return this.properties.getProperty(property);
89 }
90 }