# Branching construct based on the (babel) language selected

I'd like to keep one of my presentations in German and English and have defined a corresponding boolean variable withifthen and a command to typeset either the German or the English text.

Is there an easy way to evaluate the global options (\documentclass[ngerman]{article}) one defines for e.g. babel?

\documentclass{minimal}

\usepackage{ifthen}
\newboolean{mothertongue}
\setboolean{mothertongue}{true}

\newcommand{\tr}[2]{\ifthenelse{\boolean{mothertongue}}{#1}{#2}}

\begin{document}

\tr{Deutsch}{English}

\end{document}


Based on egregs comment I updated my example, however I get only the English text.

\documentclass[ngerman]{scrartcl}
\usepackage{ifthen,babel}
\newcommand{\tr}[2]{\ifthenelse{\equal{\languagename}{ngerman}}{#1}{#2}}

\begin{document}

\tr{deutsch}{english}

\languagename

\end{document}

• Babel maintains the main language name in \bbl@main@language and the current name is available as \languagename. You can also look at tug.org/pracjourn/2007-1/gregorio – egreg Sep 6 '13 at 17:27
• Looks like an excellent idea, so far it's not working. Based on the ifthen documentation \equal should do thecorrect comparison. – Uwe Ziegenhagen Sep 6 '13 at 17:51
• Unfortunately, by technical reasons, \languagename has category code problems; look at the iflang package by Heiko Oberdiek that wasn't available when I wrote that paper. – egreg Sep 6 '13 at 18:00
• That's the most convenient solution, if you make it an answer I'd be happy to accept. – Uwe Ziegenhagen Sep 6 '13 at 18:04
• And don't use \iflanguage either, because it compares languages in the TeX sense, ie, hyphenation patterns. – Javier Bezos Sep 7 '13 at 14:45

A test \ifthenelse{\equal{\languagename}{ngerman}}{Deutsch}{English} can't work for several reasons: first, \languagename wouldn't get expanded; but even if we added the suitable number of \expandafter tokens, it wouldn't work again, because the expansion of \languagename consists of category code 12 characters.

Rather than doing complicated tricks, it's better to load one of the many packages by Heiko Oberdiek, in this case iflang; in its documentation the problem is explained.

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

\newcommand{\tr}{\IfLanguageName{ngerman}{Deutsch}{English}}

\begin{document}
\tr

\selectlanguage{english}
\tr

\end{document}


This prints

Deutsch
English

Use pdfTeX's \pdfstrcmp primitive for comparison:

\documentclass[ngerman]{scrartcl}% http://ctan.org/pkg/KOMA-script
\usepackage{babel}%  http://ctan.org/pkg/babel
\newcommand{\tr}[2]{\ifnum\pdfstrcmp{\languagename}{ngerman}=0 #1\else #2\fi}

\begin{document}

\tr{deutsch}{english}

\languagename

\end{document}