Re: Yet another I18N approach

Splash Forums PrettyFaces Users Yet another I18N approach Re: Yet another I18N approach

#23475

mannyhan1968
Member

Hi Lincoln,

the property stuff is just what you’d expect:

private static Map<String, Properties> langToViews = new HashMap<String, Properties>();
private static Map<String, Properties> langToPaths = new HashMap<String, Properties>();
private String getView(String lang, String path) {
Properties viewMap = getViewMap(lang);
String view = viewMap.getProperty(path);
return String.format("/%s.jsf", view);
}
private Properties getViewMap(String lang) {
Properties props = langToViews.get(lang);
if (props == null) {
props = new Properties();
try {
props.load(getClass().getResourceAsStream(String.format("PathToView_%s.properties", lang)));
langToViews.put(lang, props);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
return props;
}
private String getPath(String lang, String view) {
Properties pathMap = getPathMap(lang);
String path = pathMap.getProperty(view);
// NOTE: context path is prepended automatically
return String.format("/%s/%s", lang, path);
}
private Properties getPathMap(String lang) {
Properties props = langToPaths.get(lang);
if (props == null) {
props = new Properties();
try {
props.load(getClass().getResourceAsStream(String.format("ViewToPath_%s.properties", lang)));
langToPaths.put(lang, props);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
return props;
}

And here are the property files:

PathToView_de.properties:

Suchen = search
Aktualisieren = update

PathToView_en.properties:

Search = search
Update = update

ViewToPath_xx.properties are just the reversed maps (of course this is redundant and you’d better reverse the maps on the fly).

Best regards,

Harald