jQuery XML 翻译的字符集问题

发布于 2024-08-11 03:15:36 字数 1730 浏览 8 评论 0原文

我使用手动语言翻译和使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里梦着梦中梦 2024-08-18 03:15:36

我最终通过将 xml 文件的字符集直接更改为 找到了解决方案,

<?xml version="1.0" encoding="ISO-8859-1"?>

这使得其他语言的所有重音都可以立即查看...

谢谢。

I eventually found the solution by changing the charset of the xml file directly to

<?xml version="1.0" encoding="ISO-8859-1"?>

That made all the accents from other languages instantly viewable...

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文