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