問題描述
我想了解是否可以(以及支持的程度)在 HTML 文檔中使用自定義實體進行本地化.
I'd like to understand if it is possible (and, in case, how well supported) to use custom entities in HTML documents for localization purposes.
我的設想是做這樣的事情:
What I envision is doing something like this:
<!DOCTYPE html "/locales/en-us.ent">
<html>
<head>
<title>&contactus.title;</title>
</head>
<body>
<p>&contactus.youcanreach;<br>123, Example Road<br>12345 Example City</p>
<ul id="menu">
<li>&menu.home;</li>
<li>&menu.products;</li>
<li>&menu.contactus;</li>
</ul>
</body>
</html>
所有實體都將存儲在一個文件中(每種語言一個,在上面的示例中為 en-us.ent),該文件包含在文檔的頂部,例如
and all entities would be stored in a file (one for each language, en-us.ent in the example above) that gets included at the top of the document, e.g.
<!ENTITY menu.home "Home">
<!ENTITY menu.products "Products">
<!ENTITY menu.contactus "Contact us">
...
最終,這甚至可以擴展到 HTML 片段(不確定這是否真的允許),這可能對所有頁面(例如標題、菜單等)都有用;在上面的示例中,整個 <ul>
可能就是這樣一個片段)
Eventually this could even be exapnded to HTML fragments (not sure if this is really allowed) that may be useful on all pages (such as headers, menus, etc.; in the example above, the whole <ul>
could be such a fragment)
現在,我的理解是,這在 XHTML 中理論上是可行的,但我想知道這是否也可以在 HTML 中完成,以防萬一,瀏覽器(和爬蟲)的處理能力如何.
Now, my understanding is that this is theoretically possible in XHTML, but I was wondering if this can be done also in HTML and, in case, how well browsers (and crawlers) would cope.
推薦答案
理論上是可以的.HTML 4.x(和幾個以前的版本)是 SGML 應用程序,因此您可以使用新實體擴展 DTD.
In theory, it is possible. HTML 4.x (and several previous versions) are SGML applications so you can extend the DTD with new entities.
實際上,每個主流瀏覽器都實現了一個特定于 HTML 的標簽湯吸食器,而不是一個真正的 SGML 解析器,所以你不能這樣做.這就是為什么 HTML 4 有一個 要避免的 SGML 功能列表 以及為什么 HTML 5 不是 SGML 應用程序.
In practise, every mainstream browser implements an HTML specific tag soup slurper instead of a real SGML parser so you can't do this. This is why HTML 4 has a list of SGML features to avoid and why HTML 5 isn't an SGML application.
這篇關于在 HTML 文檔中使用自定義實體的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!