001package ca.uhn.fhir.i18n;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.text.MessageFormat;
024import java.util.Map;
025import java.util.ResourceBundle;
026import java.util.concurrent.ConcurrentHashMap;
027
028/**
029 * This feature is not yet in its final state and should be considered an internal part of HAPI for now - use with caution
030 */
031public class HapiLocalizer {
032
033        private ResourceBundle myBundle;
034        private final Map<String, MessageFormat> myKeyToMessageFormat = new ConcurrentHashMap<String, MessageFormat>();
035
036        public HapiLocalizer() {
037                myBundle = ResourceBundle.getBundle(HapiLocalizer.class.getPackage().getName() + ".hapi-messages");
038        }
039
040        public String getMessage(Class<?> theType, String theKey, Object... theParameters) {
041                return getMessage(theType.getName() + '.' + theKey, theParameters);
042        }
043        
044        public String getMessage(String theQualifiedKey, Object... theParameters) {
045                if (theParameters != null && theParameters.length > 0) {
046                        MessageFormat format = myKeyToMessageFormat.get(theQualifiedKey);
047                        if (format != null) {
048                                return format.format(theParameters).toString();
049                        }
050                        
051                        String formatString = myBundle.getString(theQualifiedKey);
052                        if (formatString== null) {
053                                formatString = "!MESSAGE!";
054                        }
055                        
056                        format = new MessageFormat(formatString);
057                        myKeyToMessageFormat.put(theQualifiedKey, format);
058                        return format.format(theParameters).toString();
059                } else {
060                        String retVal = myBundle.getString(theQualifiedKey);
061                        if (retVal == null) {
062                                retVal = "!MESSAGE!";
063                        }
064                        return retVal;
065                }
066        }
067        
068        
069}