XML technologies / XML / XML in a Nutshell / Internationalization / xml:lang
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
Because XML documents are written in Unicode, XML is particularly well suited to multilingual documents; for example, an Arabic comment can appear in Greek text, something not possible with another character set. In multilingual documents, it is useful to identify the language of a particular passage. For example, a spell checker that knows only English should not try to check a French quotation.
Any XML element can have an xml:lang attribute specifying the language of its contents. The preceding maxim looks like this, for example:
<maxim xml:lang="el">
σόC6;BF;C2; έB1;C5;C4;όν
γιγνώσκει
</maxim>This identifies it as Greek. The code used, el, derives from the Greek word for “Greek,” ελληνικα.
Language codes
The value of the language attribute xml:lang should be one of the two-letter language codes defined in ISO 639.2, Codes for the Representation of Names of Languages if such a code exists for the language in question.
For languages not listed in ISO-639, you can use a language identifier registered with IANA. There are currently about 20 such identifiers, including i-navajo, i-klingon and i-lux. All these identifiers begin with i-. For example:
<maxim xml:lang="i-klingon">Heghlu'meH QaQ jajvam</maxim> If the language you need is not in either list, you can create your own language tag. Starting it with either prefix x- or X- identifies it as a user-defined language code. For example, this journal’s title is written in J. R. R. Tolkien’s fictional language Quenya:
<journal xml:lang="x-quenya">Tyalië Tyelelliéva</journal>Subcodes
Sometimes knowing the language is not enough; you also need to know the region where it is spoken. For example, French vocabulary, spelling, and pronunciation differ slightly between France, Quebec, Belgium, and Switzerland. Although written with the same ideographic character set, Mandarin and Cantonese are completely different, mutually unintelligible Chinese dialects. The United States and Great Britain are jokingly described as “two countries separated by a common language.”
To handle these differences, any number of subcodes can follow the language code to identify the language more precisely. Hyphens separate the language code from subcodes and subcodes from each other. For an ISO-639 language code, the first subcode should be a two-letter country code defined by ISO-3166, Codes for the Representation of Names of Countries. This xml:lang attribute identifies Canadian French:
<p xml:lang="fr-CA">Marie vient pour le fin de semaine.</p>The language code is usually lowercase and the country code uppercase. This is a convention, however, not a requirement.
ATTLIST declarations for xml:lang
Although the XML 1.0 specification defines the xml:lang attribute, you must still declare it in DTDs for valid documents. For example, this declaration defines the element maxim, which has already appeared several times in this section:
<!ELEMENT maxim (#PCDATA)>
<!ATTLIST maxim xml:lang NMTOKEN #IMPLIED>We used the type NMTOKEN because all permitted language codes are well-formed XML name tokens.
You can declare the attribute xml:lang however you prefer. For example, if you want it always present on the element maxim you could declare it #REQUIRED:
<!ATTLIST maxim xml:lang NMTOKEN #REQUIRED> Or, to allow only French and English text in your documents, you could declare it as an enumerated type with English as the default:
<!ATTLIST maxim xml:lang (en | fr) 'en'> Unless you use an enumerated type, the parser does not check whether the attribute value follows the rules described here. You are responsible for using the correct language codes and subcodes.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de