jQuery XML 翻译的字符集问题
我使用手动语言翻译和使用 jquery-translate libray 的谷歌翻译的混合方法。我已经让 google 方法正常工作,但是 google 缺少我雇主的客户需要的一些语言...
所以我使用 jquery 读取包含英语 + 法语加拿大语言的 xml 文件,对于示例页面,我能够读取 xml,并使用 nodeContainsText 替换页面上的文本,但是当我这样做时。通过谷歌翻译即法语完美显示文本的重音。
所以我的问题是如何获取 xml 数据作为正确的字符集/内容类型,以便正确显示。
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "xmldata.xml",
dataType: "xml",
success: parseXml
});
});
function parseXml(xml)
{
//find every Tutorial and print the author
$(xml).find("english").each(function()
{
$(this).find('phrase').each(function(){
english[count] = $(this).text();
console.log('Adding to english array..['+english[count]+']');
count = count + 1;
});
});
count = 1;
$(xml).find("french_canadian").each(function()
{
$(this).find('phrase').each(function(){
frca[count] = $(this).text();
console.log('Adding to frca array..['+frca[count]+']');
count = count + 1;
});
});
$('body').nodesContainingText().each(function(){
// step 1 search through all text nodes
var $el = $(this), text = $el.text() || $el.val();
var nn = this.nodeName;
// step 2 find position in english array for word
for(var i=0;i<english.length;i++) {
// step 3 replace with french canadian version from array
if (english[i] == text) {
// which node dot text = value
console.log('Replacing...'+english[i]+' with '+frca[i]);
$el.text(frca[i]);
}
}
});
}
正如你所看到的,我通过 firebug 使用 console.log 来帮助调试内容/字符集问题的来源...
我似乎无法掌握将从 xml 文件传入的数据转换为正确的 htmlentities 编码的语法..
我知道它应该是 .html() 但当我添加它时,它只会导致错误。那么有人可以给我一个线索吗?
谢谢。
Iam using a mixed approach of manual language translation, and google translation using jquery-translate libray. I have gotten the google method to work correctly, but google is missing some languages my employer's client requires...
So I am using jquery to read an xml file that has english + french canadian languages, for a sample page, I am able to read the xml,and to use nodeContainsText to replace text on a page, however when I do so. the text's accents that are displayed perfectly via google translation ie french.
So my problem is how do i get the xml data as a correct charset/content type, so as to display that correctly.
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "xmldata.xml",
dataType: "xml",
success: parseXml
});
});
function parseXml(xml)
{
//find every Tutorial and print the author
$(xml).find("english").each(function()
{
$(this).find('phrase').each(function(){
english[count] = $(this).text();
console.log('Adding to english array..['+english[count]+']');
count = count + 1;
});
});
count = 1;
$(xml).find("french_canadian").each(function()
{
$(this).find('phrase').each(function(){
frca[count] = $(this).text();
console.log('Adding to frca array..['+frca[count]+']');
count = count + 1;
});
});
$('body').nodesContainingText().each(function(){
// step 1 search through all text nodes
var $el = $(this), text = $el.text() || $el.val();
var nn = this.nodeName;
// step 2 find position in english array for word
for(var i=0;i<english.length;i++) {
// step 3 replace with french canadian version from array
if (english[i] == text) {
// which node dot text = value
console.log('Replacing...'+english[i]+' with '+frca[i]);
$el.text(frca[i]);
}
}
});
}
As you can see I am using console.log via firebug to help debug where the content/charset issue comes from...
I just can't seem to grasp the syntax to convert the data incoming from the xml file into a correct htmlentities coding..
I know it's supposed to be .html() but when i add that, it just causes errors. So can someone give me a clue?
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终通过将 xml 文件的字符集直接更改为 找到了解决方案,
这使得其他语言的所有重音都可以立即查看...
谢谢。
I eventually found the solution by changing the charset of the xml file directly to
That made all the accents from other languages instantly viewable...
Thanks.