如何本地化JavaScript中的JavaScript文件

问题描述:

Hallo,Wicket i18n问题: 我有一个javaScript文件,它包含我想本地化的字符串。 理想情况下,我想本地化整个文件。喜欢的东西如何本地化JavaScript中的JavaScript文件

  • my.js
  • my_de.js
  • my_fr.js
  • ...

哪里检票口会自动选择合适的js文件,因为它与属性文件呢。

任何人都知道如何做到这一点?

传递语言环境资源引用:

class MyPage extends WebPage implements IHeaderContributor { 
    public void renderHead(IHeaderResponse response) { 
     response.renderJavascriptReference(new ResourceReference(
     MyPage.class, "my.js", getLocale(), getStyle())); 
    } 
} 
+0

我想出了一个使用PackageResource的解决方案,但你的更短。非常感谢。 – nebenmir 2010-10-21 05:41:15

如果字符串已经在使用Javascript files.But当你在你的数据库本地化的字符串,并希望它们传递到发生了什么实际的解决方案的工作你的Javascript代码?您可以创建一个名为YourPageJS.xmlXML文件,并存储由某些键标识的Javascript代码。

<?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> 

你叫通过位于一个实用工具类,下面的方法验证码,通过你的参数:

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

调用看起来是这样的:

YourUtils.getJsByClass(YourPage.class, "showModal" ,"firstParameter","secondParameter"); 

其中的参数可能是您的本地化字符串。在上述情况下,第一个参数是标记Id。您可以将它添加到您的html头像,如下所示:

response.render(JavaScriptHeaderItem.forScript(YourUtils.getJsByClass(YourPage.class, "showModal" ,"firstParameter","secondParameter"), "yourJSid"));(检票6)

另外,在XML必须变换两个字符:'变得''{用于解析原因变得'{'