[Tex/LaTex] Multilingual text with if statement

conditionalslanguages

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?

Best Answer

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.