Java tutorial
//package com.java2s; /** * Copyright (c) 2015, 2016 IBM Corporation. All rights reserved. * <p/> * 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 * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * 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. */ import java.text.MessageFormat; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; public class Main { /** * Default resource bundle base name for localization. */ static final String DEFAULT_LOCALIZATION_BASE_NAME = "com.ibm.caas.Messages"; static String localize(String messageId, Object... parameters) { return localize(Locale.getDefault(), DEFAULT_LOCALIZATION_BASE_NAME, messageId, parameters); } static String localize(Locale locale, String baseName, String messageId, Object... parameters) { //Log.v(LOG_TAG, String.format("locale=%s, baseName=%s, messageId=%s, parameters=%s", locale, baseName, messageId, Arrays.asList(parameters))); if (locale == null) { locale = Locale.getDefault(); } try { ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale); String source = bundle.getString(messageId); return MessageFormat.format(source, parameters); } catch (MissingResourceException e) { return "message '" + messageId + "' could not be localized (" + e.getMessage() + ")"; } } }