且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 HTML 中正确显示德语字符?

更新时间:2023-02-25 13:30:09

对于不幸的是,即使是大多数程序员也无法正确理解的事情,您似乎需要一些基本的解释.

It seems you need some basic explanations about something that unfortunately even most programmers don't understand properly.

像 HTML 页面这样的文件以字节序列的形式在 Internet 上保存和传输,但您希望它们显示为字符.为了将字节转换为字符,您需要一组称为字符编码的规则.不幸的是,历史上出现了许多不同的字符编码来处理不同的语言.它们中的大多数都基于美国 ASCII 编码,但是只要您有 ASCII 之外的字符例如德语变音,您需要非常小心使用哪种编码.

Files like your HTML page are saved and transmitted over the Internet as a sequence of bytes, but you want them displayed as characters. In order to translate bytes into characters, you need a set of rules called a character encoding. Unfortunately, there are many different character encodings that have historically emerged to handle different languages. Most of them are based on the American ASCII encoding, but as soon as you have characters outside of ASCII such as German umlauts, you need to be very careful about which encoding you use.

问题的根源在于,为了正确解码 HTML 文件,浏览器需要知道要使用哪种编码.您可以通过多种方式告诉它:

The source of your problem is that in order to correctly decode an HTML file, the browser needs to know which encoding to use. You can tell it so in several ways:

  • The "Content-Type" HTTP header
  • The HTML META tag
  • The XML encoding attribute if you use XHTML

因此,您需要选择一种编码,使用该编码保存 HTML 文件,并确保您至少以上面列出的一种方式声明该编码(如果您使用了超过一个该死的确保他们同意).至于使用什么编码,德国人通常使用ISO/IEC 8859-15,但 UTF-8 是一个很好的替代方案,可以处理任何类型的非 ASCII 字符同时.

So you need to pick one encoding, save the HTML file using that encoding, and make sure that you declare that encoding in at least one of the ways listed above (and if you use more than one make damn sure they agree). As for what encoding to use, Germans usually use ISO/IEC 8859-15, but UTF-8 is a good alternative that can handle any kind of non-ASCII characters at the same time.