The definition seems to be "almost" correct, in that it gives almost the expected result, apart from spurious spaces:
\newcommand{\mynewcommand}[3]{%
\newcommand{#1}{%
\iflanguage{english}{#2}{%
\iflanguage{french}{#3}{}
}%
}%
}
However this is error prone if one wanted to augment the supported languages; with xparse
it's easier:
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mynewcommand}{m m m}
{
\cs_new:Npn #1
{
\str_case:onF { \languagename }
{
{english}{#2}
{french}{#3}
}
{``No~def~of~\texttt{\token_to_str:N #1}~for~\languagename''}
}
}
\ExplSyntaxOff
If other languages were desired it would be sufficient to add an argument to \mynewcommand
and the suitable line along the same scheme.
An alternative way might be to define different commands:
\makeatletter
\newcommand{\newlanguagecommand}[1]{%
\newcommand#1{%
\@ifundefined{\string#1\languagename}
{``No def of \texttt{\string#1} for \languagename''}
{\@nameuse{\string#1\languagename}}%
}%
}
\newcommand{\addtolanguagecommand}[3]{%
\@namedef{\string#1#2}{#3}}
\makeatother
\newlanguagecommand{\uno}
\addtolanguagecommand{\uno}{english}{one}
\addtolanguagecommand{\uno}{french}{une}
Unknown languages will result in "No def of \command
for language"
With \newlanguagecommand{\uno}
one defines the command so that its expansion is \\uno<languagename>
; for example, when the language is French, writing \uno
will use \\unofrench
which is defined by \addtolanguagecommand{\uno}{french}{une}
to expand to "une".
This should give no problems also in moving arguments as long as the replacement text doesn't contain fragile commands (text is OK).
A revamped expl3
implementation. The translations are stored in a property list and used according to the current language.
The defined command can receive an optional argument, which should be a valid language name, to force using the corresponding translation independent of the current language.
\documentclass{article}
\usepackage[french,ngerman,english]{babel}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newlanguagecommand}{mm}
{% #1 = command to define
% #2 = key-value list
\prop_new:c { \__roepo_nlc_strip:N #1 }
\prop_gset_from_keyval:cn { \__roepo_nlc_strip:N #1 } { #2 }
\NewDocumentCommand{#1}{o}
{
\IfNoValueTF { ##1 }
{
\roepo_nlc:NV #1 \languagename
}
{
\roepo_nlc:Nn #1 { ##1 }
}
}
}
\cs_new:Nn \roepo_nlc:Nn
{
\prop_if_in:cnTF { \__roepo_nlc_strip:N #1 } { #2 }
{
\prop_item:cn { \__roepo_nlc_strip:N #1 } { #2 }
}
{
UNSET~TRANSLATION
}
}
\cs_generate_variant:Nn \roepo_nlc:Nn { NV }
% syntactic sugar
\cs_new:Nn \__roepo_nlc_strip:N { g_roepo_nlc_ \cs_to_str:N #1 _prop }
\ExplSyntaxOff
\newlanguagecommand{\uno}{
english=one,
ngerman=eins,
french=un
}
\newlanguagecommand{\duck}{
english=duck,
ngerman=Ente,
}
\begin{document}
English: \uno{} and \duck{} (\duck[ngerman])
\selectlanguage{ngerman}
Deutsch: \uno{} und \duck{} (\duck[english])
\selectlanguage{french}
Français: \uno{} et \duck{} (\duck[english])
\end{document}
As it says in the cleveref manual, you need to hook your \crefformat
commands into babel's language-switching mechanism. This is explained in detail in the babel documentation, but basically you need to add your \crefformat
definitions to the appropriate \extras<language>
macro.
Cleveref provides a convenience macro \cref@addlanguagedefs
to accomplish this (which has the advantage of also working for the polyglossia package). Adding the following to your preamble will do the trick:
\makeatletter
\cref@addlanguagedefs{english}{%
\crefformat{figure}{my fancy figure~#2#1#3}}
\cref@addlanguagedefs{ngerman}{%
\crefformat{figure}{meine tolle Abbildung~#2#1#3}}
\makeatother
There's one more wrinkle. This hooks the format changes into babel's language-switching, but it doesn't actually set the formats until you switch language. (Arguably cleveref should do this for you - maybe I'll fix this in a future version.) Perhaps the easiest way to set the initial formats is to add a \selectlanguage{ngerman}
command at the very beginning of your document.
Also, you should carefully read the instructions in the cleveref manual about where to pass language options. Basically, you should be passing language options to \documentclass
, not to \usepackage{babel}
as in your MWE. Otherwise cleveref won't do what you want.
(Off topic: I'm not sure why you're putting a space before the ~
in your \crefformat
definitions. ~
inserts a non-breaking space, so you almost certainly don't want the additional space character before it. I've fixed this in the \crefformat
definitions given above.)
Here's a full MWE demonstrating all of the above:
\documentclass[english,ngerman]{article}
\usepackage[demo]{graphicx}
\usepackage{babel}
\usepackage{hyperref}
\usepackage{cleveref}
\makeatletter
\cref@addlanguagedefs{english}{%
\crefformat{figure}{my fancy figure~#2#1#3}}
\cref@addlanguagedefs{ngerman}{%
\crefformat{figure}{meine tolle Abbildung~#2#1#3}}
\makeatother
\begin{document}
\selectlanguage{ngerman}
\cref{fig:one}
\begin{figure}[h]
\includegraphics[width=\textwidth]{example-image-a}
\caption{I am a figure\label{fig:one}}
\end{figure}
\selectlanguage{english}
\cref{fig:one}
\selectlanguage{ngerman}
\cref{fig:one}
\end{document}
Note that, since you're only changing the name used for figures, you could also accomplish everything you want using the higher-level \crefname
command instead of \crefformat
. This has the advantage of automatically defining all the other variants - plurals, reference ranges, etc. - which would otherwise need to be added manually to the above MWE in any real document:
\documentclass[english,ngerman]{article}
\usepackage[demo]{graphicx}
\usepackage{babel}
\usepackage{hyperref}
\usepackage{cleveref}
\makeatletter
\cref@addlanguagedefs{english}{%
\crefname{figure}{my fancy figure}{my fancy figures}}
\cref@addlanguagedefs{ngerman}{%
\crefname{figure}{meine tolle Abbildung}{meine tolle Abbildungen}}
\makeatother
\begin{document}
\selectlanguage{ngerman}
\cref{fig:one}
\begin{figure}[h]
\includegraphics[width=\textwidth]{example-image-a}
\caption{I am a figure\label{fig:one}}
\end{figure}
\selectlanguage{english}
\cref{fig:one}
\selectlanguage{ngerman}
\cref{fig:one}
\end{document}
Maybe there's a case for adding an optional language argument to \crefname
, \crefformat
etc. when babel or polyglossia is loaded, to simplify this a little in multilingual documents. On the other hand, language switching in cleveref works "out of the box" as long as you're happy with the default formats and names. Going beyond this and customising the default language strings seems to require very similar solutions in other packages (e.g. varioref). And the above mechanism is explained in the babel documentation. There's a strong case for making cleveref work the same way as other packages.
Best Answer
The
spanish
option is not passed to the class, to begin with.I don't think you really need to have class options for this, however. Here is
zunbeltz.cls
:and here the test document
zunbeltz.tex
:You'll get