Java tutorial
/* * Copyright (C) 2010-2101 Alibaba Group Holding Limited. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.otter.shared.common.utils.code; import java.text.MessageFormat; import java.util.MissingResourceException; import java.util.ResourceBundle; import org.apache.commons.lang.StringUtils; /** * <pre> * ResourceBundle * ???,?? ResourceBundleUtil,?,key??? * * : * * ? res/BundleName.properties: * key1=value1 * key2=value2,{0} * * ?: * ResourceBundleUtil util = new ResourceBundleUtil("res/BundleName"); * util.getMessage("key1"); //:value1 * util.getMessage("key2","stone"); //:value2,stone * </pre> * * @author jianghang 2011-9-13 ?04:44:05 */ public class ResourceBundleUtil { private ResourceBundle bundle; // ? /** * ResourceBundleUtil,?bundle * * @param bundleName ??? * @throws MissingResourceException ??,? */ public ResourceBundleUtil(String bundleName) { this.bundle = ResourceBundle.getBundle(bundleName); } /** * <pre> * ?,?key,??? * ?,keymessage???,?params????? * * keynull,null * keymessagenull,null * </pre> * * @param key ??? * @param params ?? * @return ??? */ public String getMessage(String key, String... params) { // ? if (key == null) { return null; } // message String msg = bundle.getString(key); // ??,msg if (params == null || params.length == 0) { return msg; } // ?,?message if (StringUtils.isBlank(msg)) { // msgnull,msg return msg; } return MessageFormat.format(msg, (Object[]) params); } }