Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LocalizationTool |
|
| 1.4285714285714286;1,429 |
1 | package org.apache.turbine.services.localization; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.util.Locale; | |
23 | import java.util.MissingResourceException; | |
24 | ||
25 | import org.apache.logging.log4j.Logger; | |
26 | import org.apache.logging.log4j.LogManager; | |
27 | import org.apache.fulcrum.localization.LocalizationService; | |
28 | import org.apache.turbine.annotation.TurbineService; | |
29 | import org.apache.turbine.services.pull.ApplicationTool; | |
30 | import org.apache.turbine.util.RunData; | |
31 | /** | |
32 | * A pull tool which provides lookups for localized text by delegating | |
33 | * to the configured Fulcrum <code>LocalizationService</code>. | |
34 | * | |
35 | * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> | |
36 | * @author <a href="mailto:dlr@collab.net">Daniel Rall</a> | |
37 | * @author <a href="mailto:jon@collab.net">Jon Stevens</a> | |
38 | */ | |
39 | public class LocalizationTool implements ApplicationTool | |
40 | { | |
41 | /** Logging */ | |
42 | 57 | private static Logger log = LogManager.getLogger(LocalizationTool.class); |
43 | ||
44 | /** Fulcrum Localization component */ | |
45 | @TurbineService | |
46 | private LocalizationService localizationService; | |
47 | ||
48 | /** | |
49 | * The language and country information parsed from the request's | |
50 | * <code>Accept-Language</code> header. Reset on each request. | |
51 | */ | |
52 | protected Locale locale; | |
53 | ||
54 | /** | |
55 | * Creates a new instance. Used by <code>PullService</code>. | |
56 | */ | |
57 | public LocalizationTool() | |
58 | 36 | { |
59 | 36 | refresh(); |
60 | 36 | } |
61 | ||
62 | /** | |
63 | * <p>Performs text lookups for localization.</p> | |
64 | * | |
65 | * <p>Assuming there is a instance of this class with a HTTP | |
66 | * request set in your template's context named <code>l10n</code>, | |
67 | * the VTL <code>$l10n.HELLO</code> would render to | |
68 | * <code>hello</code> for English requests and <code>hola</code> | |
69 | * in Spanish (depending on the value of the HTTP request's | |
70 | * <code>Accept-Language</code> header).</p> | |
71 | * | |
72 | * @param key The identifier for the localized text to retrieve. | |
73 | * @return The localized text. | |
74 | */ | |
75 | public String get(String key) | |
76 | { | |
77 | try | |
78 | { | |
79 | 6 | return localizationService.getString(getBundleName(null), getLocale(), key); |
80 | } | |
81 | 0 | catch (MissingResourceException noKey) |
82 | { | |
83 | 0 | log.error(noKey); |
84 | 0 | return null; |
85 | } | |
86 | } | |
87 | ||
88 | /** | |
89 | * Gets the current locale. | |
90 | * | |
91 | * @return The locale currently in use. | |
92 | */ | |
93 | public Locale getLocale() | |
94 | { | |
95 | 24 | return locale; |
96 | } | |
97 | ||
98 | /** | |
99 | * The return value of this method is used to set the name of the | |
100 | * bundle used by this tool. Useful as a hook for using a | |
101 | * different bundle than specified in your | |
102 | * <code>LocalizationService</code> configuration. | |
103 | * | |
104 | * @param data The inputs passed from {@link #init(Object)}. | |
105 | * (ignored by this implementation). | |
106 | * | |
107 | * @return the name of the bundle to use | |
108 | */ | |
109 | protected String getBundleName(Object data) | |
110 | { | |
111 | 6 | return localizationService.getDefaultBundleName(); |
112 | } | |
113 | ||
114 | /** | |
115 | * Formats a localized value using the provided objects. | |
116 | * | |
117 | * @param key The identifier for the localized text to retrieve, | |
118 | * @param args The objects to use as {0}, {1}, etc. when | |
119 | * formatting the localized text. | |
120 | * @return Formatted localized text. | |
121 | */ | |
122 | public String format(String key, Object... args) | |
123 | { | |
124 | 0 | return localizationService.format(getBundleName(null), getLocale(), key, args); |
125 | } | |
126 | ||
127 | // ApplicationTool implementation | |
128 | ||
129 | /** | |
130 | * Sets the request to get the <code>Accept-Language</code> header | |
131 | * from (reset on each request). | |
132 | */ | |
133 | @Override | |
134 | public void init(Object data) | |
135 | { | |
136 | 39 | if (data instanceof RunData) |
137 | { | |
138 | // Pull necessary information out of RunData while we have | |
139 | // a reference to it. | |
140 | 39 | locale = localizationService.getLocale(((RunData) data).getRequest()); |
141 | } | |
142 | 39 | } |
143 | ||
144 | /** | |
145 | * No-op. | |
146 | */ | |
147 | @Override | |
148 | public void refresh() | |
149 | { | |
150 | 39 | locale = null; |
151 | 39 | } |
152 | } |