Skip to content

Instantly share code, notes, and snippets.

@xcooper
Created July 29, 2013 09:58
Show Gist options
  • Save xcooper/6103311 to your computer and use it in GitHub Desktop.
Save xcooper/6103311 to your computer and use it in GitHub Desktop.
This class is intentional to be extend to get all i18n key-value pairs using Map
package com.gss.tds.dc.struts2.action;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
/**
* This class is intentional to be extend to get all i18n key-value pairs using Map<br>
*
* @author xcooper
*
*/
@Slf4j
public abstract class AbstractDumpI18nAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 3997633807822833093L;
protected AbstractDumpI18nAction() {
super();
}
/**
* Get all i18n key-value pairs
* @return
*/
protected Map<String, String> dumpAllI18nOfAction() {
Map<String, String> ret = new HashMap<String, String>();
List<ResourceBundle> rbs = digAllResourceBundlesThroughHierarchy();
Iterator<ResourceBundle> it = rbs.iterator();
while (it.hasNext()) {
ResourceBundle rb = it.next();
Enumeration<String> keys = rb.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = rb.getString(key);
ret.put(key, value);
}
}
return ret;
}
private List<ResourceBundle> digAllResourceBundlesThroughHierarchy() {
List<ResourceBundle> ret = new ArrayList<ResourceBundle>();
String className = this.getClass().getName();
String[] classNameFraguments = className.split("\\.");
if (log.isTraceEnabled()) {
log.trace("break down class name - {}", className);
}
int len = classNameFraguments.length;
String clsName = className;
while(len > 0) {
ResourceBundle resourceBundle = getTexts(clsName);
if (resourceBundle != null) {
ret.add(resourceBundle);
}
String[] cnf = Arrays.copyOfRange(classNameFraguments, 0, len--);
cnf[len] = "package";
clsName = StringUtils.join(cnf, '.');
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment