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
12
13
14
15
16
17 public class I18nUtil {
18
19 private static final Logger LOGGER = LogManager.getRootLogger();
20
21
22
23
24 private static final ResourceBundle LOCALE_ROOT = ResourceBundle.getBundle("i18n.jsql", Locale.ROOT);
25
26
27
28
29 private static ResourceBundle localeDefault = ResourceBundle.getBundle("i18n.jsql", Locale.getDefault());
30
31 private I18nUtil() {
32
33 }
34
35
36
37
38
39
40 public static String valueByKey(String key) {
41 return (String) I18nUtil.localeDefault.getObject(key);
42 }
43
44
45
46
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
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 }