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  
13  
14  
15  
16  
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  
25  
26      public static final ResourceBundle BUNDLE_ROOT = ResourceBundle.getBundle(I18nUtil.BASE_NAME, Locale.ROOT);
27      
28      
29  
30  
31      private static ResourceBundle currentBundle = ResourceBundle.getBundle(I18nUtil.BASE_NAME, Locale.getDefault());
32  
33      private I18nUtil() {
34          
35      }
36      
37      
38  
39  
40  
41  
42      public static String valueByKey(String key) {
43          return I18nUtil.currentBundle.getString(key.replace(" ", "_"));  
44      }
45      
46      
47  
48  
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      
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  }