001package ca.uhn.fhir.i18n; 002 003import static org.apache.commons.lang3.StringUtils.isNotBlank; 004 005/* 006 * #%L 007 * HAPI FHIR - Core Library 008 * %% 009 * Copyright (C) 2014 - 2017 University Health Network 010 * %% 011 * Licensed under the Apache License, Version 2.0 (the "License"); 012 * you may not use this file except in compliance with the License. 013 * You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, software 018 * distributed under the License is distributed on an "AS IS" BASIS, 019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 020 * See the License for the specific language governing permissions and 021 * limitations under the License. 022 * #L% 023 */ 024 025import java.text.MessageFormat; 026import java.util.ArrayList; 027import java.util.Enumeration; 028import java.util.HashSet; 029import java.util.List; 030import java.util.Map; 031import java.util.ResourceBundle; 032import java.util.Set; 033import java.util.concurrent.ConcurrentHashMap; 034 035/** 036 * This feature is not yet in its final state and should be considered an internal part of HAPI for now - use with caution 037 */ 038public class HapiLocalizer { 039 040 public static final String UNKNOWN_I18N_KEY_MESSAGE = "!MESSAGE!"; 041 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HapiLocalizer.class); 042 private List<ResourceBundle> myBundle = new ArrayList<ResourceBundle>(); 043 044 private final Map<String, MessageFormat> myKeyToMessageFormat = new ConcurrentHashMap<String, MessageFormat>(); 045 private String[] myBundleNames; 046 047 public HapiLocalizer() { 048 this(HapiLocalizer.class.getPackage().getName() + ".hapi-messages"); 049 } 050 051 public HapiLocalizer(String... theBundleNames) { 052 myBundleNames = theBundleNames; 053 init(); 054 } 055 056 protected void init() { 057 for (String nextName : myBundleNames) { 058 myBundle.add(ResourceBundle.getBundle(nextName)); 059 } 060 } 061 062 private String findFormatString(String theQualifiedKey) { 063 String formatString = null; 064 for (ResourceBundle nextBundle : myBundle) { 065 if (nextBundle.containsKey(theQualifiedKey)) { 066 formatString = nextBundle.getString(theQualifiedKey); 067 } 068 if (isNotBlank(formatString)) { 069 break; 070 } 071 } 072 073 if (formatString == null) { 074 ourLog.warn("Unknown localization key: {}", theQualifiedKey); 075 formatString = UNKNOWN_I18N_KEY_MESSAGE; 076 } 077 return formatString; 078 } 079 080 public String getMessage(Class<?> theType, String theKey, Object... theParameters) { 081 return getMessage(theType.getName() + '.' + theKey, theParameters); 082 } 083 084 public String getMessage(String theQualifiedKey, Object... theParameters) { 085 if (theParameters != null && theParameters.length > 0) { 086 MessageFormat format = myKeyToMessageFormat.get(theQualifiedKey); 087 if (format != null) { 088 return format.format(theParameters).toString(); 089 } 090 091 String formatString = findFormatString(theQualifiedKey); 092 093 format = new MessageFormat(formatString.trim()); 094 myKeyToMessageFormat.put(theQualifiedKey, format); 095 return format.format(theParameters).toString(); 096 } 097 String retVal = findFormatString(theQualifiedKey); 098 return retVal; 099 } 100 101 public Set<String> getAllKeys(){ 102 HashSet<String> retVal = new HashSet<String>(); 103 for (ResourceBundle nextBundle : myBundle) { 104 Enumeration<String> keysEnum = nextBundle.getKeys(); 105 while (keysEnum.hasMoreElements()) { 106 retVal.add(keysEnum.nextElement()); 107 } 108 } 109 return retVal; 110 } 111 112}