| 1 | /* |
| 2 | * Copyright 2005-2006 The RbUtils Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | // $Id$ |
| 19 | |
| 20 | package org.ktc.rbutils.api.i18n; |
| 21 | |
| 22 | import java.text.MessageFormat; |
| 23 | import java.util.Locale; |
| 24 | import java.util.MissingResourceException; |
| 25 | import java.util.ResourceBundle; |
| 26 | |
| 27 | /** |
| 28 | * Represents a message that can be localised. All messages use the same Locale to display |
| 29 | * information in a human readable form. |
| 30 | * <p> |
| 31 | * The underlying implementation uses {@link java.text.MessageFormat MessageFormat} and |
| 32 | * ResourceBundle. |
| 33 | * @since RbUtils 0.9.3.3 |
| 34 | * @version $Revision$ |
| 35 | * @author moishi |
| 36 | */ |
| 37 | public class Message { |
| 38 | /** The locale to localise messages to. */ |
| 39 | private static Locale locale = LocaleUtils.getEmptyLocale(); |
| 40 | |
| 41 | /** Key for the message format. */ |
| 42 | private final String key; |
| 43 | |
| 44 | /** Arguments for MessageFormat. */ |
| 45 | private final Object[] args; |
| 46 | |
| 47 | /** Name of the resource bundle to get messages from. */ |
| 48 | private final String bundlename; |
| 49 | |
| 50 | /** |
| 51 | * Creates a new <code>Message</code> instance. |
| 52 | * @param bundlename resource bundle name. |
| 53 | * @param key the key to locate the translation. |
| 54 | * @param args arguments for the translation. |
| 55 | */ |
| 56 | public Message(final String bundlename, final String key, final Object[] args) { |
| 57 | this.key = key; |
| 58 | this.args = args; |
| 59 | this.bundlename = bundlename; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Find a ResourceBundle for a given bundle name using the shared <code>locale</code> field. |
| 64 | * @param bundleName the bundle name. |
| 65 | * @return a ResourceBundle. |
| 66 | * @throws MissingResourceException if the bundle cannot be loaded. |
| 67 | */ |
| 68 | private ResourceBundle getBundle(final String bundleName) { |
| 69 | return ResourceBundle.getBundle(bundleName, locale); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the message in a human localized readable format. <br> |
| 74 | * If the bundle cannot be loaded, tries to format the key with the args parameter. |
| 75 | * @return the translated message. |
| 76 | * @throws NullPointerException if some attributes are <code>null</code>. |
| 77 | * @throws IllegalArgumentException if the format operation didn't succeed. |
| 78 | */ |
| 79 | public String getMessage() { |
| 80 | try { |
| 81 | final ResourceBundle bundle = getBundle(bundlename); |
| 82 | final String pattern = bundle.getString(key); |
| 83 | |
| 84 | final String message = MessageFormat.format(pattern, args); |
| 85 | return message; |
| 86 | } |
| 87 | catch (final MissingResourceException ex) { |
| 88 | // If the Check author didn't provide i18n resource bundles and logs error messages |
| 89 | // directly, this will return the author's original message |
| 90 | return MessageFormat.format(key, args); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the locale used to display the localized message. |
| 96 | * <p> |
| 97 | * All Message share the same Locale. |
| 98 | * @return the locale used to display the localized message. |
| 99 | */ |
| 100 | public static Locale getLocale() { |
| 101 | return locale; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sets the locale to use for localization. |
| 106 | * <p> |
| 107 | * <b>WARNING:</b> this Locale will be used by all Message in the Thread. |
| 108 | * <p> |
| 109 | * If <code>null</code>, the locale is set with the |
| 110 | * {@link LocaleUtils#EMPTY_LOCALE empty locale}. |
| 111 | * @param theLocale the locale to use for localization. |
| 112 | */ |
| 113 | public static void setLocale(final Locale theLocale) { |
| 114 | // Needed because a NullPointerException occured on getMessage() call with a null locale |
| 115 | // Patch on the 0.9.1 version |
| 116 | if (theLocale == null) { |
| 117 | locale = LocaleUtils.getEmptyLocale(); |
| 118 | } |
| 119 | else { |
| 120 | locale = theLocale; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } |