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;
}