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