1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.internal; |
21 |
|
|
22 |
|
import java.util.Locale; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Provider; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.slf4j.Logger; |
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.LocalizationManager; |
35 |
|
import org.xwiki.localization.Translation; |
36 |
|
import org.xwiki.localization.TranslationBundle; |
37 |
|
import org.xwiki.localization.TranslationBundleContext; |
38 |
|
import org.xwiki.localization.TranslationBundleDoesNotExistsException; |
39 |
|
import org.xwiki.localization.TranslationBundleFactory; |
40 |
|
import org.xwiki.localization.TranslationBundleFactoryDoesNotExistsException; |
41 |
|
|
42 |
|
|
43 |
|
@link |
44 |
|
|
45 |
|
@version |
46 |
|
@since |
47 |
|
|
48 |
|
@Component |
49 |
|
@Singleton |
|
|
| 90% |
Uncovered Elements: 3 (30) |
Complexity: 10 |
Complexity Density: 0.48 |
|
50 |
|
public class DefaultLocalizationManager implements LocalizationManager |
51 |
|
{ |
52 |
|
|
53 |
|
@link |
54 |
|
|
55 |
|
@Inject |
56 |
|
@Named("context") |
57 |
|
private Provider<ComponentManager> componentManagerProvider; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
private TranslationBundleContext bundleContext; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
private Logger logger; |
70 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 5 |
Complexity Density: 0.5 |
|
71 |
166004 |
@Override... |
72 |
|
public Translation getTranslation(String key, Locale locale) |
73 |
|
{ |
74 |
166004 |
for (TranslationBundle bundle : this.bundleContext.getBundles()) { |
75 |
1086995 |
try { |
76 |
1086997 |
Translation translation = bundle.getTranslation(key, locale); |
77 |
1086994 |
if (translation != null && translation.getLocale().equals(locale)) { |
78 |
79785 |
return translation; |
79 |
|
} |
80 |
|
} catch (Exception e) { |
81 |
0 |
this.logger.error("Failed to get translation", e); |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
86218 |
Locale parentLocale = LocaleUtils.getParentLocale(locale); |
87 |
86219 |
if (parentLocale != null) { |
88 |
80955 |
return getTranslation(key, parentLocale); |
89 |
|
} |
90 |
|
|
91 |
5262 |
return null; |
92 |
|
} |
93 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 4 |
Complexity Density: 0.44 |
|
94 |
2 |
@Override... |
95 |
|
public TranslationBundle getTranslationBundle(String bundleType, String bundleId) |
96 |
|
throws TranslationBundleDoesNotExistsException, TranslationBundleFactoryDoesNotExistsException |
97 |
|
|
98 |
|
{ |
99 |
2 |
if (this.componentManagerProvider.get().hasComponent(TranslationBundle.class, bundleType + ':' + bundleId)) { |
100 |
1 |
try { |
101 |
1 |
return this.componentManagerProvider.get().<TranslationBundle> getInstance(TranslationBundle.class, |
102 |
|
bundleType + ':' + bundleId); |
103 |
|
} catch (ComponentLookupException e) { |
104 |
0 |
this.logger.error("Failed to lookup component", e); |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
1 |
TranslationBundleFactory bundleFactory; |
109 |
1 |
try { |
110 |
1 |
bundleFactory = this.componentManagerProvider.get().getInstance(TranslationBundleFactory.class, bundleType); |
111 |
|
} catch (ComponentLookupException e) { |
112 |
0 |
throw new TranslationBundleFactoryDoesNotExistsException(String.format( |
113 |
|
"Failed to lookup BundleFactory for type [%s]", bundleType), e); |
114 |
|
} |
115 |
|
|
116 |
1 |
return bundleFactory.getBundle(bundleId); |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
119 |
1 |
@Override... |
120 |
|
public void use(String bundleType, String bundleId) throws TranslationBundleDoesNotExistsException, |
121 |
|
TranslationBundleFactoryDoesNotExistsException |
122 |
|
{ |
123 |
1 |
TranslationBundle bundle = getTranslationBundle(bundleType, bundleId); |
124 |
|
|
125 |
1 |
this.bundleContext.addBundle(bundle); |
126 |
|
} |
127 |
|
} |