如何在 HTML 中正确显示德语字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/423693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I properly display German characters in HTML?
提问by venkatachalam
My pages contain German characters and I have typed the text in between the HTML tag, but the browser views some characters differently. Do I need to include anything in HTML to properly display German characters?
我的页面包含德语字符,并且我在 HTML 标记之间输入了文本,但浏览器以不同的方式查看某些字符。我是否需要在 HTML 中包含任何内容才能正确显示德语字符?
<label> ausgefüllt </label>
回答by Michael Borgwardt
It seems you need some basic explanations about something that unfortunately even most programmers don't understand properly.
不幸的是,即使大多数程序员都无法正确理解,似乎您需要一些基本的解释。
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 ASCIIencoding, 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 页面这样的文件以字节序列的形式在 Internet 上保存和传输,但您希望它们显示为字符。为了将字节转换为字符,您需要一组称为字符编码的规则。不幸的是,历史上出现了许多不同的字符编码来处理不同的语言。它们中的大多数都基于美国ASCII编码,但是一旦您有 ASCII 之外的字符(例如德语变音符号),您就需要非常小心使用哪种编码。
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:
问题的根源在于,为了正确解码 HTML 文件,浏览器需要知道要使用哪种编码。您可以通过以下几种方式告诉它:
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-8is a good alternative that can handle any kind of non-ASCII characters at the same time.
因此,您需要选择一种编码,使用该编码保存 HTML 文件,并确保至少以上面列出的一种方式声明该编码(如果您使用多种方式,请确保他们同意)。至于使用什么编码,德国人通常使用ISO/IEC 8859-15,但UTF-8是一个很好的替代方案,可以同时处理任何类型的非 ASCII 字符。
回答by Karsten
UTF-8 is your friend.
UTF-8 是你的朋友。
Try
尝试
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
and check which encoding your webserver sends in the header.
并检查您的网络服务器在标头中发送的编码。
If you use PHP, you can send your own headers in this way (you have to put this before any other output):
如果你使用 PHP,你可以用这种方式发送你自己的头文件(你必须把它放在任何其他输出之前):
<?php header('Content-Type: text/html; charset=utf-8'); ?>
Also doublecheck that you saved your document in UTF-8.
还要仔细检查您是否以 UTF-8 格式保存了您的文档。
回答by tulasidhar
Try the solution in blog post German characters encoding issue (2012-05-10):
尝试博客文章德语字符编码问题(2012-05-10) 中的解决方案:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
回答by G?ran Lilja
回答by Rowland Shaw
Sounds like a character encoding issue, in that the file is saved as a different character encoding to what the webserver is saying it is.
听起来像是字符编码问题,因为文件被保存为与网络服务器所说的不同的字符编码。
回答by Kieran
Declare <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
宣布 <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
and when saving the file, for example in notepad, choose the save as to be UTF-8 and not just .txt.
保存文件时,例如在记事本中,选择另存为 UTF-8 而不仅仅是 .txt。
This should render the characters ok.
这应该使字符正常。
回答by Hriju
you may try utf8_encode() or utf8_decode() functions.Check if any of these works.
您可以尝试 utf8_encode() 或 utf8_decode() 函数。检查这些是否有效。
For example <?php echo utf8_encode('ausgefüllt'); ?>
例如 <?php echo utf8_encode('ausgefüllt'); ?>
Hope it will work.
希望它会起作用。
回答by myplacedk
I don't like the use of HTML entities (like %uuml;), they are only needed when there is something wrong with your characterset.
我不喜欢使用 HTML 实体(如 %uuml;),只有在您的字符集出现问题时才需要它们。
In short:
简而言之:
The RIGHT way is to fix your characterset.
正确的方法是修复您的字符集。
The EASY way is to just use entities. You may not ever see any problems with this.
简单的方法是只使用实体。您可能永远不会看到任何问题。
Tracking down characterset error can be very difficult. If you give us an URL where we can see the problem, we can probably give you a good hint where to look.
追踪字符集错误可能非常困难。如果您给我们一个可以查看问题的 URL,我们可能会给您一个很好的提示。