1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.script; |
21 |
|
|
22 |
|
import java.util.Collection; |
23 |
|
import java.util.Locale; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Provider; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.component.manager.ComponentLookupException; |
32 |
|
import org.xwiki.component.manager.ComponentManager; |
33 |
|
import org.xwiki.localization.LocaleUtils; |
34 |
|
import org.xwiki.localization.LocalizationContext; |
35 |
|
import org.xwiki.localization.LocalizationManager; |
36 |
|
import org.xwiki.localization.Translation; |
37 |
|
import org.xwiki.rendering.block.Block; |
38 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
39 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
40 |
|
import org.xwiki.rendering.syntax.Syntax; |
41 |
|
import org.xwiki.script.service.ScriptService; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@version |
47 |
|
@since |
48 |
|
|
49 |
|
@Component |
50 |
|
@Named("localization") |
51 |
|
@Singleton |
|
|
| 65.2% |
Uncovered Elements: 16 (46) |
Complexity: 18 |
Complexity Density: 0.62 |
|
52 |
|
public class LocalizationScriptService implements ScriptService |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Inject |
58 |
|
private LocalizationManager localization; |
59 |
|
|
60 |
|
|
61 |
|
@link |
62 |
|
|
63 |
|
@Inject |
64 |
|
private LocalizationContext localizationContext; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@Inject |
70 |
|
@Named("context") |
71 |
|
private Provider<ComponentManager> componentManager; |
72 |
|
|
73 |
|
|
74 |
|
@param |
75 |
|
@return |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
4280 |
public Translation get(String key)... |
78 |
|
{ |
79 |
4280 |
return get(key, this.localizationContext.getCurrentLocale()); |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
@param |
84 |
|
@param@link |
85 |
|
@link |
86 |
|
|
87 |
|
@return |
88 |
|
@since |
89 |
|
@since |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
4280 |
public Translation get(String key, Locale locale)... |
92 |
|
{ |
93 |
4280 |
return this.localization.getTranslation(key, locale); |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
@param@link |
98 |
|
|
99 |
|
@param |
100 |
|
@return |
101 |
|
|
102 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
103 |
0 |
public boolean use(String bundleType, String bundleId)... |
104 |
|
{ |
105 |
0 |
try { |
106 |
0 |
this.localization.use(bundleType, bundleId); |
107 |
|
} catch (Exception e) { |
108 |
|
|
109 |
0 |
return false; |
110 |
|
} |
111 |
|
|
112 |
0 |
return true; |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
@return@link |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
13 |
public Locale getCurrentLocale()... |
119 |
|
{ |
120 |
13 |
return this.localizationContext.getCurrentLocale(); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
@param |
128 |
|
@return@link |
129 |
|
|
130 |
|
@since |
131 |
|
@see |
132 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
133 |
0 |
public Locale toLocale(String str)... |
134 |
|
{ |
135 |
0 |
try { |
136 |
0 |
return LocaleUtils.toLocale(str); |
137 |
|
} catch (IllegalArgumentException e) { |
138 |
0 |
return null; |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@param |
146 |
|
@return |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
148 |
68433 |
public String render(String key)... |
149 |
|
{ |
150 |
68433 |
return render(key, (Collection<?>) null); |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
@param |
155 |
|
@param@link |
156 |
|
@link |
157 |
|
|
158 |
|
@return |
159 |
|
@since |
160 |
|
@since |
161 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
162 |
0 |
public String render(String key, Locale locale)... |
163 |
|
{ |
164 |
0 |
return render(key, (Collection<?>) null, locale); |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
@param |
169 |
|
@param |
170 |
|
@return |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
72232 |
public String render(String key, Collection<?> parameters)... |
173 |
|
{ |
174 |
72236 |
return render(key, Syntax.PLAIN_1_0, parameters); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
@param |
179 |
|
@param |
180 |
|
@param@link |
181 |
|
@link |
182 |
|
|
183 |
|
@return |
184 |
|
@since |
185 |
|
@since |
186 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
187 |
0 |
public String render(String key, Collection<?> parameters, Locale locale)... |
188 |
|
{ |
189 |
0 |
return render(key, Syntax.PLAIN_1_0, parameters); |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
@param |
194 |
|
@param |
195 |
|
@return |
196 |
|
@since |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
198 |
1 |
public String render(String key, Syntax syntax)... |
199 |
|
{ |
200 |
1 |
return render(key, syntax, (Collection<?>) null); |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
@param |
205 |
|
@param |
206 |
|
@param@link |
207 |
|
@link |
208 |
|
|
209 |
|
@return |
210 |
|
@since |
211 |
|
@since |
212 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
213 |
0 |
public String render(String key, Syntax syntax, Locale locale)... |
214 |
|
{ |
215 |
0 |
return render(key, syntax, (Collection<?>) null, locale); |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
@param |
220 |
|
@param |
221 |
|
@param |
222 |
|
@return |
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
224 |
72240 |
public String render(String key, Syntax syntax, Collection<?> parameters)... |
225 |
|
{ |
226 |
72238 |
return render(key, syntax, parameters, this.localizationContext.getCurrentLocale()); |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
@param |
231 |
|
@param |
232 |
|
@param |
233 |
|
@param@link |
234 |
|
@link |
235 |
|
|
236 |
|
@return |
237 |
|
@since |
238 |
|
@since |
239 |
|
|
|
|
| 93.8% |
Uncovered Elements: 1 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
240 |
72238 |
public String render(String key, Syntax syntax, Collection<?> parameters, Locale locale)... |
241 |
|
{ |
242 |
72238 |
Translation translation = this.localization.getTranslation(key, locale); |
243 |
|
|
244 |
72241 |
String result; |
245 |
|
|
246 |
72242 |
if (translation != null) { |
247 |
71736 |
Block block = |
248 |
71736 |
parameters != null ? translation.render(locale, parameters.toArray()) : translation.render(locale); |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
71737 |
try { |
253 |
71737 |
BlockRenderer renderer = |
254 |
|
this.componentManager.get().getInstance(BlockRenderer.class, syntax.toIdString()); |
255 |
|
|
256 |
71739 |
DefaultWikiPrinter wikiPrinter = new DefaultWikiPrinter(); |
257 |
71739 |
renderer.render(block, wikiPrinter); |
258 |
|
|
259 |
71739 |
result = wikiPrinter.toString(); |
260 |
|
} catch (ComponentLookupException e) { |
261 |
|
|
262 |
0 |
result = null; |
263 |
|
} |
264 |
|
} else { |
265 |
502 |
result = key; |
266 |
|
} |
267 |
|
|
268 |
72238 |
return result; |
269 |
|
} |
270 |
|
} |