Why does this language test fail?
For implementation reasons, the replacement text of \languagename
has its first character of category code 12; as far as \ifx
is concerned,
e
11n
11g
11l
11i
11s
11h
11
and
e
12n
11g
11l
11i
11s
11h
11
are different. Indeed, if you do
\documentclass{article}
\usepackage[english]{babel}
\begin{document}
\edef\suplang{\string english}
\ifx\languagename\suplang
Supported language: \languagename
\else
Not supported language: \languagename
\fi
\end{document}
then the output is
Do you need to do that business? No, of course not.
\usepackage{pdftexcmds} % to work with all engines
\makeatletter
\newcommand{\supported}[3]{%
\ifnum\pdf@strcmp{\languagename}{#1}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{#2}{#3}%
}
\makeatother
Then
\supported{suplang}
{Supported language: \languagename}
{Not supported language: \languagename}
will work.
\documentclass{article}
\usepackage[english]{babel}
\usepackage{pdftexcmds} % to work with all engines
\makeatletter
\newcommand{\supported}[3]{%
\ifnum\pdf@strcmp{\languagename}{#1}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{#2}{#3}%
}
\makeatother
\def\suplang{english}
\begin{document}
\supported{\suplang}
{Supported language: \languagename}
{Not supported language: \languagename}
\end{document}
Alternative definition with \else
and \fi
for the true and false branches:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{pdftexcmds} % to work with all engines
\makeatletter
\newcommand{\issupported}[1]{%
TT\fi\ifnum\pdf@strcmp{\languagename}{#1}=\z@
}
\makeatother
\def\suplang{english}
\begin{document}
\if\issupported\suplang
Supported language: \languagename
\else
Not supported language: \languagename
\fi
\end{document}
The answer of egreg already explains the reason for the unexpected result of the example in the question and also shows several methods to solve the issue.
Package iflang
wraps the internal stuff of the comparison with \languagename
in a simple macro \IfLanguageName
. The example of the question becomes:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{iflang}
\begin{document}
\IfLanguageName{english}{%
Supported language: \languagename
}{%
Not supported language: \languagename
}
\end{document}