I'm trying to extend the example for multilanguage support shown in here: https://tex.stackexchange.com/a/42791/7561
The current solution
As it is right now, one needs to define the macro using
\newlangcommand{\uno}
and then declare de different outputs one language at a time, like
\addtolangcommand{\uno}{english}{one}
\addtolangcommand{\uno}{french}{une}
The expected behavior
However, I want to use that in a single call. For example
\newmlcommand[english=one, french=une]{\uno}
That should declare the \uno macro, and then make the \addtolangcommand once per pair of language string in the optional argument.
The problem
I defined that macro as
\newcommand{\newmlcommand}[2][]{%
\newlangcommand#2%
\pgfkeys{/mlcommands, command=#2, #1}%
}
and thought that I will use pgfkeys to handle the languages. The idea was to do it with:
\pgfkeys{
/mlcommands/.is family, /mlcommands,
command/.get=\langcmd,
.unknown/.code = {%
\addtolangcommand{\langcmd}{\pgfkeyscurrentname}{#1}
}%
}
That is for every pair of language=string I will execute the \addtolangcommand and assign them to the \langcmd temporal macro that holds the original macro passed to \newmlcommand.
However, I'm can't get the delay expansion of the macros right. My idea is to execute
\addtolangcommand{\langcmd}{language}{string}
that was passed using the key pair language=string. To achieve that, the \addlangcommand should be on hold, and expand both \langcmd into the original macro definition and \pgfkeyscurrentname into the language string before executing the \addtolangcommand. Note that the \langcmd is set as parameter using the key pair command=\macro that is set in the options in \newmlcommand.
However I can't get it right.
Full code
Here are my attempts:
\documentclass{article}
\usepackage[french, english, spanish]{babel}
\usepackage{pgfkeys}
\makeatletter
% https://tex.stackexchange.com/a/42791/7561
\newcommand{\newlangcommand}[1]{%
\newcommand#1{%
\@ifundefined{\string#1\languagename}
{``No def of \texttt{\string#1} for \languagename''}
{\@nameuse{\string#1\languagename}}%
}%
}
\newcommand{\addtolangcommand}[3]{%
\@namedef{\string#1#2}{#3}}
\def\langcmd{}
\pgfkeys{
/mlcommands/.is family, /mlcommands,
command/.get=\langcmd,
.unknown/.code = {%
% tried this idea: save it in a macro and execute it later
% but, the expansion is still a problem
% \def\tmp{\noexpand\addtolangcommand{\langcmd}{\pgfkeyscurrentname}{#1}}%
% \tmp%
% and this idea: try to delay the expansion of addtolangcommand
% \expandafter\expandafter\expandafter\expandafter\addtolangcommand\expandafter\expandafter{\langcmd}\expandafter{\pgfkeyscurrentname}{#1}%
% and the naive aplication doesn't work either
\addtolangcommand{\langcmd}{\pgfkeyscurrentname}{#1}
}%
}
\newcommand{\newmlcommand}[2][]{%
\newlangcommand#2%
\pgfkeys{/mlcommands, command=#2, #1}%
}
\makeatother
% How to use (original)
%\newlangcommand{\uno}
%\addtolangcommand{\uno}{english}{one}
%\addtolangcommand{\uno}{french}{une}
% or the new (non working way)
\newmlcommand[english=one, french=une]{\uno}
\begin{document}
\selectlanguage{english}
\uno
\selectlanguage{french}
\uno
\selectlanguage{spanish}
\uno
\end{document}
english=stringexecutes\addtolangcommand{\cmd}{english}{string}where\cmdis the macro passed as parameter incommand=\macroin the definition of the\newmlcommand. – adn Aug 29 '16 at 21:05\expandafter\expandafter\expandafter\addtolangcommand\expandafter\expandafter\expandafter{\expandafter\langcmd\expandafter}\expandafter{\pgfkeyscurrentname}{#1}– percusse Aug 29 '16 at 21:51