I would like to know if there is some variable in LaTeX that stores the currently used language. The context is that I have some command, lets call it \printText
which prints text in the current language. However, the command only has support for a few languages. Thus I would like to print an error message saying something like: <current language> is not supported by \printText
.
Here is a MWE:
\documentclass{article}
\ExplSyntaxOn
\msg_new:nnnn { printText } { unsupported-language }
{ Language~`#1'~ not~ supported~ for~ ` \token_to_str:N \printText '. }
{ ` \token_to_str:N \printText '~ currently~ supports:~ english,~ spanish,~ norwegian~ and~ polish. }
\prop_new:N\g_vebjorn_text_prop
\prop_gset_from_keyval:Nn \g_vebjorn_text_prop
{
english = { This~ is~ a~ sentence },
spanish = { Esta~es~una~frase },
polish = { To~jest~zdanie }
}
\newcommand{\printText}
{
\prop_get:NVNTF \g_vebjorn_text_prop \languagename \l_tmpa_tl
{ \l_tmpa_tl }
{ \msg_error:nnn {printText} {unsupported-language} {current~ language} }
}
\ExplSyntaxOff
% Babel may or may not be imported afterwards:
\usepackage[italian,polish]{babel}
\begin{document}
\selectlanguage{polish}
Hello.
\printText % If babel is not imported, this prints the english sentence.
\selectlanguage{italian}
Hello.
\printText
\end{document}
I would like to change the {current~ language}
in \msg_error:nnn {printText} {unsupported-language} {current~ language}
to some variable (macro) that stores the current language used. Preferably, it should also work with \selectlanguage
, so that it will display the active language used when calling the command.
Edit: \languagename
does not work inside the error message, as it displays as Language `\languagename ' not supported
. How can I make sure that \languagename
displays correctly inside the error message?
\languagename
.\addtoextras{polish}{\dev\printText{whatever}}
then babel will switch the definition when changing language, and any package or document author can add more language variants without needing to touch your code. See how fixed texts like "table of contents" are handled\contentsname
does not check the current language, babel redfines\contentsname
when switching languages