View Javadoc
1   package com.jsql.view.swing.util;
2   
3   import com.jsql.util.I18nUtil;
4   import com.jsql.util.StringUtil;
5   import com.jsql.view.swing.dialog.DialogAbout;
6   import com.jsql.view.swing.text.JPlaceholder;
7   import com.jsql.view.swing.text.JToolTipI18n;
8   import com.jsql.view.swing.tree.model.NodeModelEmpty;
9   import org.apache.commons.lang3.StringUtils;
10  
11  import javax.swing.*;
12  import javax.swing.text.JTextComponent;
13  import java.util.*;
14  
15  public class I18nViewUtil {
16  
17      /**
18       * A list of graphical components for each i18n keys in the main properties
19       */
20      private static final Map<String, Set<Object>> COMPONENTS_LOCALIZED = new HashMap<>();
21      
22      // Initialize the list of graphical components
23      static {
24          for (String keyI18n: I18nUtil.BUNDLE_ROOT.keySet()) {
25              I18nViewUtil.COMPONENTS_LOCALIZED.put(keyI18n, new HashSet<>());
26          }
27      }
28  
29      private I18nViewUtil() {
30          // Utility class
31      }
32      
33      /**
34       * Return the i18n keys of components whose text is replaced
35       * when the translation changes.
36       * @return a set of key names of an i18n key in the properties
37       */
38      public static Set<String> keys() {
39          return I18nViewUtil.COMPONENTS_LOCALIZED.keySet();
40      }
41      
42      /**
43       * Get a list of graphical components whose text corresponds
44       * to the i18n key in the properties.
45       * @param key name of an i18n key in the properties
46       * @return set of graphical components
47       */
48      public static Set<Object> componentsByKey(String key) {
49          return I18nViewUtil.COMPONENTS_LOCALIZED.get(key);
50      }
51  
52      public static void switchI18nComponents() {
53          for (String key : I18nViewUtil.keys()) {
54              for (Object component : I18nViewUtil.componentsByKey(key)) {
55                  switch (component) {
56                      case JPlaceholder c -> c.setPlaceholderText(I18nUtil.valueByKey(key));
57                      case DialogAbout c -> c.setTitle(I18nUtil.valueByKey(key) + " " + StringUtil.APP_NAME);
58                      case JToolTipI18n c -> c.setText(I18nViewUtil.valueByKey(key));
59                      case NodeModelEmpty c -> c.setText(I18nViewUtil.valueByKey(key));
60                      case JLabel c -> c.setText(I18nViewUtil.valueByKey(key));
61                      case JMenuItem c -> c.setText(I18nViewUtil.valueByKey(key));
62                      case JButton c -> c.setText(I18nViewUtil.valueByKey(key));
63                      case JComboBox<?> c -> c.setToolTipText(I18nViewUtil.valueByKey(key));
64                      case JTextComponent c -> c.setText(I18nViewUtil.valueByKey(key));
65                      default -> {
66                          // ignore
67                      }
68                  }
69              }
70          }
71      }
72      
73      /**
74       * Add a graphical component to those whose text must be changed when
75       * the language changes.
76       * @param key name of an i18n key in the properties
77       * @param component graphical component which will receive the translated text
78       */
79      public static void addComponentForKey(String key, Object component) {
80          I18nViewUtil.COMPONENTS_LOCALIZED.get(key.replace(" ", "_")).add(component);  // e.g BIND BINARY
81      }
82  
83      /**
84       * Return the text corresponding to an i18n key in the properties.
85       * @param key an i18n key in the properties
86       * @return text corresponding to the key
87       */
88      public static String valueByKey(String key) {
89          return I18nViewUtil.isNonUbuntu(I18nUtil.getCurrentLocale())
90          ? I18nViewUtil.formatNonLatin(I18nUtil.valueByKey(key))
91          : I18nUtil.valueByKey(key);
92      }
93  
94      public static boolean isNonUbuntu(Locale locale) {
95          return Locale.forLanguageTag("zh").getLanguage().equals(locale.getLanguage())
96          || Locale.forLanguageTag("ko").getLanguage().equals(locale.getLanguage())
97          || Locale.forLanguageTag("ja").getLanguage().equals(locale.getLanguage());
98      }
99  
100     public static String formatNonLatin(String label) {
101         return I18nViewUtil.formatNonLatin(label, StringUtils.EMPTY);
102     }
103 
104     public static String formatNonLatin(String label, String custom) {
105         return String.format(
106             "<html><span style=\"font-family:'%s';%s\">%s</span></html>",
107             UiUtil.FONT_NAME_MONO_ASIAN,
108             custom,
109             label
110         );
111     }
112 }