0

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?

3
  • 1
    IIRC the current language is stored in \languagename.
    – campa
    Nov 4, 2022 at 12:28
  • Unfortuantley, it produces: `` Language `\languagename ' not supported ``
    – Vebjorn
    Nov 4, 2022 at 12:40
  • actually you are probably better not to test this but rather use \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 Nov 4, 2022 at 12:54

1 Answer 1

0

I used \languagename instead of #1 in the error message and this worked.

\documentclass{article}

\ExplSyntaxOn

\msg_new:nnnn { printText } { unsupported-language }
  { Language~`\languagename'~ 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:nn {printText} {unsupported-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}

enter image description here

1
  • 2
    you can also expand the argument by using \msg_error:nnx. Nov 4, 2022 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .