
com.jfinal.i18n.I18n Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jfinal-java8 Show documentation
Show all versions of jfinal-java8 Show documentation
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.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import com.jfinal.kit.StrKit;
/**
* I18N support.
*
* Example:
* 1: Create resource file "demo_zh_CN.properties" content with: msg=你好 JFinal! 你好{0}!
* Create resource file "demo_en_US.properties" content with: msg=Hello JFinal! Hello {0}!
*
* 2: Res res = I18n.use("demo", "zh_CN");
* res.get("msg"); // return value: 你好 JFinal! 你好{0}!
* res.format("msg", "詹波"); // return value: 你好 JFinal! 你好詹波!
*
* res = I18n.use("demo", "en_US");
* res.get("msg"); // return value: Hello JFinal! Hello{0}!
* res.format("msg", "james"); // return value: Hello JFinal! Hello James!
*
*/
public class I18n {
static String defaultBaseName = "i18n";
static String defaultLocale = Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
private static final ConcurrentHashMap resMap = new ConcurrentHashMap();
private I18n(){
}
public static void setDefaultBaseName(String defaultBaseName) {
if (StrKit.isBlank(defaultBaseName)) {
throw new IllegalArgumentException("defaultBaseName can not be blank.");
}
I18n.defaultBaseName = defaultBaseName;
}
public static void setDefaultLocale(String defaultLocale) {
if (StrKit.isBlank(defaultLocale)) {
throw new IllegalArgumentException("defaultLocale can not be blank.");
}
I18n.defaultLocale = defaultLocale;
}
/**
* Using the base name and locale to get the Res object, which is used to get i18n message value from the resource file.
* @param baseName the base name to load Resource bundle
* @param locale the locale string like this: "zh_CN" "en_US"
* @return the Res object to get i18n message value
*/
public static Res use(String baseName, String locale) {
String resKey = baseName + locale;
Res res = resMap.get(resKey);
if (res == null) {
res = new Res(baseName, locale);
resMap.put(resKey, res);
}
return res;
}
public static Res use(String baseName, Locale locale) {
return use(baseName, toLocale(locale));
}
public static Res use(String locale) {
return use(defaultBaseName, locale);
}
public static Res use() {
return use(defaultBaseName, defaultLocale);
}
public static Locale toLocale(String locale) {
String[] array = locale.split("_");
if (array.length == 1) {
return new Locale(array[0]);
}
return new Locale(array[0], array[1]);
}
public static String toLocale(Locale locale) {
return locale.getLanguage() + "_" + locale.getCountry();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy