1 /* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.istack.internal.localization; 27 28 import com.sun.istack.internal.localization.LocalizableMessageFactory.ResourceBundleSupplier; 29 import java.text.MessageFormat; 30 import java.util.HashMap; 31 import java.util.Locale; 32 import java.util.MissingResourceException; 33 import java.util.ResourceBundle; 34 35 /** 36 * Localizes the {@link Localizable} into a message 37 * by using a configured {@link Locale}. 38 * 39 * @author WS Development Team 40 */ 41 public class Localizer { 42 43 private final Locale _locale; 44 private final HashMap _resourceBundles; 45 46 public Localizer() { 47 this(Locale.getDefault()); 48 } 49 50 public Localizer(Locale l) { 51 _locale = l; 52 _resourceBundles = new HashMap(); 53 } 54 55 public Locale getLocale() { 56 return _locale; 57 } 58 59 public String localize(Localizable l) { 60 String key = l.getKey(); 61 if (key == Localizable.NOT_LOCALIZABLE) { 62 // this message is not localizable 63 return (String) l.getArguments()[0]; 64 } 65 66 String bundlename = l.getResourceBundleName(); 67 68 try { 69 ResourceBundle bundle = 70 (ResourceBundle) _resourceBundles.get(bundlename); 71 72 if (bundle == null) { 73 bundle = l.getResourceBundle(_locale); 74 if (bundle != null) { 75 _resourceBundles.put(bundlename, bundle); 76 } 77 } 78 79 if (bundle == null) { 80 try { 81 bundle = ResourceBundle.getBundle(bundlename, _locale); 82 } catch (MissingResourceException e) { 83 // work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist: 84 // all files with an extension different from .class (hence all the .properties files) 85 // get copied to the top level directory instead of being in the package where they 86 // are defined 87 // so, since we can't find the bundle under its proper name, we look for it under 88 // the top-level package 89 90 int i = bundlename.lastIndexOf('.'); 91 if (i != -1) { 92 String alternateBundleName = 93 bundlename.substring(i + 1); 94 try { 95 bundle = 96 ResourceBundle.getBundle( 97 alternateBundleName, 98 _locale); 99 } catch (MissingResourceException e2) { 100 //try context classloader 101 try { 102 bundle = ResourceBundle.getBundle(bundlename, _locale, Thread.currentThread().getContextClassLoader()); 103 } catch (MissingResourceException e3) { 104 // give up 105 return getDefaultMessage(l); 106 } 107 108 } 109 } 110 } 111 112 _resourceBundles.put(bundlename, bundle); 113 } 114 115 if (bundle == null) { 116 return getDefaultMessage(l); 117 } 118 119 if (key == null) 120 key = "undefined"; 121 122 String msg; 123 try { 124 msg = bundle.getString(key); 125 } catch (MissingResourceException e) { 126 // notice that this may throw a MissingResourceException of its own (caught below) 127 msg = bundle.getString("undefined"); 128 } 129 130 // localize all arguments to the given localizable object 131 Object[] args = l.getArguments(); 132 for (int i = 0; i < args.length; ++i) { 133 if (args[i] instanceof Localizable) 134 args[i] = localize((Localizable) args[i]); 135 } 136 137 String message = MessageFormat.format(msg, args); 138 return message; 139 140 } catch (MissingResourceException e) { 141 return getDefaultMessage(l); 142 } 143 144 } 145 146 private String getDefaultMessage(Localizable l) { 147 String key = l.getKey(); 148 Object[] args = l.getArguments(); 149 StringBuilder sb = new StringBuilder(); 150 sb.append("[failed to localize] "); 151 sb.append(key); 152 if (args != null) { 153 sb.append('('); 154 for (int i = 0; i < args.length; ++i) { 155 if (i != 0) 156 sb.append(", "); 157 sb.append(String.valueOf(args[i])); 158 } 159 sb.append(')'); 160 } 161 return sb.toString(); 162 } 163 }