13

A problem that sometimes arises with constructing packages, is enabling translations.

Currently I maintain two packages foo-nl.sty (Dutch version) and foo-en.sty (English version). A problem with this approach is of course that one needs to synchronize the two packages, and there is no mechanism to check if this is the case.

What other options are there to provide translations. It would be nice to automatically set the language as well, based on the \usepackage[dutch]{babel} command in the main document.

3
9

The translations package was developed for exactly this purpose. In the package translations are provided for keywords for each language with

\DeclareTranslation{<language>}{<keyword>}{<translation>}

One should probably also add

\DeclareTranslationFallback{<keyword>}{<translation>}

which is used if a babel language is chosen for which no translation is provided.

Translations declared this way can be used with

\GetTranslation{<keyword>}

This command is expandable so that it also works in section titles that are used as hyperref bookmarks, for example.

Here is a short example package:

\documentclass{article}
\usepackage[english,dutch]{babel}

\usepackage{filecontents}
\begin{filecontents}{mypackage.sty}
\ProvidesPackage{mypackage}
\RequirePackage{translations}

% a command that's translated according to the provided translations:
\newcommand*\mypackagetitle{\section*{\GetTranslation{mypackage-title}}}

% the translations:
% the fallback is used for languages where no specific translation is provided
\DeclareTranslationFallback {mypackage-title}{My title}
\DeclareTranslation{English}{mypackage-title}{My title}
\DeclareTranslation{Dutch}  {mypackage-title}{Mijn titel}

\end{filecontents}

\usepackage{mypackage}

\begin{document}

\mypackagetitle

\edef\foo{\GetTranslation{mypackage-title}}
\show\foo
% > \foo=macro:
% ->Mijn titel.
% l.27 \show\foo

\selectlanguage{english}

\mypackagetitle

\edef\foo{\GetTranslation{mypackage-title}}
\show\foo
% > \foo=macro:
% ->My title.
% l.37 \show\foo

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.