View Javadoc
1   package ca.uhn.fhir.i18n;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.text.MessageFormat;
24  import java.util.Map;
25  import java.util.ResourceBundle;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  /**
29   * This feature is not yet in its final state and should be considered an internal part of HAPI for now - use with caution
30   */
31  public class HapiLocalizer {
32  
33  	private ResourceBundle myBundle;
34  	private final Map<String, MessageFormat> myKeyToMessageFormat = new ConcurrentHashMap<String, MessageFormat>();
35  
36  	public HapiLocalizer() {
37  		myBundle = ResourceBundle.getBundle(HapiLocalizer.class.getPackage().getName() + ".hapi-messages");
38  	}
39  
40  	public String getMessage(String theKey, Object... theParameters) {
41  		if (theParameters != null && theParameters.length > 0) {
42  			MessageFormat format = myKeyToMessageFormat.get(theKey);
43  			if (format != null) {
44  				return format.format(theParameters).toString();
45  			}
46  			
47  			String formatString = myBundle.getString(theKey);
48  			if (formatString== null) {
49  				formatString = "!MESSAGE!";
50  			}
51  			
52  			format = new MessageFormat(formatString);
53  			myKeyToMessageFormat.put(theKey, format);
54  			return format.format(theParameters).toString();
55  		} else {
56  			String retVal = myBundle.getString(theKey);
57  			if (retVal == null) {
58  				retVal = "!MESSAGE!";
59  			}
60  			return retVal;
61  		}
62  	}
63  	
64  	
65  }