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
19
20 private static final Map<String, Set<Object>> COMPONENTS_LOCALIZED = new HashMap<>();
21
22
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
31 }
32
33
34
35
36
37
38 public static Set<String> keys() {
39 return I18nViewUtil.COMPONENTS_LOCALIZED.keySet();
40 }
41
42
43
44
45
46
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
67 }
68 }
69 }
70 }
71 }
72
73
74
75
76
77
78
79 public static void addComponentForKey(String key, Object component) {
80 I18nViewUtil.COMPONENTS_LOCALIZED.get(key.replace(" ", "_")).add(component);
81 }
82
83
84
85
86
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 }