Created
July 10, 2012 10:10
-
-
Save thibaudcolas/3082465 to your computer and use it in GitHub Desktop.
i18n internationalization with parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.MessageFormat; | |
/** | |
* {@inheritDoc} | |
* @return True if the given key exists for our context, false otherwise. | |
*/ | |
@Override | |
public boolean render(InternalContextAdapter context, Writer writer, | |
Node node) | |
throws IOException, ResourceNotFoundException, | |
ParseErrorException, MethodInvocationException { | |
// Key is the first child of our node for the given context. | |
String key = String.valueOf(node.jjtGetChild(0).value(context)); | |
BundleList b = (BundleList)(context.get(BundleList.KEY)); | |
if (b != null) { | |
// Get value from the bundle | |
String msg = b.getValue(key); | |
// Number of childs / params for our node. | |
int params = node.jjtGetNumChildren(); | |
// Stronger: indexOf('{') != -1 && indexOf('{') < indexOf('}') | |
// Still possible to use { & } if the message has no parameter. | |
if ((params > 1) && (msg.indexOf('{') != -1)) { | |
Object[] args = new Object[params - 1]; | |
for (int i=1; i<params; i++) { | |
args[i-1] = node.jjtGetChild(i).value(context); | |
} | |
// Replaces all of the {vars} with their {args}. | |
// Call syntax -> #i18n('project.label' ${it.title}) | |
// Properties syntax : project.label = {0} Project | |
msg = MessageFormat.format(msg, args); | |
if (log.isDebugEnabled()) { | |
log.debug("#i18n render - args inserted for " + key); | |
} | |
} | |
// Write value where the key was. | |
writer.write(msg); | |
return true; | |
} | |
else { | |
log.warn("{}: Failed to resolved key \"{}\"" + | |
": no bundle defined in template {}", | |
this.getName(), key, node.getTemplateName()); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment