在 HTML 文档中使用自定义实体

2022-01-11 00:00:00 localization html html-entities xhtml

我想了解是否可以(以及支持的程度)在 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.

相关文章