1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.localization.internal

File DefaultLocalizationManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

6
21
3
1
127
77
10
0.48
7
3
3.33

Classes

Class Line # Actions
DefaultLocalizationManager 50 21 0% 10 3
0.990%
 

Contributing tests

This file is covered by 32 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.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    * Default implementation of the {@link LocalizationManager} component.
44    *
45    * @version $Id: 908ffda3eab8ff086e96eb7a94d3a5d35121b3dd $
46    * @since 4.3M2
47    */
48    @Component
49    @Singleton
 
50    public class DefaultLocalizationManager implements LocalizationManager
51    {
52    /**
53    * Provides access to different bundles based on their hint. Needed in {@link #use(String, String)}.
54    */
55    @Inject
56    @Named("context")
57    private Provider<ComponentManager> componentManagerProvider;
58   
59    /**
60    * Used to access the current bundles.
61    */
62    @Inject
63    private TranslationBundleContext bundleContext;
64   
65    /**
66    * The logger to log.
67    */
68    @Inject
69    private Logger logger;
70   
 
71  2389949 toggle @Override
72    public Translation getTranslation(String key, Locale locale)
73    {
74  2389950 for (TranslationBundle bundle : this.bundleContext.getBundles()) {
75  100113051 try {
76  100113064 Translation translation = bundle.getTranslation(key, locale);
77  100112945 if (translation != null && translation.getLocale().equals(locale)) {
78  1260976 return translation;
79    }
80    } catch (Exception e) {
81  0 this.logger.error("Failed to get translation", e);
82    }
83    }
84   
85    // Try parent locale
86  1128968 Locale parentLocale = LocaleUtils.getParentLocale(locale);
87  1128972 if (parentLocale != null) {
88  1084147 return getTranslation(key, parentLocale);
89    }
90   
91  44817 return null;
92    }
93   
 
94  40 toggle @Override
95    public TranslationBundle getTranslationBundle(String bundleType, String bundleId)
96    throws TranslationBundleDoesNotExistsException, TranslationBundleFactoryDoesNotExistsException
97   
98    {
99  40 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  39 TranslationBundleFactory bundleFactory;
109  39 try {
110  39 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  39 return bundleFactory.getBundle(bundleId);
117    }
118   
 
119  39 toggle @Override
120    public void use(String bundleType, String bundleId) throws TranslationBundleDoesNotExistsException,
121    TranslationBundleFactoryDoesNotExistsException
122    {
123  39 TranslationBundle bundle = getTranslationBundle(bundleType, bundleId);
124   
125  39 this.bundleContext.addBundle(bundle);
126    }
127    }