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

\writestatus{loading}{Language Head and Label Texts}

\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\dodocommando\xdosetupsometextprefix
   \dotripleempty\dodosetupsometextprefix}

\def\dodosetupsometextprefix[#1][#2][#3]%
  {\ifthirdargument
     \def\docommando##1{\dodocommando[#1#2][##1]}%
     \processcommalist[#3]\docommando
   \else
     \def\docommando##1{\dodocommando[#1\currentmainlanguage][##1]}%
     \processcommalist[#2]\docommando
   \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}

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

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

\endETEX

\beginTEX

\def\dogetupsometextprefix#1#2#3%
  {\@EA\ifx\csname#2#1#3\endcsname\relax
   \@EA\ifx\csname#2#3\endcsname\relax
   \@EA\ifx\csname#2\defaultlanguage{#1}#3\endcsname\relax
   \@EA\ifx\csname#2\s!en#3\endcsname\relax
   \@EA\ifx\csname#2\s!nl#3\endcsname\relax
           \reporttextprefixerror{#1}{#2}{#3}%
      \else\csname#2\s!nl#3\endcsname
   \fi\else\csname#2\s!en#3\endcsname
   \fi\else\csname#2\defaultlanguage{#1}#3\endcsname
   \fi\else\csname#2#3\endcsname
   \fi\else\csname#2#1#3\endcsname
   \fi}

\endTEX

\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#2][#3,,]\fi}

\def\dopresetsometextprefix
  {\let\dodocommando\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]% don't group! SLOW if really used: speed up
  {\getparameters[\??lg][#1]%
   \doifdefinedelse{\??lg\currentlanguage}%
     {\getvalue{\??lg\currentlanguage}}
     {\doifdefinedelse{\??lg\s!en}
        {\getvalue{\??lg\s!en}}
        {\doifdefinedelse{\??lg\s!nl}
          {\getvalue{\??lg\s!nl}}
          {[translation #1]}}}}

\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
