001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.i18n; 020 021import java.text.MessageFormat; 022import java.util.Locale; 023import java.util.MissingResourceException; 024import java.util.ResourceBundle; 025 026import org.apache.wiki.WikiEngine; 027 028 029/** 030 * Manages all internationalization in JSPWiki. 031 * 032 * @since 2.6 033 */ 034public class InternationalizationManager { 035 036 /** The name of the ResourceBundle which contains any and all JSPWiki core 037 * resource strings. It's value is {@value}. 038 */ 039 public static final String CORE_BUNDLE = "CoreResources"; 040 041 /** The name of the ResourceBundle which contains any and all JSPWiki default templates 042 * resource strings. It's value is {@value}. 043 */ 044 public static final String DEF_TEMPLATE = "templates.default"; 045 // public static final String JSPWIKI_BUNDLE = "jspwiki"; 046 // public static final String PLUGINS_BUNDLE = "plugins"; 047 048 /** 049 * Constructs a new InternationalizationManager. 050 * 051 * @param engine To which engine this belongs to 052 */ 053 public InternationalizationManager( WikiEngine engine ) 054 { 055 } 056 057 /** 058 * Returns a String from the CORE_BUNDLE using English as the default 059 * locale. 060 * 061 * @param key Key to find 062 * @return The English string 063 * @throws MissingResourceException If there is no such key 064 */ 065 public String get( String key ) throws MissingResourceException 066 { 067 return get( CORE_BUNDLE, Locale.ENGLISH, key ); 068 } 069 070 /** 071 * Finds a resource bundle. 072 * 073 * @param bundle The ResourceBundle to find. Must exist. 074 * @param locale The Locale to use. Set to null to get the default locale. 075 * @return A localized string 076 * @throws MissingResourceException If the key cannot be located at all, even from the default locale. 077 */ 078 public ResourceBundle getBundle( String bundle, Locale locale ) throws MissingResourceException 079 { 080 if( locale == null ) 081 { 082 locale = Locale.getDefault(); 083 } 084 085 ResourceBundle b = ResourceBundle.getBundle( bundle, locale ); 086 087 return b; 088 } 089 090 /** 091 * If you are too lazy to open your own bundle, use this method 092 * to get a string simply from a bundle. 093 * @param bundle Which bundle the string is in 094 * @param locale Locale to use - null for default 095 * @param key Which key to use. 096 * @return A localized string (or from the default language, if not found) 097 * @throws MissingResourceException If the key cannot be located at all, even from the default locale. 098 */ 099 public String get( String bundle, Locale locale, String key ) throws MissingResourceException 100 { 101 return getBundle( bundle, locale ).getString( key ); 102 } 103 104 /** 105 * Obtain a parameterized String from the bundle. 106 * @param bundle Which bundle the string is in 107 * @param locale Locale to use - null for default 108 * @param key Which key to use. 109 * @param args parameters to insert in the String. 110 * @return A localized string (or from the default language, if not found) 111 * @throws MissingResourceException If the key cannot be located at all, even from the default locale. 112 */ 113 public String get( String bundle, Locale locale, String key, Object... args ) throws MissingResourceException 114 { 115 MessageFormat mf = new MessageFormat( get( bundle, locale, key ), locale ); 116 return mf.format( args ); 117 } 118 119}