1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package com.sun.javafx.runtime.util; |
27 | |
|
28 | |
import java.util.Collections; |
29 | |
import java.util.Map; |
30 | |
import java.util.Locale; |
31 | |
import java.util.WeakHashMap; |
32 | |
import java.util.concurrent.ConcurrentHashMap; |
33 | |
import java.util.regex.Pattern; |
34 | |
import com.sun.javafx.runtime.util.backport.ResourceBundle; |
35 | |
|
36 | 0 | public class StringLocalization { |
37 | |
|
38 | 8 | private static final Map<ThreadGroup, Map<String, String>> map = |
39 | |
Collections.synchronizedMap( |
40 | |
new WeakHashMap<ThreadGroup, Map<String, String>>()); |
41 | |
|
42 | |
public static String getLocalizedString(String scriptName, String explicitKey, |
43 | |
String literal, Object... embeddedExpr) { |
44 | 34 | String key = scriptName.replaceAll("/", "\\."); |
45 | 34 | int lastDot = key.lastIndexOf('.'); |
46 | 34 | if (lastDot != -1) { |
47 | 8 | key = key.substring(0, lastDot) + "/" + key.substring(lastDot + 1); |
48 | |
} else { |
49 | 26 | key = "/" + key; |
50 | |
} |
51 | 34 | return getLocalizedString( |
52 | |
getPropertiesName(key), explicitKey, literal, Locale.getDefault(), embeddedExpr); |
53 | |
} |
54 | |
|
55 | |
public static String getLocalizedString(String propertiesName, String explicitKey, |
56 | |
String literal, Locale locale, Object... embeddedExpr) { |
57 | 53 | String localization = literal; |
58 | |
|
59 | |
try { |
60 | 53 | ResourceBundle rb = ResourceBundle.getBundle(propertiesName, |
61 | |
locale, getCallerLoader(), FXPropertyResourceBundle.FXPropertiesControl.INSTANCE); |
62 | 53 | if (explicitKey != null) { |
63 | 23 | localization = rb.getString(explicitKey); |
64 | 23 | if (explicitKey.equals(localization) && |
65 | |
!rb.keySet().contains(explicitKey)) { |
66 | 7 | localization = literal; |
67 | |
} |
68 | |
} else { |
69 | 30 | localization = rb.getString(literal.replaceAll("\r\n|\r|\n", "\n")); |
70 | |
} |
71 | |
|
72 | 53 | if (embeddedExpr.length != 0) { |
73 | 3 | localization = FXFormatter.sprintf(locale, localization, embeddedExpr); |
74 | |
} |
75 | 0 | } catch (Exception e) { |
76 | 0 | e.printStackTrace(); |
77 | 53 | } |
78 | |
|
79 | 53 | return localization; |
80 | |
} |
81 | |
|
82 | |
public static void associate(String source, String properties) { |
83 | 5 | getAssociation().put(source, properties); |
84 | 5 | } |
85 | |
|
86 | |
public static void dissociate(String source) { |
87 | 3 | Map<String, String> assoc = getAssociation(); |
88 | |
|
89 | |
|
90 | 3 | assoc.remove(source); |
91 | |
|
92 | |
|
93 | 3 | if (source.indexOf('/') == -1) { |
94 | 1 | String toRemove = source + "/"; |
95 | 1 | for (String key : assoc.keySet()) { |
96 | 1 | if (key.startsWith(toRemove)) { |
97 | 1 | assoc.remove(key); |
98 | |
} |
99 | |
} |
100 | |
} |
101 | 3 | } |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public static String getPropertiesName(String key) { |
110 | 48 | String propertiesName = key.replaceAll("^/", "").replaceAll("/", "."); |
111 | 48 | Map<String, String> assoc = getAssociation(); |
112 | 48 | Pattern chopoff = Pattern.compile("[\\./][^\\./]*\\z"); |
113 | |
|
114 | |
while (true) { |
115 | 100 | if (assoc.containsKey(key)) { |
116 | 6 | propertiesName = assoc.get(key); |
117 | 6 | break; |
118 | |
} else { |
119 | 94 | if ("".equals(key)) { |
120 | 42 | break; |
121 | 52 | } else if (chopoff.matcher(key).find()) { |
122 | 48 | key = chopoff.matcher(key).replaceAll(""); |
123 | |
} else { |
124 | 4 | key = ""; |
125 | |
} |
126 | |
} |
127 | |
} |
128 | |
|
129 | 48 | return propertiesName; |
130 | |
} |
131 | |
|
132 | |
private static Map<String, String> getAssociation() { |
133 | 56 | ThreadGroup tg = Thread.currentThread().getThreadGroup(); |
134 | 56 | Map<String, String> assoc = map.get(tg); |
135 | |
|
136 | 56 | if (assoc == null) { |
137 | 8 | assoc = new ConcurrentHashMap<String, String>(); |
138 | 8 | map.put(tg, assoc); |
139 | |
} |
140 | |
|
141 | 56 | return assoc; |
142 | |
} |
143 | |
|
144 | 8 | private static final ClassContext CC = new ClassContext(); |
145 | 8 | private static final String PKGNAME = StringLocalization.class.getPackage().getName(); |
146 | |
private static ClassLoader getCallerLoader() { |
147 | 53 | Class[] callers = CC.getClassContext(); |
148 | |
try { |
149 | 246 | for (Class c : callers) { |
150 | 246 | if (!c.getName().startsWith(PKGNAME)) { |
151 | 53 | return c.getClassLoader(); |
152 | |
} |
153 | |
} |
154 | 0 | } catch (SecurityException se) { |
155 | 0 | } |
156 | 0 | return StringLocalization.class.getClassLoader(); |
157 | |
} |
158 | |
} |