3

I am writing a latex class and would like to have a command that prints a sentence in different languages if babel is imported. If babel is not imported (detected) it will simply print the sentence in english. I expect to write the sentence myself, but somehow tell LaTeX that if it detects spanish, then print this sentence instead.

\documentclass{article}

% This is some command in the .sty file
\newcommand{\printText}{%
    This is a sentence
    % Esta es una frase % for spanish
    % Dette er en setning % for norwegian
    % To jest zdanie  % for polish
}

%% Babel may or may not be imported afterwards:
%\usepackage[spanish]{babel}

\begin{document}

\printText % If babel is not imported, this prints the english sentence.

\end{document}

Can I do this without importing babel in my .cls file? Also: if a language that is not supported for the command, for example italian, is used when calling \printText, then using the command should preferably print some error (and no textoutput on the PDF). Maybe something like:

\msg_new:nnn { printText } { unsupported-language }
  { Unsupported~ language~ `<current language>'~ for~ command. }
  { printText~ currently~ supports~ english,~ spanish,~ norwegian~ and~ polish. }
3
  • To check whether Babel is available, just check if one of its main macros is defined. And once you have detected that Babel is in fact available, you can simply use all of Babel's macros to your heart's content (and I am sure Babel provides support for querying the currently active language).
    – Raven
    Nov 4, 2022 at 7:00
  • Also, did you have a look at tex.stackexchange.com/q/78888/128658 yet?
    – Raven
    Nov 4, 2022 at 7:01
  • Have you looked at the translations package?
    – Joseph Wright
    Nov 4, 2022 at 11:28

1 Answer 1

5

You can make use of \languagename (it is also defined without babel):

\documentclass{article}

\ExplSyntaxOn
\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 }
     { \prop_item:Nn \g_vebjorn_text_prop {english} }
 }    
  
\ExplSyntaxOff

\usepackage[spanish]{babel}
\begin{document}

\printText

\end{document}
5
  • How can you print the error when the current language does not match any of the command languages? Do you need some kind of \str_case? If babel is not loaded then it should not return error.
    – Vebjorn
    Nov 4, 2022 at 11:12
  • 1
    sorry I overlooked the request for an error and used english as default. Replace the \prop_item:Nn \g_vebjorn_text_prop {english} by some error message. Nov 4, 2022 at 11:19
  • How can you use some commands inside the sentence, like \footnotesize? (See edit)
    – Vebjorn
    Nov 4, 2022 at 11:31
  • 1
    Don't expand your questions and ping me all the time. That is requesting personal support. Ask a new question if you have a new question. Beside this: did you try? Nov 4, 2022 at 11:41
  • I forgot to include the xcolor and the definition of softtext when running my MWE. When I did, everything worked fine.
    – Vebjorn
    Nov 4, 2022 at 11:50

You must log in to answer this question.

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