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 import java.util.function.BiConsumer;
15
16 public class I18nViewUtil {
17
18
19
20
21 private static final Map<String, Set<Object>> COMPONENTS_LOCALIZED = new HashMap<>();
22
23
24 static {
25 for (String keyI18n: I18nUtil.BUNDLE_ROOT.keySet()) {
26 I18nViewUtil.COMPONENTS_LOCALIZED.put(keyI18n, new HashSet<>());
27 }
28 }
29
30 private I18nViewUtil() {
31
32 }
33
34
35
36
37
38
39 public static Set<String> keys() {
40 return I18nViewUtil.COMPONENTS_LOCALIZED.keySet();
41 }
42
43
44
45
46
47
48
49 public static Set<Object> componentsByKey(String key) {
50 return I18nViewUtil.COMPONENTS_LOCALIZED.get(key);
51 }
52
53 public static void switchI18nComponents() {
54 Map<Class<?>, BiConsumer<Object, String>> classHandlers = new LinkedHashMap<>();
55 classHandlers.put(JPlaceholder.class, (c, s) -> ((JPlaceholder) c).setPlaceholderText(I18nUtil.valueByKey(s)));
56 classHandlers.put(DialogAbout.class, (c, s) -> ((DialogAbout) c).setTitle(I18nUtil.valueByKey(s) + " " + StringUtil.APP_NAME));
57 classHandlers.put(JToolTipI18n.class, (c, s) -> ((JToolTipI18n) c).setText(I18nViewUtil.valueByKey(s)));
58 classHandlers.put(NodeModelEmpty.class, (c, s) -> ((NodeModelEmpty) c).setText(I18nViewUtil.valueByKey(s)));
59 classHandlers.put(JLabel.class, (c, s) -> ((JLabel) c).setText(I18nViewUtil.valueByKey(s)));
60 classHandlers.put(JMenuItem.class, (c, s) -> ((JMenuItem) c).setText(I18nViewUtil.valueByKey(s)));
61 classHandlers.put(JButton.class, (c, s) -> ((JButton) c).setText(I18nViewUtil.valueByKey(s)));
62 classHandlers.put(JComboBox.class, (c, s) -> ((JComboBox<?>) c).setToolTipText(I18nViewUtil.valueByKey(s)));
63 classHandlers.put(JTextComponent.class, (c, s) -> ((JTextComponent) c).setText(I18nViewUtil.valueByKey(s)));
64 for (String key : I18nViewUtil.keys()) {
65 for (Object component : I18nViewUtil.componentsByKey(key)) {
66 classHandlers.entrySet().stream()
67 .filter(entry -> entry.getKey().isInstance(component))
68 .findFirst()
69 .ifPresent(entry -> entry.getValue().accept(component, key));
70 }
71 }
72 }
73
74
75
76
77
78
79
80 public static void addComponentForKey(String key, Object component) {
81 I18nViewUtil.COMPONENTS_LOCALIZED.get(key.replace(" ", "_")).add(component);
82 }
83
84
85
86
87
88
89 public static String valueByKey(String key) {
90 return I18nViewUtil.isNonUbuntu(I18nUtil.getCurrentLocale())
91 ? I18nViewUtil.formatNonLatin(I18nUtil.valueByKey(key))
92 : I18nUtil.valueByKey(key);
93 }
94
95 public static boolean isNonUbuntu(Locale locale) {
96 return Locale.forLanguageTag("zh").getLanguage().equals(locale.getLanguage())
97 || Locale.forLanguageTag("ko").getLanguage().equals(locale.getLanguage())
98 || Locale.forLanguageTag("ja").getLanguage().equals(locale.getLanguage());
99 }
100
101 public static String formatNonLatin(String label) {
102 return I18nViewUtil.formatNonLatin(label, StringUtils.EMPTY);
103 }
104
105 public static String formatNonLatin(String label, String custom) {
106 return String.format(
107 "<html><span style=\"font-family:'%s';%s\">%s</span></html>",
108 UiUtil.FONT_NAME_MONO_ASIAN,
109 custom,
110 label
111 );
112 }
113 }