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      public static final String BASE_NAME = "i18n.jsql";
21  
22      /**
23       * Bundle of standard i18n keys and translated text for root language English
24       */
25      public static final ResourceBundle BUNDLE_ROOT = ResourceBundle.getBundle(I18nUtil.BASE_NAME, Locale.ROOT);
26      
27      /**
28       * Bundle of i18n keys and translated text for the current system language
29       */
30      private static ResourceBundle currentBundle = ResourceBundle.getBundle(I18nUtil.BASE_NAME, Locale.getDefault());
31  
32      private I18nUtil() {
33          // Utility class
34      }
35      
36      /**
37       * Return the text corresponding to an i18n key in the properties.
38       * @param key an i18n key in the properties
39       * @return text corresponding to the key
40       */
41      public static String valueByKey(String key) {
42          return I18nUtil.currentBundle.getString(key.replace(" ", "_"));  // e.g BLIND BINARY
43      }
44      
45      /**
46       * Verify if there is a language properties file corresponding to the current system language.
47       * If not then it invites the user to use the translation process.
48       */
49      public static void checkCurrentLanguage() {
50          // fall working as uppercase regional matches lowercase bundle (eg. es_AN)
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();
54              LOGGER.log(
55                  LogLevelUtil.CONSOLE_SUCCESS,
56                  () -> "Contribute and translate parts of "+ StringUtil.APP_NAME +" into "+ languageHost +": "
57                  + "click on the top right button and open menu [Community], choose [I help translate jSQL into > another language...] and "
58                  + "translate some text into "+ languageHost +" then click on [Send]. Your translation will be integrated to the next release"
59              );
60          }
61      }
62      
63      
64      // Getters and setters
65      
66      public static void setCurrentBundle(Locale newLocale) {
67          I18nUtil.currentBundle = ResourceBundle.getBundle(I18nUtil.BASE_NAME, newLocale);
68      }
69      
70      public static Locale getCurrentLocale() {
71          return I18nUtil.currentBundle.getLocale();
72      }
73  }