Proper use of PHP: header('Content-language: …');

不打扰是莪最后的温柔 提交于 2019-12-10 18:13:08

问题


aside the obvious fashion of setting the language of a page:

<meta name="language" content="de"><html lang="de">

I recently found an amazing aspect typical to only the programming language of PHP that could enables developers to set the language at the very top of PHP files:

<?php  

 /* Set and pre-define the language in the header;
  * Eliminating guesswork for the Header language.
  */

  header('Content-language: de');

?>

Two questions arise eveidentaly to the PHP programmer:

Main Question: When should one set the language in a PHP header?

SideQ1: Big websites don't bother using it: why don't they?

SideQ2: Do search engines listen to this and if so, what implications does this PHP header have?


回答1:


Shorter answer: Don't bother, cause browsers typically ignore it.

Longer answer: The W3C recommends using the lang attribute in HTML over the Content-Language header in HTTP:

http://www.w3.org/International/geo/html-tech/tech-lang.html#ri20040808.110827800

Rationale: User agents tend to ignore the header or implement it inconsistently.

Plus, the lang attribute is more flexible. If you have some content in one language and other in another, you can specify that precisely, like so:

<body> 
 <div lang="en" xml:lang="en">
  <h1>Welcome!</h1> 
  <p>Lots of text in English...</p>
 </div>
 <div lang="fr" xml:lang="fr">
  <h1>Bienvenue !</h1> 
  <p>Beaucoup de texte en français...</p>
 </div>
</body> 

Taken from: http://www.w3.org/International/geo/html-tech/tech-lang.html#ri20040728.121403792

I don't know about search engines.




回答2:


You can use this header for informational purposes. But it's primarily meant for content negotiation.

Each http URL can transparently refer to different resources. Depending on a clients Accept: and Accept-Language: header, the server can send a specific variant of a resource. And that's when it is advisable that such descriptive headers are included.

If it's used in that fashion, it will be accompanied by a Vary: * or Vary: Accept-Language header. Otherwise it's really just informational. (It never catched on to be widely used all by itself.)



来源:https://stackoverflow.com/questions/5423989/proper-use-of-php-headercontent-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!