All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jfinal.i18n.Res Maven / Gradle / Ivy

Go to download

JFinal is a simple, light, rapid,independent, extensible Java WEB + ORM framework. The feature of JFinal looks like ruby on rails especially ActiveRecord.

The newest version!
/**
 * Copyright (c) 2011-2019, James Zhan 詹波 ([email protected]).
 *
 * 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.jfinal.i18n;

import java.text.MessageFormat;
import java.util.ResourceBundle;
import com.jfinal.kit.StrKit;

/**
 * Res is used to get the message value from the ResourceBundle of the related Locale.
 */
public class Res {
	
	private final ResourceBundle resourceBundle;
	
	public Res(String baseName, String locale) {
		if (StrKit.isBlank(baseName)) {
			throw new IllegalArgumentException("baseName can not be blank");
		}
		if (StrKit.isBlank(locale)) {
			throw new IllegalArgumentException("locale can not be blank, the format like this: zh_CN or en_US");
		}
		
		this.resourceBundle = ResourceBundle.getBundle(baseName, I18n.toLocale(locale));
	}
	
	/**
	 * Get the message value from ResourceBundle of the related Locale.
	 * @param key message key
	 * @return message value
	 */
	public String get(String key) {
		return resourceBundle.getString(key);
	}
	
	/**
	 * Get the message value from ResourceBundle by the key then format with the arguments.
	 * Example:
* In resource file : msg=Hello {0}, today is{1}.
* In java code : res.format("msg", "james", new Date());
* In freemarker template : ${_res.format("msg", "james", new Date())}
* The result is : Hello james, today is 2015-04-14. */ public String format(String key, Object... arguments) { return MessageFormat.format(resourceBundle.getString(key), arguments); } public ResourceBundle getResourceBundle() { return resourceBundle; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy