001package org.apache.turbine.services.localization; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.Locale; 023import java.util.MissingResourceException; 024 025import org.apache.logging.log4j.Logger; 026import org.apache.logging.log4j.LogManager; 027import org.apache.fulcrum.localization.LocalizationService; 028import org.apache.turbine.annotation.TurbineService; 029import org.apache.turbine.services.pull.ApplicationTool; 030import org.apache.turbine.util.RunData; 031/** 032 * A pull tool which provides lookups for localized text by delegating 033 * to the configured Fulcrum <code>LocalizationService</code>. 034 * 035 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> 036 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a> 037 * @author <a href="mailto:jon@collab.net">Jon Stevens</a> 038 */ 039public class LocalizationTool implements ApplicationTool 040{ 041 /** Logging */ 042 private static Logger log = LogManager.getLogger(LocalizationTool.class); 043 044 /** Fulcrum Localization component */ 045 @TurbineService 046 private LocalizationService localizationService; 047 048 /** 049 * The language and country information parsed from the request's 050 * <code>Accept-Language</code> header. Reset on each request. 051 */ 052 protected Locale locale; 053 054 /** 055 * Creates a new instance. Used by <code>PullService</code>. 056 */ 057 public LocalizationTool() 058 { 059 refresh(); 060 } 061 062 /** 063 * <p>Performs text lookups for localization.</p> 064 * 065 * <p>Assuming there is a instance of this class with a HTTP 066 * request set in your template's context named <code>l10n</code>, 067 * the VTL <code>$l10n.HELLO</code> would render to 068 * <code>hello</code> for English requests and <code>hola</code> 069 * in Spanish (depending on the value of the HTTP request's 070 * <code>Accept-Language</code> header).</p> 071 * 072 * @param key The identifier for the localized text to retrieve. 073 * @return The localized text. 074 */ 075 public String get(String key) 076 { 077 try 078 { 079 return localizationService.getString(getBundleName(null), getLocale(), key); 080 } 081 catch (MissingResourceException noKey) 082 { 083 log.error(noKey); 084 return null; 085 } 086 } 087 088 /** 089 * Gets the current locale. 090 * 091 * @return The locale currently in use. 092 */ 093 public Locale getLocale() 094 { 095 return locale; 096 } 097 098 /** 099 * 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 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 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 if (data instanceof RunData) 137 { 138 // Pull necessary information out of RunData while we have 139 // a reference to it. 140 locale = localizationService.getLocale(((RunData) data).getRequest()); 141 } 142 } 143 144 /** 145 * No-op. 146 */ 147 @Override 148 public void refresh() 149 { 150 locale = null; 151 } 152}