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