1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.localization.script

File LocalizationScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

4
29
13
1
270
110
18
0.62
2.23
13
1.38

Classes

Class Line # Actions
LocalizationScriptService 52 29 0% 18 16
0.6521739465.2%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Provides Component-specific Scripting APIs.
45    *
46    * @version $Id: 4a538604196a03cd8a944fe91f7ddb78fe29af70 $
47    * @since 4.3M2
48    */
49    @Component
50    @Named("localization")
51    @Singleton
 
52    public class LocalizationScriptService implements ScriptService
53    {
54    /**
55    * Used to access translations.
56    */
57    @Inject
58    private LocalizationManager localization;
59   
60    /**
61    * Used to access current {@link java.util.Locale}.
62    */
63    @Inject
64    private LocalizationContext localizationContext;
65   
66    /**
67    * Used to lookup renderers.
68    */
69    @Inject
70    @Named("context")
71    private Provider<ComponentManager> componentManager;
72   
73    /**
74    * @param key the translation key
75    * @return the translation, null if none can be found
76    */
 
77  4280 toggle public Translation get(String key)
78    {
79  4280 return get(key, this.localizationContext.getCurrentLocale());
80    }
81   
82    /**
83    * @param key the translation key
84    * @param locale the {@link Locale} for which this translation is searched. The result might me associated to a
85    * different {@link Locale} (for example getting the {@code fr} translation when asking for the
86    * {@code fr_FR} one).
87    * @return the translation, null if none can be found
88    * @since 9.0RC1
89    * @since 8.4.2
90    */
 
91  4280 toggle public Translation get(String key, Locale locale)
92    {
93  4280 return this.localization.getTranslation(key, locale);
94    }
95   
96    /**
97    * @param bundleType the hint of the {@link org.xwiki.localization.TranslationBundleFactory} to use to get the
98    * actual bundle
99    * @param bundleId the identifier of the bundle for the passed type
100    * @return true if the bundle has been found and properly added to the list of current translation bundles, false
101    * otherwise
102    */
 
103  0 toggle public boolean use(String bundleType, String bundleId)
104    {
105  0 try {
106  0 this.localization.use(bundleType, bundleId);
107    } catch (Exception e) {
108    // TODO set current error
109  0 return false;
110    }
111   
112  0 return true;
113    }
114   
115    /**
116    * @return the {@link Locale} to use by default in the current context
117    */
 
118  13 toggle public Locale getCurrentLocale()
119    {
120  13 return this.localizationContext.getCurrentLocale();
121    }
122   
123    /**
124    * Converts the given string to a locale. E.g. the string "pt_BR" is converted to a locale with the language set to
125    * Portuguese and the country set to Brazil.
126    *
127    * @param str the String to convert to Locale
128    * @return the corresponding locale, or {@link Locale#ROOT} if the given string is {@code null} or empty; if the
129    * given string doesn't represent a locale (e.g. invalid format) then {@code null} is returned
130    * @since 5.3M2
131    * @see org.apache.commons.lang3.LocaleUtils#toLocale(String)
132    */
 
133  0 toggle 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    // Helpers
143   
144    /**
145    * @param key the translation key
146    * @return the rendered translation message
147    */
 
148  68433 toggle public String render(String key)
149    {
150  68433 return render(key, (Collection<?>) null);
151    }
152   
153    /**
154    * @param key the translation key
155    * @param locale the {@link Locale} for which this translation is searched. The result might me associated to a
156    * different {@link Locale} (for example getting the {@code fr} translation when asking for the
157    * {@code fr_FR} one).
158    * @return the rendered translation message
159    * @since 9.0RC1
160    * @since 8.4.2
161    */
 
162  0 toggle public String render(String key, Locale locale)
163    {
164  0 return render(key, (Collection<?>) null, locale);
165    }
166   
167    /**
168    * @param key the translation key
169    * @param parameters the translation parameters
170    * @return the rendered translation message
171    */
 
172  72232 toggle public String render(String key, Collection<?> parameters)
173    {
174  72236 return render(key, Syntax.PLAIN_1_0, parameters);
175    }
176   
177    /**
178    * @param key the translation key
179    * @param parameters the translation parameters
180    * @param locale the {@link Locale} for which this translation is searched. The result might me associated to a
181    * different {@link Locale} (for example getting the {@code fr} translation when asking for the
182    * {@code fr_FR} one).
183    * @return the rendered translation message
184    * @since 9.0RC1
185    * @since 8.4.2
186    */
 
187  0 toggle 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 key the translation key
194    * @param syntax the syntax in which to render the translation message
195    * @return the rendered translation message, the key if no translation can be found and null if the rendering failed
196    * @since 5.1M2
197    */
 
198  1 toggle public String render(String key, Syntax syntax)
199    {
200  1 return render(key, syntax, (Collection<?>) null);
201    }
202   
203    /**
204    * @param key the translation key
205    * @param syntax the syntax in which to render the translation message
206    * @param locale the {@link Locale} for which this translation is searched. The result might me associated to a
207    * different {@link Locale} (for example getting the {@code fr} translation when asking for the
208    * {@code fr_FR} one).
209    * @return the rendered translation message, the key if no translation can be found and null if the rendering failed
210    * @since 9.0RC1
211    * @since 8.4.2
212    */
 
213  0 toggle public String render(String key, Syntax syntax, Locale locale)
214    {
215  0 return render(key, syntax, (Collection<?>) null, locale);
216    }
217   
218    /**
219    * @param key the translation key
220    * @param syntax the syntax in which to render the translation message
221    * @param parameters the translation parameters
222    * @return the rendered translation message, the key if no translation can be found and null if the rendering failed
223    */
 
224  72240 toggle 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 key the translation key
231    * @param syntax the syntax in which to render the translation message
232    * @param parameters the translation parameters
233    * @param locale the {@link Locale} for which this translation is searched. The result might me associated to a
234    * different {@link Locale} (for example getting the {@code fr} translation when asking for the
235    * {@code fr_FR} one).
236    * @return the rendered translation message, the key if no translation can be found and null if the rendering failed
237    * @since 9.0RC1
238    * @since 8.4.2
239    */
 
240  72238 toggle 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    // Render the block
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    // TODO set current error
262  0 result = null;
263    }
264    } else {
265  502 result = key;
266    }
267   
268  72238 return result;
269    }
270    }