View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.ui.i18n;
20  
21  import java.text.MessageFormat;
22  import java.util.MissingResourceException;
23  import java.util.ResourceBundle;
24  
25  import net.sf.magicproject.deckbuilder.MdbLoader;
26  import net.sf.magicproject.token.IdConst;
27  
28  /***
29   * LanguageManager.java Created on 23 janv. 2004
30   * 
31   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
32   * @since 0.54.16
33   * @since 0.54.17 in case of the main language didn't contain the specified
34   * @since 0.54.18 language extension is no more $lang, but .lang to suit to key,
35   *        we search in the secondary one linux users, and so sourceforge web
36   *        hosting
37   */
38  public final class LanguageManagerMDB {
39  
40  	/***
41  	 * Create a new instance of this class.
42  	 */
43  	private LanguageManagerMDB() {
44  		super();
45  	}
46  
47  	/***
48  	 * The resource bundle used for all messages
49  	 */
50  	private static ResourceBundle bundle;
51  
52  	/***
53  	 * The resource bundle used if the main one did not contain a message in the
54  	 * user language
55  	 */
56  	private static ResourceBundle secondaryBundle;
57  
58  	/***
59  	 * @param key
60  	 * @return the message associated to the key
61  	 * @since 0.54.17 in case of the main language didn't contain the specified
62  	 *        key, we search in the secondary one
63  	 * @since 0.86 in case of the key has not been found in no defined languages,
64  	 *        the "-[0-9]*-" string of key is replaced by "%n" and the caught
65  	 *        value is passed as parameter. If the key not found, the original key
66  	 *        is returned.
67  	 */
68  	public static String getString(String key) {
69  		try {
70  			return bundle.getString(key);
71  		} catch (MissingResourceException e) {
72  			try {
73  				return secondaryBundle.getString(key);
74  			} catch (MissingResourceException e2) {
75  				if (key.matches(".*-[0-9]*-.*")) {
76  					String res = key.replaceFirst("-[0-9]*-", "-%n-");
77  					int index = res.indexOf("%n");
78  					final String value = key
79  							.substring(index, key.indexOf('-', index + 1));
80  					return LanguageManagerMDB.getString(res, value);
81  				} else if (key.matches(".--1-.*")) {
82  					String res = key.replaceFirst("--1-", "-%n-");
83  					return LanguageManagerMDB
84  							.getString(res, MdbLoader.unknownSmlManaHtml);
85  				} else if (key.matches(".--1-.*")) {
86  					String res = key.replaceFirst("-x-", "-%n-");
87  					return LanguageManagerMDB
88  							.getString(res, MdbLoader.unknownSmlManaHtml);
89  				}
90  				return key;
91  			}
92  		}
93  	}
94  
95  	/***
96  	 * @param key
97  	 * @param parameters
98  	 * @return the message associated to the key
99  	 * @since 0.85.38 parameters
100 	 */
101 	public static String getString(String key, Object[] parameters) {
102 		if (parameters == null || parameters.length == 0) {
103 			return getString(key);
104 		}
105 
106 		// parameters are used
107 		return MessageFormat.format(getString(key), parameters);
108 	}
109 
110 	/***
111 	 * @param key
112 	 * @param parameter
113 	 * @return the message associated to the key
114 	 * @since 0.85.38 parameter
115 	 */
116 	public static String getString(String key, String parameter) {
117 		if (parameter == null) {
118 			return getString(key);
119 		}
120 
121 		// parameter is used
122 		return MessageFormat.format(getString(key), new Object[] { parameter });
123 	}
124 
125 	/***
126 	 * @param newMdb
127 	 */
128 	public static void setMdb(String newMdb) {
129 		String root = IdConst.TBS_DIR + "/" + newMdb + "/";
130 		bundle = LanguageManager.getPrimaryBundle(root);
131 		secondaryBundle = LanguageManager.getSecondaryBundle(root);
132 	}
133 }