5

I'm working on a document in LaTeX and I would like to have multiple language versions. I could just copy everything and translate, but this would lead to multiple versions that I would have to update individually every single time.

What I want is that I specify which language I want to compile (or perhaps it always compiles all the languages, so every time I run it saves for instance an english, dutch and german version). And then it only prints that language. I was thinking of something along the lines of:

\languageversion{english} %this could be changed for language specification

\begin{document}
...
\dutch{some dutch text here}
\english{some english text here}
\german{some german text here}

So when I select english, only the english part would be printed. This way, if I would change the layout I don't have to do this for multiple versions, but only in one.

Is this possible?

2

2 Answers 2

3

Here's a way, but I don't think this can really be useful for long texts.

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

\makeatletter
\newcommand{\declarelanguage}{\@dblarg\@declarelanguage}
\def\@declarelanguage[#1]#2{%
  \long\@namedef{#2}{%
    \edef\@temp{\string#1}%
    \ifx\@temp\languagename
      \expandafter\@firstofone
    \else
      \expandafter\@gobble
   \fi}%
}
\makeatother

\declarelanguage{dutch}
\declarelanguage[ngerman]{german}
\declarelanguage{english}

\begin{document}

English

\dutch{some dutch text here}
\english{some english text here}
\german{some german text here}

\bigskip
\hrule
\bigskip

German

\selectlanguage{ngerman}

\dutch{some dutch text here}
\english{some english text here}
\german{some german text here}

\bigskip
\hrule
\bigskip

Dutch

\selectlanguage{dutch}

\dutch{some dutch text here}
\english{some english text here}
\german{some german text here}

\end{document}

This will use the active language (according to babel rules). The \declarelanguage has an optional argument: a string that matches the babel language name. Using this it's immaterial what name you use; for example you could say

\declarelanguage[english]{EN}

and use \EN{English text} for inserting the English text. Take your pick; for German it's necessary.

Note: there's a strange babel feature: the language string maintained by \languagename has its first character of category code 12, that's the reason for the mysterious \edef\@tempa{\string#1}, which make your given string agree with babel's opinion on the matter.

1
  • Thanks guys, you pointed me in the right direction! (I had searched before, without any results) Eventually I used the procedure from latex-community.org/forum/… Which is not perfect, but works for me! Cheers! Commented Jun 17, 2012 at 9:22
0

I have found two ways to deal with the problem. Note: the document always starts with German.


First solution, language change contained

The first solution - rather trivial and inspired from user1460812's comment - allows to alter the language within a controlled container without influencing the language of the document. An example:

\newif\ifeng
\newif\ifita

\engtrue
%\itatrue

\documentclass{article}
\usepackage[main=ngerman, italian, english]{babel}

\newcommand{\italian}[1]{%
    \ifeng\begin{otherlanguage}{italian}#1\end{otherlanguage}\fi}
    
\newcommand{\english}[1]{%
    \ifeng\begin{otherlanguage}{english}#1\end{otherlanguage}\fi}

\begin{document}
    
    \par{\bf{The document starts with: \prefacename, \alsoname.}}

    \italian{Buongiorno!}
    \english{Hi!}

    \italian{In italiano si dice: \prefacename, \alsoname.}
    \english{In English it says: \prefacename, \alsoname.}

    \par{\bf{The document ends with: \prefacename, \alsoname.}}
    
\end{document}

Which prints: Result of the code above


Second solution, language change from a point on

The second solution is borrowed from egreg's answer and changes the language of the whole document (I include it here for a practical comparison between solutions):

\documentclass{article}
\usepackage[main=ngerman, italian,ngerman,english]{babel}

\makeatletter
\newcommand{\declarelanguage}{\@dblarg\@declarelanguage}
\def\@declarelanguage[#1]#2{%
    \long\@namedef{#2}{%
        \edef\@temp{\string#1}%
        \ifx\@temp\languagename
        \expandafter\@firstofone
        \else
        \expandafter\@gobble
        \fi}%
}
\makeatother

\declarelanguage[ngerman]{german}
\declarelanguage{english}
\declarelanguage{italian}

\begin{document}

\selectlanguage{english}

    \par{\bf{The document starts with: \prefacename, \alsoname.}}

    \italian{Buongiorno!}
    \english{Hi!}

    \italian{In italiano si dice: \prefacename, \alsoname.}
    \english{In English it says: \prefacename, \alsoname.}

    \par{\bf{The document ends with: \prefacename, \alsoname.}}

\end{document}

Which prints:

Result of the code above

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .