# Elegant way to define a package with translations

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.

• it's plain tex, but michael doob's "gentle introduction to tex" (gentle.tex, included in a full tex live distribution) contains an \if... facility to choose canadian or u.s. spelling from choices included within the file. might get some ideas there. – barbara beeton May 8 '14 at 14:21
• beamer also introduced translator.sty package which can be used for such things. Some examples here: tex.stackexchange.com/a/38400/1952, tex.stackexchange.com/a/62205/1952 – Ignasi May 8 '14 at 14:28
• – cgnieder May 8 '14 at 14:38

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>}


\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}