%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.

% TODO: only preload English and delay loading of other languages
% till some related command is used.

\writestatus{loading}{ConTeXt Language Macros / Labels}

\registerctxluafile{lang-lab}{autosuffix}
\registerctxluafile{lang-txt}{}

\unprotect

%D Left-overs:

\ifdefined\sixperemspace \else
    \permanent\edef\sixperemspace{\tocharacter"2006}
\fi

%D In this module we deal with language dependant labels and prefixes, like in {\em
%D Figure~12} and {\em Chapter 1}. In this file we set the default values. Users can
%D easily overrule these.
%D
%D This module is dedicated to the grandfather of Tobias Burnus, who's extensive
%D languages oriented library helped us a lot in finding the right translations. All
%D those labels are collected in files that reflect their common ancestor.
%D
%D Not all languages can be satisfied with the labeling mechanism as provided here.
%D Chinese for instance put a label in front as well as after a part number. This is
%D why the 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 call head and label
%D texts. Such texts are defines by:
%D
%D \showsetup{setupheadtext}
%D \showsetup{setuplabeltext}
%D
%D A regular \CONTEXT\ stores some 1500 labels at most.
%D
%D These commands accept all kind of inputs:
%D
%D \starttyping
%D \setuplabeltext [language] [label1=text]
%D \setuplabeltext [language] [label1=text,label2=text,...]
%D \setuplabeltext            [label1=text]
%D \setuplabeltext            [label1=text,label2=text,...]
%D \stoptyping
%D
%D The last two cases concern the current language.

\installcorenamespace{label}

% \lettonothing\currentlabelcategory

\permanent\tolerant\protected\def\definelabelclass[#1]#*[#2]%
  {\normalexpanded
     {\lang_labels_define_class_indeed
        {#1}%
        {\ifchknumber#2\or#2\else\zerocount\fi}% not \lastchknumber as we need expansion
        \expandafter\noexpand\csname lang_labels_#1_text_1\endcsname
        \expandafter\noexpand\csname            #1language\endcsname
        \expandafter\noexpand\csname lang_labels_#1_text_2\endcsname
        \expandafter\noexpand\csname            left#1text\endcsname
        \expandafter\noexpand\csname           right#1text\endcsname
        \expandafter\noexpand\csname               #1texts\endcsname
        \expandafter\noexpand\csname                #1text\endcsname}}

% hm, not interfaced

\mutable\lettonothing\thetextprefix

\mutable\let\currenttextprefixclass\s!unknown
\mutable\let\currenttextprefixtag  \s!unknown

\mutable\lettonothing\currenttextprefixname

\protected\def\lang_labels_define_class_indeed#1#2#3#4#5#6#7#8#9%
  {\permanent\instance\protected\defcsname  setup#1text\endcsname{\protecttextprefixes      #2\def\currenttextprefixclass{#1}\lang_labels_text_prefix_setup}%
   \permanent\instance\protected\defcsname preset#1text\endcsname{\protecttextprefixes\plusone\def\currenttextprefixclass{#1}\lang_labels_text_prefix_setup}%
   \permanent\instance\protected\defcsname   copy#1text\endcsname{\protecttextprefixes\plusone\def\currenttextprefixclass{#1}\lang_labels_text_prefix_copy }%
   \permanent\instance\protected\defcsname  start#1text\endcsname{\protecttextprefixes\plusone\def\currenttextprefixclass{#1}\lang_labels_text_prefix_start[#1]}%
   \permanent\instance          \letcsname   stop#1text\endcsname \relax
   \permanent\instance\def#4{\reallanguagetag{\defaultlanguage\currentmainlanguage}}%
   \ifnum#2=\plustwo % used for math and tags
     \instance\def#3{#5#4}%
%      \instance\def#5##1##2% ##1=language
%        {\ifcsname\??label\currentlabelcategory#1:##1:##2\endcsname
%           \lastnamedcs
%         \orelse\ifcsname\??label#1:##1:##2\endcsname
%           \lastnamedcs
%         \orelse\ifcsname\??language##1\s!default\endcsname
%           \expandafter#5\lastnamedcs{##2}%
%         \orelse\ifcsname\??label\currentlabelcategory#1:##2\endcsname
%           \lastnamedcs
%         \orelse\ifcsname\??label#1:##2\endcsname
%           \lastnamedcs
%         \orelse\ifcsname\??label\currentlabelcategory#1:\s!en:##2\endcsname
%           \lastnamedcs
%         \orelse\ifcsname\??label#1:\s!en:##2\endcsname
%           \lastnamedcs
%         \else
%           ##2%
%         \fi}%
     \instance\def#5##1##2% ##1=language
       {\ifcsname\??label#1:##1:##2\endcsname
          \lastnamedcs
        \orelse\ifcsname\??language##1\s!default\endcsname
          \expandafter#5\lastnamedcs{##2}%
        \orelse\ifcsname\??label#1:##2\endcsname
          \lastnamedcs
        \orelse\ifcsname\??label#1:\s!en:##2\endcsname
          \lastnamedcs
        \else
          ##2%
        \fi}%
     \permanent\instance\let#6\gobbleoneargument
     \permanent\instance\let#7\gobbleoneargument
     \permanent\instance\let#8\gobbletwoarguments
     \permanent\instance\let#9#3%
   \else
     \instance\protected\def#3{#5#4}%
     \instance\protected\def#5##1##2%
       {\ifcsname\??label#1:##1:##2\endcsname
          \enforced\expandafter\let\expandafter\thetextprefix\lastnamedcs
        \orelse\ifcsname\??language#4\s!default\endcsname
          \expandafter#5\lastnamedcs{##2}%
        \orelse\ifcsname\??label#1:##2\endcsname
          \enforced\expandafter\let\expandafter\thetextprefix\lastnamedcs
        \orelse\ifcsname\??label#1:\s!en:##2\endcsname
          \enforced\expandafter\let\expandafter\thetextprefix\lastnamedcs
        \else
          \enforced\let\thetextprefix\dummytextprefix
        \fi}%
     \permanent\instance\protected\def#6##1{#3{##1}\expandafter\firstoftwoarguments   \thetextprefix}% \flushleftlabelclass
     \permanent\instance\protected\def#7##1{#3{##1}\expandafter\secondoftwoarguments  \thetextprefix}% \flushrightlabelclass
     \permanent\instance\protected\def#8##1{#3{##1}\expandafter\lang_labels_flush_both\thetextprefix}% \flushbothlabelclass #2
     \permanent\instance\protected\def#9##1{#3{##1}\expandafter\firstoftwoarguments   \thetextprefix}% \flushleftlabelclass
   \fi
   \appendtoks
     \enforced\permanent\instance\let#6\firstofoneargument % to be checked
     \enforced\permanent\instance\let#7\firstofoneargument % to be checked
     \enforced\permanent\instance\let#8\firstofoneargument % to be checked
     \enforced\permanent\instance\let#9\firstofoneargument % to be checked
   \to \everysimplifycommands}

% why not lang_* ?

% \let\flushleftlabelclass \firstoftwoarguments
% \let\flushrightlabelclass\secondoftwoarguments

\permanent\def\lang_labels_flush_both#1#2#3{#1#3#2}

\permanent\def\dummytextprefix{\empty\empty}

%D \macros
%D   {headtext, 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 \macros
%D   {presetheadtext,presetlabeltext}
%D
%D These macros enable us to automatically define head and label texts without
%D replacing predefined ones. They are internal macros.

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

\newconstant\protecttextprefixes

\tolerant\def\lang_labels_text_prefix_start[#1]#*[#2]#*[#3]% class language name
  {\ifarguments\or\or
     \cdef\currenttextprefixtag{\reallanguagetag\currentmainlanguage}%
     \cdef\currenttextprefixname{#2}%
   \else
     \cdef\currenttextprefixtag{\reallanguagetag{#2}}%
     \cdef\currenttextprefixname{#3}%
   \fi
   \grabuntil{stop#1text}\lang_labels_text_prefix_start_indeed}

\def\lang_labels_text_prefix_start_indeed#1% text (not special checking done here yet, only for long texts anyway)
  {\edefcsname\??label\currenttextprefixclass:\currenttextprefixtag:\currenttextprefixname\endcsname{{\clf_strip{#1}}\empty}}

\tolerant\def\lang_labels_text_prefix_setup[#1]#*[#2]%
  {\ifarguments\or
     \cdef\currenttextprefixtag{\reallanguagetag\currentmainlanguage}%
     \processcommalist[#1]\lang_labels_text_prefix_setup_indeed
   \else
     \cdef\currenttextprefixtag{\reallanguagetag{#1}}%
     \processcommalist[#2]\lang_labels_text_prefix_setup_indeed
   \fi}

\def\lang_labels_text_prefix_setup_indeed#1%
  {\lang_labels_text_prefix_assign[#1]}

\def\lang_labels_text_prefix_assign[#1=#2]%
  {\lang_labels_text_prefix_assign_indeed{#1}[#2,,]}

\def\lang_labels_text_prefix_assign_indeed#1%
  {\ifcase\protecttextprefixes
     % no checking
     \expandafter\lang_labels_text_prefix_assign_yes
   \or
     % checking
     \ifcsname\??label\currenttextprefixclass:\currenttextprefixtag:#1\endcsname
       \expandafter\expandafter\expandafter\lang_labels_text_prefix_assign_nop
     \else
       \expandafter\expandafter\expandafter\lang_labels_text_prefix_assign_yes
     \fi
   \or
     % simple assignment (a bit overkill but it fits in the whole)
     \expandafter\lang_labels_text_prefix_assign_dumb
   \fi{#1}}

\lettonothing\m_lang_labels_left
\lettonothing\m_lang_labels_right

\def\lang_labels_text_prefix_assign_yes#1[#2,#3,#4]%
  {\def\m_lang_labels_left {#2}% no longer an edef ... else \Word undefined expansion issues
   \def\m_lang_labels_right{#3}% no longer an edef ... else \Word undefined expansion issues
   \defcsname\??label\currenttextprefixclass:\currenttextprefixtag:#1%
     \ifempty\m_lang_labels_right
       \ifempty\m_lang_labels_left
         \endcsname{\empty\empty}%
       \else
         \endcsname{{#2}\empty}%
       \fi
     \else
       \endcsname{{#2}{#3}}%
     \fi}

\def\lang_labels_text_prefix_assign_nop#1[#2]%
  {}

\def\lang_labels_text_prefix_assign_dumb#1[#2,#3]%
  {\defcsname\??label\currenttextprefixclass:\currenttextprefixtag:#1\endcsname{#2}}

\permanent\protected\def\setlabeltextpair#1#2#3#4#5% a fast one for usage at the Lua end
  {%\writestatus{!!!!}{#1:\reallanguagetag{#2}:#3}%
   \defcsname\??label#1:\reallanguagetag{#2}:#3\endcsname{{#4}{#5}}} % class tag key left right

\tolerant\def\lang_labels_text_prefix_copy[#1]#*[#2]%
  {\ifarguments\or
     \cdef\currenttextprefixtag{\reallanguagetag\currentmainlanguage}%
     \processcommalist[#1]\lang_labels_text_prefix_copy_indeed
   \else
     \cdef\currenttextprefixtag{\reallanguagetag{#1}}%
     \processcommalist[#2]\lang_labels_text_prefix_copy_indeed
   \fi}

\def\lang_labels_text_prefix_copy_indeed#1%
  {\lang_labels_text_prefix_copy_pair[#1]}

\def\lang_labels_text_prefix_copy_pair[#1=#2]%
  {\lang_labels_text_prefix_copy_pair_indeed{#1}[#2,,]}

% \def\lang_labels_text_prefix_copy_pair_indeed#1[#2,#3]%
%   {\expandafter\let
%      \csname\??label\currenttextprefixclass:\currenttextprefixtag:#1\expandafter\endcsname
%      \csname\??label\currenttextprefixclass:\currenttextprefixtag:#2\endcsname}
%
% this delays the aliasing so that we can switch mainlanguage in between

\def\lang_labels_text_prefix_copy_pair_indeed#1[#2,#3]%
  {\edefcsname\??label\currenttextprefixclass:#1\endcsname
     {{\noexpand\csname\??label\currenttextprefixclass:\noexpand\reallanguagetag\noexpand\currentmainlanguage:#2\endcsname}{}}}

\definelabelclass [head]      [0] % titles
\definelabelclass [label]     [0] % texts
\definelabelclass [mathlabel] [2] % functions
\definelabelclass [taglabel]  [2] % tags

\clf_definelabels{head}{titles}\s!true\relax
\clf_definelabels{label}{texts}\s!true\relax
\clf_definelabels{mathlabel}{functions}\s!false\relax
\clf_definelabels{taglabel}{tags}\s!false\relax

%D \macros
%D   {translate}
%D
%D Sometimes macros contain language specific words that are to be typeset. Such
%D macros can be made (more) language 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 de current language.

\installcorenamespace{translation}

\permanent\tolerant\protected\def\translate[#1]%
  {\getparameters[\??translation][#1]%
   \ifcsname\??translation\currentlanguage\endcsname
     \lastnamedcs
   \orelse\ifcsname\??translation\s!en\endcsname
     \lastnamedcs
   \else
     [translation #1]%
   \fi}

%D When used without argument, the last defined values are used. This enables
%D 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 translation to a macro.
%D Its form is:
%D
%D \starttyping
%D \assigntranslation[en=something,nl=iets]\to\command
%D \stoptyping

% nicer is \edef\command{\translated[...]} ... todo

\permanent\protected\def\assigntranslation[#1]\to#2% bad, this \to
  {\getparameters[\??translation][#1]%
   \edef#2{\begincsname\??translation\currentlanguage\endcsname}}

%D \macros
%D   {commalistsentence}
%D
%D Redone in \LUA:
%D
%D \startbuffer
%D \commalistsentence[aap,noot,mies]
%D \commalistsentence[aap,noot]
%D \commalistsentence[aap]
%D \commalistsentence[a,b,c]
%D \commalistsentence[a,b,c][{ \& },{ and }]
%D \commalistsentence[a,b,c][+,-]
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlines
%D \getbuffer
%D \stoplines

\permanent\tolerant\protected\def\commalistsentence[#1]#*[#2]%
  {\clf_concatcommalist
     text       {#1}%
     separators {#2}%
     separator  {\detokenize\expandafter{\normalexpanded{\labeltext{and-1}}}}%
     last       {\detokenize\expandafter{\normalexpanded{\labeltext{and-2}}}}%
   \relax}

\setuplabeltext [\s!nl] [and-1={{, }},and-2={{ en }}]   % 1, 2 en 3
\setuplabeltext [\s!en] [and-1={{, }},and-2={{, }}]     % 1, 2, 3
\setuplabeltext [\s!de] [and-1={{, }},and-2={{ und }}]  % 1, 2 und 3
\setuplabeltext [\s!hr] [and-1={{, }},and-2={{ i }}]    % 1, 2 i 3

%D Goodie (handy at \LUA\ end):

\permanent\protected\def\LABELTEXT#1{\WORD{\labeltext{#1}}} % only for simple cases!

%D For publications we have gender specific ones (one could use the main one for a
%D gender neutral one).
%D
%D \startbuffer
%D \startlines
%D     (\labeltext     {demo-texts})
%D     (\labeltexts    {demo-texts}{ TEST })
%D     (\leftlabeltext {demo-texts})
%D     (\rightlabeltext{demo-texts})
%D \stoplines
%D
%D \startlines \mainlanguage[cs]
%D     test \btxlabeltext{editor}   \par
%D     test \btxlabeltext{editor:f} \par
%D     test \btxlabeltext{editor:m} \par
%D     test \btxlabeltext{editors}   \par
%D     test \btxlabeltext{editors:f} \par
%D     test \btxlabeltext{editors:m} \par
%D \stoplines
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\protect \endinput

