简体   繁体   English

Ajax HTML文档I18n

[英]Ajax html document I18n

I am using that code : 我正在使用该代码:

$.get(
    'filename.html',
    function(content)
    {
        $('#container').empty().append(content);
    }
);

to load some external files and place the content into the current working page. 加载一些外部文件并将内容放入当前工作页面。 The external files that I load are in plain HTML and I like to ask, if there is any good way to internationalize that loaded pages. 我加载的外部文件使用纯HTML格式,我想问一下是否有什么国际化加载页面的好方法。

In my current page I have the opportunity to create a javascript object with all translation keys like: 在我当前的页面上,我有机会创建一个具有所有翻译键的javascript对象,例如:

var i18n = {
    'doc1' : {
        'title' : 'This is a title'
    }
};

In example the external files looks like that 例如,外部文件如下所示

<h3>This is a title</h3>
.....

Is there a way to change that to something similar to 有没有办法将其更改为类似于

<h3>{doc1.title}</h3>
.....

and then after the load to replace the doc1.title; 然后在加载后替换doc1.title;

May be something like this you're looking for: 您可能正在寻找这样的东西:

1. Updated 1.更新

JavaScript: JavaScript:

var i18n = { doc1: { title: 'Title', ... }, ... };
var doc_name = 'doc1';

$.get(doc_name + '.html', function(data) {
  for (var word in i18n[doc_name]) {
    data = data.replace(new RegExp('/' + word + '/', 'g'), i18n[doc_name][word]);
  }

  $('#container').html(data);
});

HTML: HTML:

<h3>{title}</h3>

2. 2。

JavaScript: JavaScript:

var doc1 = { title: 'Title', ... };

$.get("doc1.php", doc1, function(data) {
  $('#container').html(data);
});

PHP: PHP:

<h3><?= $_GET['title'] ?></h3>

May be like this: 可能是这样的:

var i18n = {
    'doc1' : {
        'title' : 'This is a title'
    }
};

$.get(
    'filename.html',
    function(content)
    {
        for (var doc in i18n) {
            for (var key in i18n[doc]) {
                var val = i18n[doc][key];
                content = content.replace( '{' + doc + '.' + key + '}', val );
            }
        }
        $('#container').empty().append(content);
    }
);

fiddle: http://jsfiddle.net/Kirrr/eW7Nn/ 小提琴: http//jsfiddle.net/Kirrr/eW7Nn/

If your retrieved document contains 如果您检索的文档包含

<h3 id="localTitle"></h3>

You could do 你可以做

$.get(
    'filename.html',
    function(content)
    {
        $('#container').empty().append(content);
        $('#container #localTitle').html('This is a title');
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM