| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>LocalizationTool.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> > <a href="index.source.html" class="el_package">org.apache.turbine.services.localization</a> > <span class="el_source">LocalizationTool.java</span></div><h1>LocalizationTool.java</h1><pre class="source lang-java linenums">package org.apache.turbine.services.localization; |
| |
| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| import java.util.Locale; |
| import java.util.MissingResourceException; |
| |
| import org.apache.logging.log4j.Logger; |
| import org.apache.logging.log4j.LogManager; |
| import org.apache.fulcrum.localization.LocalizationService; |
| import org.apache.turbine.annotation.TurbineService; |
| import org.apache.turbine.services.pull.ApplicationTool; |
| import org.apache.turbine.util.RunData; |
| /** |
| * A pull tool which provides lookups for localized text by delegating |
| * to the configured Fulcrum <code>LocalizationService</code>. |
| * |
| * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> |
| * @author <a href="mailto:dlr@collab.net">Daniel Rall</a> |
| * @author <a href="mailto:jon@collab.net">Jon Stevens</a> |
| */ |
| public class LocalizationTool implements ApplicationTool |
| { |
| /** Logging */ |
| <span class="fc" id="L42"> private static Logger log = LogManager.getLogger(LocalizationTool.class);</span> |
| |
| /** Fulcrum Localization component */ |
| @TurbineService |
| private LocalizationService localizationService; |
| |
| /** |
| * The language and country information parsed from the request's |
| * <code>Accept-Language</code> header. Reset on each request. |
| */ |
| protected Locale locale; |
| |
| /** |
| * Creates a new instance. Used by <code>PullService</code>. |
| */ |
| public LocalizationTool() |
| <span class="fc" id="L58"> {</span> |
| <span class="fc" id="L59"> refresh();</span> |
| <span class="fc" id="L60"> }</span> |
| |
| /** |
| * <p>Performs text lookups for localization.</p> |
| * |
| * <p>Assuming there is a instance of this class with a HTTP |
| * request set in your template's context named <code>l10n</code>, |
| * the VTL <code>$l10n.HELLO</code> would render to |
| * <code>hello</code> for English requests and <code>hola</code> |
| * in Spanish (depending on the value of the HTTP request's |
| * <code>Accept-Language</code> header).</p> |
| * |
| * @param key The identifier for the localized text to retrieve. |
| * @return The localized text. |
| */ |
| public String get(String key) |
| { |
| try |
| { |
| <span class="nc" id="L79"> return localizationService.getString(getBundleName(null), getLocale(), key);</span> |
| } |
| <span class="nc" id="L81"> catch (MissingResourceException noKey)</span> |
| { |
| <span class="nc" id="L83"> log.error(noKey);</span> |
| <span class="nc" id="L84"> return null;</span> |
| } |
| } |
| |
| /** |
| * Gets the current locale. |
| * |
| * @return The locale currently in use. |
| */ |
| public Locale getLocale() |
| { |
| <span class="nc" id="L95"> return locale;</span> |
| } |
| |
| /** |
| * The return value of this method is used to set the name of the |
| * bundle used by this tool. Useful as a hook for using a |
| * different bundle than specified in your |
| * <code>LocalizationService</code> configuration. |
| * |
| * @param data The inputs passed from {@link #init(Object)}. |
| * (ignored by this implementation). |
| * |
| * @return the name of the bundle to use |
| */ |
| protected String getBundleName(Object data) |
| { |
| <span class="nc" id="L111"> return localizationService.getDefaultBundleName();</span> |
| } |
| |
| /** |
| * Formats a localized value using the provided objects. |
| * |
| * @param key The identifier for the localized text to retrieve, |
| * @param args The objects to use as {0}, {1}, etc. when |
| * formatting the localized text. |
| * @return Formatted localized text. |
| */ |
| public String format(String key, Object... args) |
| { |
| <span class="nc" id="L124"> return localizationService.format(getBundleName(null), getLocale(), key, args);</span> |
| } |
| |
| // ApplicationTool implementation |
| |
| /** |
| * Sets the request to get the <code>Accept-Language</code> header |
| * from (reset on each request). |
| */ |
| @Override |
| public void init(Object data) |
| { |
| <span class="nc bnc" id="L136" title="All 2 branches missed."> if (data instanceof RunData)</span> |
| { |
| // Pull necessary information out of RunData while we have |
| // a reference to it. |
| <span class="nc bnc" id="L140" title="All 2 branches missed."> locale = (localizationService instanceof RundataLocalizationService)?</span> |
| <span class="nc" id="L141"> ((RundataLocalizationService)localizationService).getLocale((RunData) data):</span> |
| <span class="nc" id="L142"> localizationService.getLocale(((RunData) data).getRequest());</span> |
| } |
| <span class="nc" id="L144"> }</span> |
| |
| /** |
| * No-op. |
| */ |
| @Override |
| public void refresh() |
| { |
| <span class="fc" id="L152"> locale = null;</span> |
| <span class="fc" id="L153"> }</span> |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html> |