KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > HelpResources


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

11 package org.eclipse.help.internal;
12
13 import java.text.*;
14 import java.util.*;
15
16 import org.eclipse.core.runtime.*;
17
18 /**
19  * Uses a resource bundle to load images and strings from a property file.
20  */

21 public class HelpResources {
22     private static ResourceBundle resBundle;
23     static {
24         resBundle = ResourceBundle.getBundle(HelpResources.class.getName());
25     }
26     /**
27      * Resources constructor.
28      */

29     public HelpResources() {
30         super();
31     }
32     /**
33      * Returns a string from a property file
34      */

35     public static String JavaDoc getString(String JavaDoc name) {
36         try {
37             return resBundle.getString(name);
38         } catch (Exception JavaDoc e) {
39             return name;
40         }
41
42     }
43     /**
44      * Returns a string from a property file
45      */

46     public static String JavaDoc getString(String JavaDoc name, String JavaDoc replace0) {
47         try {
48             String JavaDoc stringFromPropertiesFile = resBundle.getString(name);
49             stringFromPropertiesFile = MessageFormat.format(
50                     stringFromPropertiesFile, new Object JavaDoc[]{replace0});
51             return stringFromPropertiesFile;
52         } catch (Exception JavaDoc e) {
53             return name;
54         }
55     }
56
57     /**
58      * Returns a string from a property file
59      */

60     public static String JavaDoc getString(String JavaDoc name, String JavaDoc replace0, String JavaDoc replace1) {
61         try {
62             String JavaDoc stringFromPropertiesFile = resBundle.getString(name);
63             stringFromPropertiesFile = MessageFormat.format(
64                     stringFromPropertiesFile, new Object JavaDoc[]{replace0, replace1});
65             return stringFromPropertiesFile;
66         } catch (Exception JavaDoc e) {
67             return name;
68         }
69     }
70
71     private static Locale getDefaultLocale() {
72         String JavaDoc nl = Platform.getNL();
73         // sanity test
74
if (nl == null)
75             return Locale.getDefault();
76
77         // break the string into tokens to get the Locale object
78
StringTokenizer locales = new StringTokenizer(nl, "_"); //$NON-NLS-1$
79
if (locales.countTokens() == 1)
80             return new Locale(locales.nextToken(), ""); //$NON-NLS-1$
81
else if (locales.countTokens() == 2)
82             return new Locale(locales.nextToken(), locales.nextToken());
83         else if (locales.countTokens() == 3)
84             return new Locale(locales.nextToken(), locales.nextToken(), locales
85                     .nextToken());
86         else
87             return Locale.getDefault();
88     }
89 }
90
Popular Tags