multilang with babel problem

I've loaded babel in my .sty file like this:

\RequirePackage[ukrainian,russian,english]{babel}


Then at .tex or at the same .sty file I am trying to:

\selectlanguage(X)
\languagename
\iflanguage{english}{eng}{}
\iflanguage{russian}{rus}{}
\iflanguage{ukrainian}{ukr}{}


And the result is:

X=>output
russian => rus (\languagename says russian)  OK!
ukrainian => rus (!!!) (languagename says ukrainian) WRONG!
english => eng (\languagename says english) OK!


What a magic it is? Note: everything else like word "Content", "Chapter" and so on are correctly translates to specified language.

This is a bug in babel, which appears when two cyrillic languages are chosen. I wrote about it in an article published in number 9 of ArsTeXnica, the journal of GuIT, the Italian TeX users group.

You can get around the bug by correcting the behaviour of babel:

\documentclass{article}
\usepackage[T2A,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ukrainian,russian,english]{babel}

\makeatletter
\newcommand{\@cyrillictext}{%
\fontencoding\cyrillicencoding
\selectfont
\let\encodingdefault\cyrillicencoding
}
\DeclareRobustCommand{\cyrillictext}{%
\@cyrillictext}
\let\cyr\cyrillictext
\DeclareRobustCommand{\Bulgarian}{%
\@cyrillictext
\foreign@language{bulgarian}}
\let\Bul\Bulgarian \let\Bg\Bulgarian
\DeclareRobustCommand{\Russian}{%
\@cyrillictext
\foreign@language{russian}}
\let\Rus\Russian
\DeclareRobustCommand{\Ukrainian}{%
\@cyrillictext
\foreign@language{ukrainian}}
\let\Ukr\Ukrainian
\AtBeginDocument{
\@ifundefined{defaultcyrlanguage}
{\let\defaultcyrlanguage\bbl@main@language}
{}
}
\renewcommand{\textcyrillic}[2][\defaultcyrlanguage]{%
\expandafter\foreignlanguage\expandafter{#1}{#2}}
\makeatother

\begin{document}
\selectlanguage{english}
\languagename --\the\language --%
\iflanguage{english}{eng}{}%
\iflanguage{russian}{rus}{}%
\iflanguage{ukrainian}{ukr}{}

\selectlanguage{ukrainian}
\languagename --\the\language --%
\iflanguage{english}{eng}{}%
\iflanguage{russian}{rus}{}%
\iflanguage{ukrainian}{ukr}{}%

\selectlanguage{russian}
\languagename --\the\language --%
\iflanguage{english}{eng}{}%
\iflanguage{russian}{rus}{}%
\iflanguage{ukrainian}{ukr}{}%

\end{document}


With this code the declaration \Russian and \Ukrainian don't change the fixed words (they behave like the otherlanguage* environment); if you prefer to have the (IMO wrong) behavior of babel, change the occurrences of \foreign@language into \select@language.

The problem is that at loading time, each language defines \cyrillictext to use that language; so the last language loaded wins. When \selectlanguage or a similar command to select a cyrillic language is issued, \cyrillictext is executed as part of \extras<language>; so the language name is set correctly, but the language number is set instead to the cyrillic language last loaded in with babel, in the example case to Russian.

• Great article. If you I have found more bugs, please report them for them to get fixed. – Javier Bezos Nov 1 '12 at 7:41
• @JavierBezos I've sent a bug report at LaTeX Project – egreg Nov 1 '12 at 19:19