I’ve actually just been looking into this again, and it looks like the issue is more complicated than just the http header:
http://www.w3.org/International/tutorials/tutorial-char-enc/#Slide0250. From this I gather that, for XHTML documents (that is, with an XHTML doctype):
- Specifying character encoding in the http header is not necessary (but you can)
- For XHTML served as an XML MIME type (such as application/xhtml+xml), the XML declaration *should* be used , and the meta http-equiv tag *should not* be used
- For XHTML served as text/html, the XML declaration is not necessary (and it will have undesirable effects in some user agents, i.e. break IE, so I think it’s a bad idea), and the meta http-equiv tag *should* be used.
Thus, (I think!) that to adhere to the W3C recommendations (and to avoid problems with IE), the XML declaration/prologue should be used when, and only when, the document is served as XML, and the meta http-equiv tag should be used when, and only when, the document is served as text/html. Thus I have written two short snippets that I now include in templates. Note that they both use the custom content type of application/xhtml+xml set in the manager for the documents to be served as application/xhtml+xml to compliant browsers. The first to be used at the very top before the DOCTYPE:
<?php
$docIsXHTML = ($modx->documentObject['contentType'] == "application/xhtml+xml");
$userCanHandleXHTML = (stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml'));
$charset = $modx->config['modx_charset'];
$output = "";
if ($docIsXHTML && $userCanHandleXHTML) {
$output = '<?xml version="1.0" encoding="'.$charset.'"?>';
}
return $output;
?>
The second is used at the very top of the head section:
<?php
$docIsXHTML = ($modx->documentObject['contentType'] == "application/xhtml+xml");
$userCanHandleXHTML = (stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml'));
$charset = $modx->config['modx_charset'];
$output = "";
if (($docIsXHTML && !$userCanHandleXHTML) || !$docIsXHTML) {
$output .= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />';
}
return $output;
?>
*However* this is still not the end of the story! To specify the language of the page the situation is more complex:
http://www.w3.org/TR/i18n-html-tech-lang/#ri20040429.092928424. I think that if the doc is served as an XML type, then only the xml:lang attribute should be used. However, if it is served as text/html, then both the xml:lang and lang attributes should be used. Thus I have quickly written a 3rd snippet, to be used in the html tag (or any other tag that you want to specify the language of). The parameter "lang" is the official code of the language (e.g. "en" for English).
<?php
// Parameters to snippet call
$lang = isset($lang) ? $lang : "";
// Determine user capabilities and page type
$userCanHandleXHTML = (stristr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml'));
$docIsXHTML = ($modx->documentObject['contentType'] == "application/xhtml+xml");
// Always include XML language declaration
$output = 'xml:lang="'.$lang.'" ';
// Only include html language attribute if the document is not served as XHTML
if (!$docIsXHTML || !$userCanHandleXHTML) {
$output = $output.'lang="'.$lang.'"';
}
return $output;
?>
What do people think? Have I got through the W3C recommendations correctly? Should these maybe be placed in the extras for the plugin? By the way... I just had a peek at the source of the MODx home page and I don’t think it specifies the language at all... ;-)
Michal.
EDIT: Corrected mistakes in snippets