I've read this page: http://www.w3.org/TR/i18n-html-tech-lang/
in the meantime which explains the best practice.

In short:

 - HTML 5: use <html lang = "...">

 - XHTML 1.0: use <html lang="..." xml:lang="..." xmlns 
="http://www.w3.org/1999/xhtml";>

 - XHTML 1.1: use <html xml:lang="..." xmlns ="http://www.w3.org/1999/xhtml";>

The patch below implements this:

 - it uses the lang_code template variable (already defined by po) to
   put the language into page.tmpl

 - it accepts a "lang" in meta to set that variable.

 - it validates the language tag

 - If html5 is not set, it will also generate a meta content-language
   header.

Comments:

 - I don't really like the name "lang_code" used by po since the RFCs
   talk about language tag or language.  I didn't want to change the
   template variable but I used "lang" for meta instead.  If you
   prefer consistency, you can rename it to lang_code.  I'm not sure
   if lang_code could be renamed without breaking too much.

 - The regex checking for the language tag could be moved to a global
   function and then po's islanguagecode() replaced with it.  But this
   can wait for a future patch.

diff --git a/IkiWiki/Plugin/meta.pm b/IkiWiki/Plugin/meta.pm
index 220fff9..1dfbf91 100644
--- a/IkiWiki/Plugin/meta.pm
+++ b/IkiWiki/Plugin/meta.pm
@@ -153,6 +153,16 @@ sub preprocess (@) {
                        $pagestate{$page}{meta}{updated}=$time if defined $time;
                }
        }
+       elsif ($key eq 'lang') {
+               # Check if a valid language tag is specified according to
+               # BCP 47 at http://tools.ietf.org/html/bcp47
+               # We don't implement all of BCP 47 but we check for the most
+               # common variants of: language, extlang, script and region
+               if (!$value =~ 
(/^[[:alpha:]]{2,3}(-[[:alpha:]]{3})?(-[[:alpha:]]{4})?(-[[:alpha:]]{2}|-\d{3})?$/))
 {
+                       return "";
+               }
+               $pagestate{$page}{meta}{lang_code}=$value;
+       }
 
        if (! defined wantarray) {
                # avoid collecting duplicate data during scan pass
@@ -280,6 +290,11 @@ sub preprocess (@) {
                        encode_entities($key).
                        '" content="'.encode_entities($value).'" />';
        }
+       elsif ($key eq 'lang') {
+               push @{$metaheaders{$page}}, '<meta http-equiv="'.
+                       encode_entities('content-language').
+               '" content="'.encode_entities($value).'" />' if !$config{html5};
+       }
        elsif ($key eq 'name') {
                push @{$metaheaders{$page}}, scrub('<meta '.$key.'="'.
                        encode_entities($value).
@@ -317,6 +332,11 @@ sub pagetemplate (@) {
                        if exists $pagestate{$page}{meta}{$field} && 
$template->query(name => $field);
        }
 
+       foreach my $field (qw{lang_code}) {
+               $template->param($field => $pagestate{$page}{meta}{$field})
+                       if exists $pagestate{$page}{meta}{$field} && 
$template->query(name => $field);
+       }
+
        foreach my $field (qw{permalink}) {
                if (exists $pagestate{$page}{meta}{$field} && 
$template->query(name => $field)) {
                        eval q{use HTML::Entities};
diff --git a/doc/ikiwiki/directive/meta.mdwn b/doc/ikiwiki/directive/meta.mdwn
index f8494db..3e8d86f 100644
--- a/doc/ikiwiki/directive/meta.mdwn
+++ b/doc/ikiwiki/directive/meta.mdwn
@@ -59,6 +59,13 @@ Supported fields:
   Specifies a short description for the page. This will be put in
   the html header, and can also be displayed by eg, the [[map]] directive.
 
+* lang
+
+  Specifies a language tag (such as en, en-US, zh-Hant, zh-cmn-Hans-CN,
+  or es-419) indicating the language used on this page. This information
+  will be put in the html header. Page templates can access this
+  information via the `lang_code` variable.
+
 * permalink
 
   Specifies a permanent link to the page, if different than the page
diff --git a/templates/page.tmpl b/templates/page.tmpl
index 770ac23..742fd21 100644
--- a/templates/page.tmpl
+++ b/templates/page.tmpl
@@ -1,9 +1,17 @@
 <TMPL_IF HTML5><!DOCTYPE html>
+<TMPL_IF LANG_CODE>
+<html lang="<TMPL_VAR LANG_CODE>">
+<TMPL_ELSE>
 <html>
+</TMPL_IF>
 <TMPL_ELSE><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
+<TMPL_IF LANG_CODE>
+<html lang="<TMPL_VAR LANG_CODE>" xml:lang=<TMPL_VAR LANG_CODE>" 
xmlns="http://www.w3.org/1999/xhtml";>
+<TMPL_ELSE>
 <html xmlns="http://www.w3.org/1999/xhtml";>
 </TMPL_IF>
+</TMPL_IF>
 <head>
 <TMPL_IF DYNAMIC>
 <TMPL_IF FORCEBASEURL><base href="<TMPL_VAR FORCEBASEURL>" /><TMPL_ELSE>

-- 
Martin Michlmayr
http://www.cyrius.com/



-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to