blob: e75d58ccc695cd7d1b4e6da2191522288d4711fe [file] [log] [blame]
<?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> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.services.localization</a> &gt; <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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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 &lt;code&gt;LocalizationService&lt;/code&gt;.
*
* @author &lt;a href=&quot;mailto:epugh@upstate.com&quot;&gt;Eric Pugh&lt;/a&gt;
* @author &lt;a href=&quot;mailto:dlr@collab.net&quot;&gt;Daniel Rall&lt;/a&gt;
* @author &lt;a href=&quot;mailto:jon@collab.net&quot;&gt;Jon Stevens&lt;/a&gt;
*/
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
* &lt;code&gt;Accept-Language&lt;/code&gt; header. Reset on each request.
*/
protected Locale locale;
/**
* Creates a new instance. Used by &lt;code&gt;PullService&lt;/code&gt;.
*/
public LocalizationTool()
<span class="fc" id="L58"> {</span>
<span class="fc" id="L59"> refresh();</span>
<span class="fc" id="L60"> }</span>
/**
* &lt;p&gt;Performs text lookups for localization.&lt;/p&gt;
*
* &lt;p&gt;Assuming there is a instance of this class with a HTTP
* request set in your template's context named &lt;code&gt;l10n&lt;/code&gt;,
* the VTL &lt;code&gt;$l10n.HELLO&lt;/code&gt; would render to
* &lt;code&gt;hello&lt;/code&gt; for English requests and &lt;code&gt;hola&lt;/code&gt;
* in Spanish (depending on the value of the HTTP request's
* &lt;code&gt;Accept-Language&lt;/code&gt; header).&lt;/p&gt;
*
* @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
* &lt;code&gt;LocalizationService&lt;/code&gt; 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 &lt;code&gt;Accept-Language&lt;/code&gt; 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>