The actual solution works if the Strings are already in the Javascript files.But what happens when you have localized Strings in your database and want to pass them to your Javascript code? You could create a XML
file named YourPageJS.xml
and store Javascript code there identified by some keys.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="showModal">
<![CDATA[
$(''#{0}'').modal(''show'',{1});
]]>
</entry>
</properties>
You call this code through the following method that is located in an utility class,passing your parameters:
public static String getJsByClass(Class clazz, String name, Object... params)
{
ResourceBundle resources = ResourceBundle.getBundle(clazz.getName()
+ "JS", Locale.getDefault(),
new XMLResourceBundleControl());
String mesaj = resources.getString(name);
mesaj = MessageFormat.format(mesaj, params);
return mesaj;
}
The call looks like this :
YourUtils.getJsByClass(YourPage.class, "showModal" ,"firstParameter","secondParameter");
where the parameters could be your localized strings.In the above case,the first parameter is the markup Id.You can add it to your html head like this:
response.render(JavaScriptHeaderItem.forScript(YourUtils.getJsByClass(YourPage.class, "showModal" ,"firstParameter","secondParameter"), "yourJSid"));
(Wicket 6)
Also,in the XML
you must transform two characters: '
becomes ''
and {
becomes '{'
for parsing reasons.