Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

xhtml - Using custom entities in HTML documents

tl;dr

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>

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">
...

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)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In theory, it is possible. HTML 4.x (and several previous versions) are SGML applications so you can extend the DTD with new entities.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...