%D \module
%D   [       file=lang-lab,
%D        version=1997.08.27,
%D          title=\CONTEXT\ Language Macros,
%D       subtitle=Labels,
%D         author=Hans Hagen / Tobias Burnus,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\unprotect

%D In this module we deal with language dependant labels and
%D prefixes, like in {\em Figure~12} and {\em Chapter 1}. In
%D this file we set the default values. Users can easily
%D overrule these.
%D
%D This module is dedicated to the grandfather of Tobias
%D Burnus, who's extensive languages oriented library helped us
%D a lot in finding the right translations. All those labels
%D are collected in files that reflect their common ancestor.
%D
%D Not all languages can be satisfied with the labeling
%D mechanism as provided here. Chinese for instance put a label
%D in front as well as after a part number. This is why the
%D current implementation of labels supports two labels too.

%D \macros
%D   {setupheadtext, setuplabeltext}
%D
%D First we present some macros that deal with what we will
%D call head and label texts. Such texts are defines by:
%D
%D \showsetup{setupheadtext}
%D \showsetup{setuplabeltext}
%D
%D In a few paragraphs we'll show quite a lot of examples
%D of its use.

\let\handletextprefix\relax

\def\setupheadtext {\dosetupsometextprefix[\c!title]}
\def\setuplabeltext{\dosetupsometextprefix[\c!label]}

\def\dosetupsometextprefix
  {\let\dodocommand\xdosetupsometextprefix
   \dotripleempty\dodosetupsometextprefix}

% \def\dodosetupsometextprefix[#1][#2][#3]%
%   {\ifthirdargument
%      \def\docommand##1{\dodocommand[#1#2][##1]}%
%      \processcommalist[#3]\docommand
%    \else
%      \def\docommand##1{\dodocommand[#1\currentmainlanguage][##1]}%
%      \processcommalist[#2]\docommand
%    \fi}

\def\dodosetupsometextprefix[#1][#2][#3]%
  {\ifthirdargument
     \def\docommand##1{\expanded{\dodocommand[#1\reallanguagetag{#2}]}[##1]}%
     \processcommalist[#3]\docommand
   \else
     \def\docommand##1{\expanded{\dodocommand[#1\reallanguagetag\currentmainlanguage]}[##1]}%
     \processcommalist[#2]\docommand
   \fi}

\def\doassignsometextprefix[#1][#2,#3,#4]%
  {\setvalue{#1}{\handletextprefix{#2}{#3}}}

\def\xdosetupsometextprefix[#1][#2=#3]%
  {\doassignsometextprefix[#1#2][#3,,]}

%D By changing the meaning of \type {\handletextprefix} we
%D can filter the left and right labeltext as well as convert
%D labels to uppercase.
%D
%D These commands accept all kind of inputs:
%D
%D \starttyping
%D \setuplabeltext [language] [labellabel=text]
%D \setuplabeltext [language] [labellabel=text,labellabel=text,...]
%D \setuplabeltext            [labellabel=text]
%D \setuplabeltext            [labellabel=text,labellabel=text,...]
%D \stoptyping
%D
%D The last two cases concern the current language.

%D \macros
%D   {headtext,
%D    labeltext, leftlabeltext, rightlabeltext, labeltexts,
%D    LABELTEXT, LEFTLABELTEXT, RIGHTLABELTEXT, LABELTEXTS}
%D
%D Once defined, head and label texts can be called upon using:
%D
%D \showsetup{headtext}
%D \showsetup{labeltext}
%D
%D The latter one has an upcased alternative \type{\LABELTEXT}.

% \def\labellanguage{\currentmainlanguage}
% \def\headlanguage {\currentmainlanguage}

% \def\labellanguage{\defaultlanguage\currentmainlanguage}
% \def\headlanguage {\defaultlanguage\currentmainlanguage}

\def\labellanguage{\reallanguagetag{\defaultlanguage\currentmainlanguage}}
\def\headlanguage {\reallanguagetag{\defaultlanguage\currentmainlanguage}}

\appendtoks \let\labellanguage\currentlanguage \to \everycurrentdate

\unexpanded\def\headtext
  {\let\handletextprefix\firstoftwoarguments
   \let\reporttextprefixerror\doreporttextprefixerror
   \global\labeltextdonetrue
   \dogetupsometextprefix\headlanguage\c!title}

\unexpanded\def\leftlabeltext
  {\let\handletextprefix\firstoftwoarguments
   \let\reporttextprefixerror\doreporttextprefixerror
   \global\labeltextdonetrue
   \dogetupsometextprefix\labellanguage\c!label}

\unexpanded\def\rightlabeltext
  {\let\handletextprefix\secondoftwoarguments
   \let\reporttextprefixerror\doreporttextprefixerror
   \global\labeltextdonetrue
   \dogetupsometextprefix\labellanguage\c!label}

\unexpanded\def\LEFTLABELTEXT
  {\def\handletextprefix##1##2{\uppercase{##1}}\DOLABELTEXT}

\unexpanded\def\RIGHTLABELTEXT
  {\def\handletextprefix##1##2{\uppercase{##2}}\DOLABELTEXT}

\def\DOLABELTEXT#1%
  {\bgroup
   \the\everyuppercase
   \let\reporttextprefixerror\doreporttextprefixerror
   \global\labeltextdonetrue
   \dogetupsometextprefix\labellanguage\c!label{#1}% not \labeltext (see \MONTH)
   \egroup}

\let\labeltext      \leftlabeltext
\let\LABELTEXT      \LEFTLABELTEXT

\unexpanded\def\labeltexts#1#2{\leftlabeltext{#1}#2\rightlabeltext{#1}}
\unexpanded\def\LABELTEXTS#1#2{\LEFTLABELTEXT{#1}#2\RIGHTLABELTEXT{#1}}

\newif\iflabeltextdone % needs to be reset elsewhere
\newif\iftracelabels % shows missing labels

\def\doreporttextprefixerror#1#2#3%
  {\iftracelabels{\tttf[#2:~#3/#1]~}\fi}

\def\dosetexpandedheadlabeltext#1#2#3%
  {\bgroup
   \let\handletextprefix\firstoftwoarguments
   \let\reporttextprefixerror\gobblethreearguments
   \keepencodedtokens % test on multilingual pascal, ok in stretched
  %\dontexpandencodedtokens % not usable in token handler
   \expanded
     {\egroup\noexpand\def\noexpand#2% watch out, no \edef
        {\dogetupsometextprefix{\headlanguage}{#1}{#3}}}}

\def\setexpandedheadtext {\dosetexpandedheadlabeltext\c!title}
\def\setexpandedlabeltext{\dosetexpandedheadlabeltext\c!label}

% \def\dogetupsometextprefix#1#2#3%
%   {\ifcsname#2#1#3\endcsname
%      \csname#2#1#3\endcsname                 \else
%    \ifcsname#2#3\endcsname
%      \csname#2#3\endcsname                   \else
%    \ifcsname#2\defaultlanguage#1#3\endcsname
%      \csname#2\defaultlanguage#1#3\endcsname \else
%    \ifcsname#2\s!en#3\endcsname
%      \csname#2\s!en#3\endcsname              \else
%    \ifcsname#2\s!nl#3\endcsname
%      \csname#2\s!nl#3\endcsname              \else
%      \reporttextprefixerror{#1}{#2}{#3}%
%    \fi\fi\fi\fi\fi}
%
% \def\dogetupsometextprefix#1#2#3% must be expandable !
%   {\ifcsname#2#1#3\endcsname
%      \csname#2#1#3\endcsname
%    \else\@EA\ifx\csname\??la#1\c!default\endcsname\empty
%      \ifcsname#2#3\endcsname
%        \csname#2#3\endcsname
%      \else\ifcsname#2\s!en#3\endcsname
%        \csname#2\s!en#3\endcsname
%      \else
%        \reporttextprefixerror{#1}{#2}{#3}%
%      \fi\fi
%    \else
%      \dogetupsometextprefix{\csname\??la#1\c!default\endcsname}{#2}{#3}%
%    \fi\fi}

\def\dogetupsometextprefix#1#2#3% must be expandable ! #1 == language
  {\ifcsname#2#1#3\endcsname
     \csname#2#1#3\endcsname
   \else\ifcsname\??la#1\s!default\endcsname
     \expandafter\dogetupsometextprefix\csname\??la#1\s!default\endcsname{#2}{#3}%
   \else\ifcsname#2#3\endcsname
     \csname#2#3\endcsname
   \else\ifcsname#1\s!en#3\endcsname
     \csname#2\s!en#3\endcsname
   \else
     \reporttextprefixerror{#1}{#2}{#3}%
   \fi\fi\fi\fi}

\ifx\simplifiedcommands\undefined \newtoks\simplifiedcommands \fi

\appendtoks
  \let \headtext       \firstofoneargument
  \let \labeltext      \firstofoneargument
  \let \leftlabeltext  \firstofoneargument
  \let \rightlabeltext \firstofoneargument
  \let \HEADTEXT       \firstofoneargument
  \let \LABELTEXT      \firstofoneargument
  \let \LEFTLABELTEXT  \firstofoneargument
  \let \RIGHTLABELTEXT \firstofoneargument
\to \simplifiedcommands

%D \macros
%D   {presetheadtext,presetlabeltext}
%D
%D The next two macros enable us to automatically define
%D head and label texts without replacing predefined ones.
%D These are internal macros.

\def\xdopresetsometextprefix[#1][#2=#3]%
  {\ifundefined{#1#2}\doassignsometextprefix[#1\reallanguagetag{#2}][#3,,]\fi}

\def\dopresetsometextprefix
  {\let\dodocommand\xdopresetsometextprefix
   \dotripleempty\dodosetupsometextprefix}

\def\presetheadtext {\dopresetsometextprefix[\c!title]}
\def\presetlabeltext{\dopresetsometextprefix[\c!label]}

%D \macros
%D   {translate}
%D
%D Sometismes macros contain language specific words that are to
%D be typeset. Such macros can be made (more) language
%D independant by using:
%D
%D \showsetup{translate}
%D
%D like for instance:
%D
%D \starttyping
%D \translate[en=something,nl=iets]
%D \stoptyping
%D
%D which expands to {\em something} or {\em iets}, depending on
%D de current language.

\def\dotranslate[#1]%
  {\getparameters[\??lg][#1]%
   \ifcsname\??lg\currentlanguage\endcsname
     \csname\??lg\currentlanguage\endcsname
   \else\ifcsname\??lg\s!en\endcsname
     \csname\??lg\s!en\endcsname
   \else
     [translation #1]%
   \fi\fi}

\unexpanded\def\translate
  {\dosingleempty\dotranslate}

%D When used without argument, the last defined values are
%D used. This enables repetitive use like
%D
%D \starttyping
%D \en \translate\ means \nl \translate
%D \stoptyping

%D \macros
%D   {assigntranslation}
%D
%D This macro is a system macro, and can be used to assign a
%D translation to a macro. Its form is:
%D
%D \starttyping
%D \assigntranslation[en=something,nl=iets]\to\command
%D \stoptyping

\def\assigntranslation[#1]\to#2%
  {\getparameters[\??lg][#1]%
   \edef#2{\csname\??lg\currentlanguage\endcsname}}

\protect \endinput
