Babel:显示当前语言的命令

Babel:显示当前语言的命令

我想知道 LaTeX 中是否有一些变量可以存储当前使用的语言。上下文是我有一些命令,我​​们称之为\printText打印当前语言文本的命令。但是,该命令仅支持少数几种语言。因此,我想打印一条错误消息,内容如下:<current language> is not supported by \printText

以下是 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}

我想将{current~ language}in更改\msg_error:nnn {printText} {unsupported-language} {current~ language}为存储当前使用的语言的变量(宏)。最好它还应与 一起使用\selectlanguage,以便它在调用命令时显示使用的活动语言。

编辑:\languagename在错误消息中不起作用,因为它显示为Language `\languagename ' not supported。如何确保\languagename在错误消息中正确显示?

答案1

我在错误消息中使用了\languagename而不是并且它起作用了。#1

\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}

在此处输入图片描述

相关内容