XML technologies / DocBook / The DocBook XSL stylesheets / Changing localization / Customizing language files

(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)

Language files provide the most flexible way to change or add localized text.

Structure of language files

At the time of writing, the DocBook XSL stylesheets support over 60 languages stored in the directory common. Each filename consists of a language code and the extension .xml. The stylesheets read these files during transformation. Each language file has a so-called context, shown in the following table. The German file common/de.xml is shown in greatly abbreviated form. Further entries may be added over time.

Table: Allowed context names in a language file

Context name

Meaning

title

Contains title formatting instructions based on the *.autolabel parameter. For example, if the parameter chapter.autolabel has the value 0 (zero), the context title-unnumbered is selected and chapter numbering is disabled. If the parameter instead has the value 1, a or another value, the context title-numbered is selected. The same applies to appendices, book parts, and other elements. The context title is used for all other block elements with a title, or elements for which the preceding contexts are undefined.

title-unnumbered

title-numbered

subtitle

Text for subtitles, where applicable.

xref

Used to create cross-references. For details, see Customizing with a gentext template.

xref-number

xref-number-and-title

authorgroup

Text separating authors in an authorgroup.

glossary

Translations for the elements glosssee and glossseealso.

msgset

Translations for the elements msglevel, msgorig and msgaud.

termdef

Text before and after the element termdef.

datetime

Date format for the language, plus full and abbreviated month and day names.

datetime-full

datetime-abbrev

htmlhelp

Language code for HTMLHelp.

index

Delimiters for index entries.

iso690

Replacement text for bibliographies according to ISO 690 (see also Output compliant with ISO 690 and ISO 690-2).

The following example shows a greatly abbreviated version.

Example: Excerpt from the German language file common/de.xml

<l:l10n language="de" english-language-name="German" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0">
  <l:context name="title">
    <l:template name="chapter" text="%t"/>
  </l:context>
  <l:context name="subtitle">
    <l:template name="chapter" text="%s"/>
  </l:context>
  <l:context name="xref">
    <l:template name="chapter" text="Kapitel %n. %t"/>
  </l:context>
  <l:context name="datetime">
    <l:template name="format" text="d.m.Y"/>
  </l:context>
  <l:context name="datetime-full">
    <l:template name="January" text="Januar"/>
  </l:context>
  <l:context name="datetime-abbrev">
    <l:template name="Jan" text="Jan"/>
  </l:context>
  <l:context name="htmlhelp">
    <l:template name="langcode" text="0x0407 German (GERMANY)"/>
  </l:context>
  <l:context name="index">
    <l:template name="term-separator" text=", " lang="en"/>
  </l:context>
  <l:context name="iso690">
    <l:template name="lastfirst.sep" text=", " lang="en"/>
  </l:context> 
</l:l10n>

Using these data, the function gentext can return the translated string for a keyword in each supported language. Each language file contains several elements, as shown above. An entry is identified by a key in the attribute key or name. The translated text is in the attribute text. Sometimes the stylesheet needs information about the order of components, such as numbering and title. A percent sign followed by a letter serves as a placeholder (see the following table, “Placeholders in language files”).  

Table: Placeholders in language files

Placeholder

Meaning

%t

replaced by the title

%s

produces the subtitle

%n

produces numbering

%p

inserts a page number (useful only for XSL-FO )

The following string contains placeholders for the chapter number (%n) and title (%t):

Kapitel %n. &quot;%t&quot;

Substitution produces the following for this chapter:

Kapitel 15. "Die DocBook-XSL-Stylesheets"

 

Customizing localized text

Override the default translations if they seem inappropriate, if you want to change the order within a format, or if you need a revised style. The following example changes the default chapter text to “Thema” (“Topic”), including cross-references:

Example: Modified chapter localization

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="..."/>
  <xsl:param name="local.l10n.xml" select="document('')"/> 
  <l:i18n xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0"> 
   <l:l10n english-language-name="German" language="de" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0">
     <l:gentext key="chapter" text="Thema"/>
     <l:context name="title-numbered">
       <l:template name="chapter" text="Thema %n %t"/>
     </l:context>
     <l:context name="xref">
       <l:template name="chapter" text="%n. Thema, %t"/>
     </l:context>
     <!-- ... -->
   </l:l10n>
  </l:i18n>
</xsl:stylesheet>

Line 4: Uses the empty document() function to reference the current stylesheet.
Line 5: Root element of the language files, allowing only l:l10n elements (l10n abbreviates localisation: first and last letters, with 10 for the number of omitted letters). It is bound to the namespace "http://docbook.sourceforge.net/xmlns/l10n/1.0".
Line 6: Each language entry begins with an l:l10n element.
Line 9: Numbered components (value title-numbered) contain text, indicated by %t, and the corresponding number (%n).
Line 12: Cross-references (value xref) contain the referenced element’s name and the corresponding reference text.

The stylesheet above keeps parameters and language data together in the XSLT file. In l:10n, enter only the entries to change, keeping the total small. Calling the document() function with an empty argument gives the parameter local.l10n.xml the node-set of the current stylesheet, allowing the DocBook stylesheets to select the appropriate translations.

This method suits a small number of entries; otherwise, the stylesheet becomes cluttered. For many entries, move the language customizations to a separate file, as described next.

 

Adding your own entries

For extensive translation changes or changes in several languages, create your own language file.

For example, to insert a localized term for each othercredit element with the attribute class and the value translator, in this case “Übersetzer” (“translator”), proceed as follows:

  1. Create the skeleton of your language file and include every language you want to support. Replace the ellipses with your customized or new terms:

<l:i18n xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0"> 
  <l:l10n language="en" english-language-name="English"> 
   ...  
  </l:l10n> 
  <l:l10n language="de" english-language-name="German"> 
   ...
  </l:l10n> 
</l:i18n>

  1. Invent a new keyword that does not already exist in common/en.xml. Insert the following line in the English part (line 3):

<l:gentext key="Translator" text="Translator"/> 

In the German part (line 6), enter:

<l:gentext key="Translator" text="Übersetzer"/> 

  1. Create a customization file as shown in A customization file for the DocBook stylesheets. Set the URI or path to your language file correctly and insert the following line:

<xsl:param name="local.l10n.xml" select="document('myl10n.xml')"/> 

  1. Call gentext in your templates to obtain a translated term. This example requires a translation for othercredit, so write:

<xsl:template select="othercredit[@class='translator']"> 
  <xsl:call-template name="gentext"> 
    <xsl:with-param name="key">Translator</xsl:with-param> 
  </xsl:call-template> 
  <!-- ... --> 
</xsl:template>

This method keeps language-dependent text out of your templates and makes adding further languages easy when needed.

  

back to top