|
Jmeter在国际化资源解析方面做得不错,适应不同的Locale去展现UI文字以及图片。通过修改属性文件jmeter.properties的language达到一套程序适应多国语言。
具体目录见:
图片:src\core\org\apache\jmeter\images
多语言文件:src\core\org\apache\jmeter\resources
核心的类是Locale和ResourceBundle。
具体的资源文件制作可以用UE或者专业的I18Nedit(可从sourceforge.net下载)
下面是自己组合源码来简单测试:
package org.apache.jmeter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Vector;
public class I18ResourcesTest {
private static ResourceBundle resources;
private static Locale locale;
//初始化locale
//根据情况重新设置
public static void initLocale(String language ) {
String loc = language; //appProperties.getProperty("language"); // $NON-NLS-1$
if (loc != null) {
String []parts = split(loc,"_",true);// $NON-NLS-1$
if (parts.length==2) {
setLocale(new Locale(parts[0], parts[1]));
} else {
setLocale(new Locale(loc, "")); // $NON-NLS-1$
}
} else {
setLocale(Locale.getDefault());
}
}
public static void setLocale(Locale loc) {
locale = loc;
/*
* See bug 29920. getBundle() defaults to the property file for the
* default Locale before it defaults to the base property file, so we
* need to change the default Locale to ensure the base property file is
* found.
*/
Locale def = null;
if (loc.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
def = Locale.getDefault();
// Don't change locale from en_GB to en
if (!def.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
Locale.setDefault(Locale.ENGLISH);
} else {
def = null; // no need to reset Locale
}
}
//获取资源文件
//org.apache.jmeter.resources.messages是一个basename,存在org\apache\jmeter\resources\messages.properties
//或者org\apache\jmeter\resources\messages_zh_CN.properties的属性文件
//内容 key=value,如 monitor_legend_memory_per=Memory % (used/total)
resources = ResourceBundle.getBundle("org.apache.jmeter.resources.messages", locale); // $NON-NLS-1$
/*
* Reset Locale if necessary so other locales are properly handled
*/
if (def != null) {
Locale.setDefault(def);
}
}
public static String getResStringDefault(String key) {
if (key == null) {
return null;
}
// Resource keys cannot contain spaces
key = key.replace(' ', '_'); // $NON-NLS-1$ // $NON-NLS-2$
key = key.toLowerCase(java.util.Locale.ENGLISH);
String resString = null;
try {
resString = resources.getString(key);
} catch (MissingResourceException mre) {
resString ="";
}
return resString;
}
public static String[] split(String splittee, String splitChar,boolean truncate) {
if (splittee == null || splitChar == null) {
return new String[0];
}
final String EMPTY_ELEMENT = "";
int spot;
final int splitLength = splitChar.length();
final String adjacentSplit = splitChar + splitChar;
final int adjacentSplitLength = adjacentSplit.length();
if(truncate) {
while ((spot = splittee.indexOf(adjacentSplit)) != -1) {
splittee = splittee.substring(0, spot + splitLength)
+ splittee.substring(spot + adjacentSplitLength, splittee.length());
}
if(splittee.startsWith(splitChar)) {
splittee = splittee.substring(splitLength);
}
if(splittee.endsWith(splitChar)) { // Remove trailing splitter
splittee = splittee.substring(0,splittee.length()-splitLength);
}
}
Vector returns = new Vector();
final int length = splittee.length(); // This is the new length
int start = 0;
spot = 0;
while (start < length && (spot = splittee.indexOf(splitChar, start)) > -1) {
if (spot > 0) {
returns.addElement(splittee.substring(start, spot));
}
else
{
returns.addElement(EMPTY_ELEMENT);
}
start = spot + splitLength;
}
if (start < length) {
returns.add(splittee.substring(start));
} else if (spot == length - splitLength){// Found splitChar at end of line
returns.addElement(EMPTY_ELEMENT);
}
String[] values = new String[returns.size()];
returns.copyInto(values);
return values;
}
public static void main(String[] args) {
//String language="";
//格式:语言_国家/地区
String language="zh_CN"; //zh_CN,zh_TW;"",es
initLocale(language);
System.out.println(getResStringDefault("monitor_legend_memory_per"));
System.out.println(getResStringDefault("monitor_legend_load"));
}
} |
|