[英]Using jquery i18n plugin for i18n?
I am using jquery i18n plugin ( http://code.google.com/p/jquery-i18n-properties/ ) to internationalize the messages placed in jquery/js.我正在使用 jquery i18n 插件( http://code.google.com/p/jquery-i18n-properties/ )来国际化放置在 jquery/js 中的消息。 i have below project structure.我有以下项目结构。
I have some.js
file in js
folder and inside some.js
file i have to refer a properties file which is located in properties
folder of src/main/resources
folder.我在js
文件夹中有some.js
文件,在some.js
文件中,我必须引用位于src/main/resources
文件夹的properties
文件夹中的properties
文件。 can i do as below?我可以做如下吗? inside properties
folder of src/main/resources
folder i have placed Messages.properties
and Messages_en_US.properties
.在src/main/resources
文件夹的properties
文件夹中,我放置了Messages.properties
和Messages_en_US.properties
。 In both the properties files i have placed myForm.success.msg=Success
property.在这两个属性文件中,我都放置了myForm.success.msg=Success
属性。 Now i am trying to access it as below.现在我正在尝试如下访问它。 But below code does not get the value of myForm.success.msg
key.但是下面的代码没有得到myForm.success.msg
键的值。 instead of giving value, it is giving the key itself but not the value
. instead of giving value, it is giving the key itself but not the value
。 Am i referring the properties file wrongly?我是否错误地引用了属性文件? is my properties file referring correct?我的属性文件引用正确吗? Please help me.请帮我。
<script type="text/javascript" src="./public/js/some.js"></script>
<script type="text/javascript" src="./public/js/jquery.i18n.properties-min-1.0.9.js"></script>
jQuery.i18n.properties({
name:'Messages',
path:'properties/', //as i have properties file in properties folder of src/main/resources
mode:'both',
callback: function() {
alert("message "+jQuery.i18n.prop('myForm.success.msg'));
}
});
I don't try it but I think you should put the *.properties
files in public/resources
directory.我不尝试,但我认为您应该将*.properties
文件放在public/resources
目录中。
And the path shoud be:路径应该是:
jQuery.i18n.properties({
name:'Messages',
path:'../resources/', // changed here
mode:'both',
callback: function() {
alert("message "+jQuery.i18n.prop('myForm.success.msg'));
}
Try it.尝试一下。
Deploy your app to a server, eg.将您的应用程序部署到服务器,例如。 Tomcat, and then access to the pages by server. Tomcat,然后通过服务器访问页面。 The root cause of your problem is a html page can't access to properties files across domain.问题的根本原因是 html 页面无法跨域访问属性文件。
Unfortunately, that will not work.不幸的是,这行不通。 The JQuery plugin will always look for a properties file that are within the webcontent
folder. JQuery 插件将始终查找webcontent
文件夹中的属性文件。 Try this instead:试试这个:
Copy those properties file inside your webapp folder and test it with actual path.将这些属性文件复制到您的 webapp 文件夹中,并使用实际路径对其进行测试。 eg resources/custom/i18n/
[Note - Here I placed the properties file inside a i18n
folder and placed a resources
folder under webapp
].例如resources/custom/i18n/
[注意 - 这里我将属性文件放在i18n
文件夹中,并将resources
文件夹放在webapp
下]。 This will fix your problem!这将解决您的问题!
Now we can manage two files, but do we really need it?现在我们可以管理两个文件,但我们真的需要它吗? The easiest thing to do is have your file only in the src/main/resources
folder, but as part of your build step copy this properties file to your second location (ie inside the webapp
folder).最简单的方法是将您的文件仅放在src/main/resources
文件夹中,但作为构建步骤的一部分,将此属性文件复制到您的第二个位置(即webapp
文件夹内)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.