View Javadoc
1   package com.jsql.util;
2   
3   import org.apache.logging.log4j.LogManager;
4   import org.apache.logging.log4j.Logger;
5   
6   import java.net.URL;
7   import java.util.Locale;
8   import java.util.ResourceBundle;
9   
10  /**
11   * Utility class managing different text translations like English, Chinese and Arabic.
12   * It retrieves text in the current language of the system and also the one choice
13   * manually by user.
14   * If the current system language is not supported then the user is proposed to use
15   * the community translation protocol.
16   */
17  public class I18nUtil {
18      
19      private static final Logger LOGGER = LogManager.getRootLogger();
20      
21      /**
22       * Bundle of standard i18n keys and translated text for root language English
23       */
24      private static final ResourceBundle LOCALE_ROOT = ResourceBundle.getBundle("i18n.jsql", Locale.ROOT);
25      
26      /**
27       * Bundle of i18n keys and translated text for the current system language
28       */
29      private static ResourceBundle localeDefault = ResourceBundle.getBundle("i18n.jsql", Locale.getDefault());
30  
31      private I18nUtil() {
32          // Utility class
33      }
34      
35      /**
36       * Return the text corresponding to a i18n key in the properties.
37       * @param key a i18n key in the properties
38       * @return text corresponding to the key
39       */
40      public static String valueByKey(String key) {
41          return (String) I18nUtil.localeDefault.getObject(key);
42      }
43      
44      /**
45       * Verify if there is a language properties file corresponding to the current system language.
46       * If not then it invites the user to use the translation process.
47       */
48      public static void checkCurrentLanguage() {
49          
50          URL path = I18nUtil.class.getClassLoader().getResource("i18n/jsql_"+ Locale.getDefault().getLanguage() +".properties");
51          
52          if (!"en".equals(Locale.getDefault().getLanguage()) && path == null) {
53              
54              String languageHost = Locale.getDefault().getDisplayLanguage(Locale.ENGLISH);
55              
56              LOGGER.log(
57                  LogLevelUtil.CONSOLE_SUCCESS,
58                  () -> String.join(
59                      "",
60                      "Please contribute and translate parts of jSQL Injection into ",
61                      languageHost,
62                      ": ",
63                      "click on the top right button and open menu [Community], choose [I help translate jSQL into > another language...] and ",
64                      "translate some text into ",
65                      languageHost,
66                      " then click on [Send]. Your translation will be integrated to the next release by the developer."
67                  )
68              );
69          }
70      }
71      
72      public static boolean isAsian(Locale newLocale) {
73          return Locale.forLanguageTag("zh").getLanguage().equals(newLocale.getLanguage())
74              || Locale.forLanguageTag("ko").getLanguage().equals(newLocale.getLanguage())
75              || Locale.forLanguageTag("ja").getLanguage().equals(newLocale.getLanguage());
76      }
77      
78      
79      // Getters and setters
80      
81      public static void setLocaleDefault(ResourceBundle localeDefault) {
82          I18nUtil.localeDefault = localeDefault;
83      }
84      
85      public static Locale getLocaleDefault() {
86          return I18nUtil.localeDefault.getLocale();
87      }
88  
89      public static ResourceBundle getLocaleRoot() {
90          return LOCALE_ROOT;
91      }
92  }