KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > LocalizationUtils


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.runtime;
12
13 import java.lang.reflect.Field JavaDoc;
14
15 /**
16  * Helper methods related to string localization.
17  *
18  * @since org.eclipse.equinox.common 3.3
19  */

20 public class LocalizationUtils {
21     /**
22      * This method can be used in the absence of NLS class. The method tries to
23      * use the NLS-based translation routine. If it falls, the method returns the original
24      * non-translated key.
25      *
26      * @param key case-sensetive name of the filed in the translation file representing
27      * the string to be translated
28      * @return
29      */

30     static public String JavaDoc safeLocalize(String JavaDoc key) {
31         try {
32             Class JavaDoc messageClass = Class.forName("org.eclipse.core.internal.runtime.CommonMessages"); //$NON-NLS-1$
33
if (messageClass == null)
34                 return key;
35             Field JavaDoc field = messageClass.getDeclaredField(key);
36             if (field == null)
37                 return key;
38             Object JavaDoc value = field.get(null);
39             if (value instanceof String JavaDoc)
40                 return (String JavaDoc) value;
41         } catch (ClassNotFoundException JavaDoc e) {
42             // eat exception and fall through
43
} catch (NoClassDefFoundError JavaDoc e) {
44             // eat exception and fall through
45
} catch (SecurityException JavaDoc e) {
46             // eat exception and fall through
47
} catch (NoSuchFieldException JavaDoc e) {
48             // eat exception and fall through
49
} catch (IllegalArgumentException JavaDoc e) {
50             // eat exception and fall through
51
} catch (IllegalAccessException JavaDoc e) {
52             // eat exception and fall through
53
}
54         return key;
55     }
56 }
57
Popular Tags