%\iffalse
% Copyright 1997 Javier Bezos-L\'opez. All rights reserved.
% 
% This file is part of the polyglot system release 1.1.
% ------------------------------------------------------
%
% This program can be redistributed and/or modified under the terms
% of the LaTeX Project Public License Distributed from CTAN
% archives in directory macros/latex/base/lppl.txt; either
% version 1 of the License, or any later version.
%
%\fi

\def\fileversion{1.1}
\def\filedate{September 1, 1997}
\def\docdate{September 1, 1997}

% 
% \title{The \textsf{Polyglot} Package\footnote{This 
% package is currently at version 1.1.}}
%
% \author{Javier Bezos-L\'opez\footnote{Please, send your
% comments and suggestions to \texttt{jbezos@mx3.redestb.es}, or
% to my postal address: Apartado 116.035, E-28080 Madrid, Spain.
% English is not my strong point, so contact me when you
% find mistakes in the manual.}}
%
% \date{\docdate}
% 
% \maketitle
%
% \def\abstractname{Notice to Users of Version 1.0}
%
% \begin{abstract}
% I'm afraid some user commands have been changed,
% specially |\selectlanguage| and |\uselanguage|,
% and some other supressed---|\enablegroup| 
% and |\disablegroup|, which have been merged into
% |\selectlanguage|. These changes will be definitive, and I
% had to do them in order to implement a right communication
% to files and marks, and avoid mixing up definitions which sometimes
% made \LaTeX\ enter in an endless loop.
% Furthermore, the new syntax is far more robust,
% flexible and readable.
%
% Most of commands for description
% files haven't changed at all, though some of them has been 
% reimplemented. Yet, handling of dialects has been
% much improved; there is a new |\SetLanguage| (the old one
% has been renamed to |\SetDialect|) and you can create
% a dialect at any time with |\DeclareDialect| without the restrictions
% of the now obsolete |\GenerateDialects|.
% \end{abstract}
%
% 
% \section{Introduction}
% 
% The package \textsf{polyglot} provides a new language selection
% system taking advantage of the features of \LaTeXe. It provides some
% utilities which make writing a language style quite easy and
% straightforward.
%
% Some of the features implemeted by polyglot are:
%
% \begin{itemize}
% \item You can switch between languages freely. You have not
% to take care of neither head lines nor toc and bib
% entries---the right language is always used.\footnote{Index
%   entries follow a special syntax and
%   the current version of \textsf{polyglot} cannot handle that. For this
%   reason the index entries remain untouched and you may
%   use language commands only to some extent.}
% You can even get just a few commands from a language, not all.
% 
% \item ``Ligatures,'' which are shortcuts for diacritical
% marks; for instance, in French |^e| is a synonymous
% to |\^{e}|. Some other ligatures perform special actions,
% as Spanish |'r| which allows divide ``extrarradio'' as 
% ``extra-radio''. A very cool feature: in most of cases,
% ligatures does not interfere with other definitions;
% for instance, with the \textsf{index} package
% |^{<entry>}| still works, provided the <entry> has
% two or more tokens.\footnote{And provided 
% \textsf{index} is loaded before \textsf{polyglot}.
% \textsf{index} is a package by David Jones.}
%
% \item Dialects---small language variants---are supported. For
% instance American is a variant of English with another date
% format. Hyphenations patterns are attached to dialects and
% different rules for US and British English can be used.
% 
% \item New glyphs are created if necessary. For instance, if
% you are using |OT1| the German umlauts are lowered, but if you
% switch to |T1| the actual font glyphs are used. Some other font
% encoding may have their own glyphs which will be used only 
% with that encoding.
% 
% \item Customization is quite easy---just redefine a command
% of a language with |\renewcommand| when the language
% is in force. The new definition will be remembered,
% even if you switch back and forth between languages.
% 
% \item An unique layout can be used through the document,
% even with lots of languages. If you use a class with
% a certain layout you don't want to modify, you or the
% class can tell \textsf{polyglot} not to touch
% it at all.
% \end{itemize}
%
% \section{Quick start}
%
% \subsection{Installation}
%
% To install the package follow these steps:
% \begin{enumerate}
% \item First, run |polyglot.ins|. The files |polyglot.sty|,
%    |polyglot.ltx|, |polyglot.def| and |polyglot.cfg| are generated.
%
% \item Remove |polyglot.ltx| and
%    |polyglot.def| as they are not needed in the basic installation.
%
% \item Move |polyglot.sty| and |polyglot.cfg| as well as the files
%  with extensions |.ld| and |.ot1| to a place where \LaTeX{}
%  can find them.
% \end{enumerate}
%
% The files are: 
%
% \begin{itemize}
% \item |polyglot.sty| is the file to be read with |\usepackage|.
%
% \item |polyglot.cfg| gives your system configuration.
%
% \item Languages are stored in files with
%       extension |.ld|, as, for instance, |spanish.ld|, |english.ld|
%       and so on.
%
% \item |polyglot.ltx| has some definitions for instalation of hyphenation
%       patterns as well as preloading of languages and the \textsf{polyglot} style
%       (actually |polyglot.def|).
%
% \item Finally, there are some files named like languages, but with extensions
%       like font encodings.
% \end{itemize}
%
% Once installed, you can use polyglot.
% To write a, say, German document simply state the
% |german| option in the |\documentclass| and load
% the package with |\usepackage{polyglot}|.
% That's all. A new layout, with new commands,
% ligatures and, if the |OT1| encoding is in force,
% new glyphs will be available to you, as described
% at the end of this manual. If you are happy with
% that you need not go further; but if you are
% interested in advanced features (how to insert a
% Spanish date or how to create your own ligatures,
% for instance) just continue. 
%
% \section{User Interface}
% 
% \subsection{Groups}
% 
% A language has a series of commands and variables (counters and lengths)
% stored in several groups. These groups are:
% \begin{description}
% \item[names] Translation of commands for titles, etc., following
% the international \LaTeX{} conventions.
% \item[layout] Commands and variables for the general layout of the
% document (|enumerate|, |itemize|, etc.)
% \item[date] Logically, commands concerning dates.
% \item[ligatures] A way to provide ligatures, i.e., shorthands for accented
% letters, and other special symbols.
% \item[text] Modifications of some typografical conventions: spacing
% before o after characters (French `:' or `;', Spanish `\%'), etc.
% \item[tools] Supplemetary command definitions. 
% \end{description}
% These groups belong to two categories: names, layout, and date are
% considered \emph{document} groups, since they are intended for
% formatting the document; ligatures, tools and text, are considered
% \emph{text} groups, since you will want to use them temporarily
% for a short text in some language.
% 
% \subsection{Selecting a Language}
%
% You must load languages with |\usepackage|:
% \begin{sample}
% |\usepackage[english,spanish]{polyglot}|
% \end{sample}
% 
% Global options (those of  |\documentclass|) will be recognized as usual.
% The very first language will be automatically
% selected (with the starred version, see below).
% For this purpose, class options  are taken in consideration \emph{before} package
% options. (Perhaps this is not the usual convention in \LaTeXe, but it is
% more logical; in general, you will state the main language as class option
% and the secondary ones as package options.) You can cancel this automatic
% selection with the package option |loadonly|:
% \begin{sample}
% |\usepackage[german,spanish,loadonly]{polyglot}|
% \end{sample}
%
% \begin{cmd}
% |\selectlanguage*[<no-groups>]{<language>}|
% \end{cmd}
%
% Selects all groups from <language>, except if there is the optional
% argument. <no-groups> is a list of groups \emph{not} to be selected, in the
% form |no<group>,no<group>,...|. For instance, if you dislike
% using ligatures:
% \begin{sample}
% |\selectlanguage*[noligatures]{french}|
% \end{sample}
% This command also sets defaults to be used by the
% unstarred version (see below).
%
% \begin{cmd}
% |\selectlanguage[<groups>]{<language>}|
% \end{cmd}
%
% Where <groups> is a list of <group>s and/or |no<group>|s.
%
% There are three posibilities:
% \begin{itemize}
% \item When a group is cited in the form <group>
% (e.g., |date| or |names|), this group is selected
% for the current language, i.e., that of the current
% |\selectlanguage|.
%
% \item When a group is cited in the form |no<group>|
% (e.g., |nodate| or |nonames|), this group is cancelled.
%
% \item When a group is not cited, then the default, i.e., that
% set by the |\selectlanguage*| in force, is used; it can be
% a |no|-form, and then the group will be disabled if
% necessary. If there is
% no a previous starred selection, the |no|-form will be
% presumed in all of groups.
% \end{itemize}
% If no optional argument is given, then |text,ligatures,tools| are
% presumed. Thus, at most two languages are selected at time. 
%
% Here is an example:
% \begin{sample}
% |\selectlanguage*[noligatures]{spanish}|\\
% Now we have Spanish |names|, |date|, |layout|, |text| and |tools|,\\
% but no |ligatures| at all.\\
% |\selectlanguage[date,notools]{french}|\\
% Now we have Spanish |names|, |layout| and |text|, French |date|,\\
% but neither |ligatures| nor |tools|.\\
% |\selectlanguage[ligatures]{german}|\\
% Now we have Spanish |names|, |date|, |layout|, |text| and |tools|,\\
% and German |ligatures|.\\
% \end{sample}
%
% Selection is always local. There is also the possibility
% to use |selectlanguage| as environment. 
% \begin{sample}
% |\begin{selectlanguage}*[<no-groups>]{<language>} <text> \end{selectlanguage}|\\
% |\begin{selectlanguage}[<groups>]{<language>} <text> \end{selectlanguage}|
% \end{sample}
%
% In general, you will use these commands without the optional
% argument---the starred version as command at the very
% beginning of documents and the starless version as environment
% in a temporary change of language.
%
% For the sake of clarity, spaces are ignored in the optional
% argument, so that you can write 
% \begin{sample}
% |\selectlanguage[date, tools, no ligatures]{spanish}|
% \end{sample}
%
% \begin{cmd}
% |\uselanguage[<groups>]{<language>}{<text>}|
% \end{cmd}
%
% A command version of the |selectlanguage| environment, although only
% the unstarred version is allowed.
%
% \begin{cmd}
% |\unselectlanguage|
% \end{cmd}
% 
% You can switch all of groups off
% with |\unselectlanguage|. Thus, you return to the
% original \LaTeX{} as far as \textsf{polyglot}
% is concerned.\footnote{Well, not exactly. But you
%   should not notice it at all.}
% 
% A typical file will look like this
% \begin{sample}
% |\documentclass[german]{...}|\\
% |\usepackage[spanish]{polyglot}|\\
% |\newenvironment{spanish}%|\\
% |  {\begin{quote}\begin{selectlanguage}{spanish}}%|\\
% |  {\end{selectlanguage}\end{quote}}|\\
% |\begin{document}|\\
% |Deutsche text|\\
% |\begin{spanish}|\\
% |  Texto en espa'nol|\\
% |\end{spanish}|\\
% |Deutsche text|\\
% |\end{document}|\\
% \end{sample}
%
% Note that |\selectlanguage*{german}| is not necessary, since
% it is selected by the package. (Because there is no |loadonly|
% package option.)
% 
% \subsection{Tools}
%
% \begin{cmd}
% |\thelanguage|
% \end{cmd}
% 
% The name of the current language. You \emph{cannot} redefine this command
% in a document.
%
% \begin{cmd}
% |\languagelist|
% \end{cmd}
%
% A list of languages requested.
%
% \begin{cmd}
% |\allowhyphens|
% \end{cmd}
%
% Allows further hyphenation in words with the primitive |\accent|.
%
% \begin{cmd}
% |\hexnumber  \octalnumber  \charnumber|
% \end{cmd}
%
% Synonymous to |"|, |'| and |`| for numbers (|\hexnumber F3| $=$ |"F3|)
% provided for convenience. 
%
% \begin{cmd}
% |\nofiles|
% \end{cmd}
%
% Not a new command really, but it has been reimplemented to optimize 
% some internal macros related with file writing.
%
% \begin{cmd}
% |\ensurelanguage|
% \end{cmd}
%
% The \textsf{polyglot} package modifies internally some 
% \LaTeX{} commands in order to do its best for making
% sure the current language is used
% in a head/foot line, even if the page is shipped out when another
% language is in force.
% Take for instance
% \begin{sample}
% (With |\selectlanguage*{spanish}|)\\
% |\section{Sobre la confecci'on de t'itulos}|
% \end{sample}
% In this case, if the page is broken inside, say, a German text
% |\ensurelanguage| restores |spanish| and hence the accents.
%
% Yet, some non-standard classes or packages can modify the marks.
% Most of times (but not always) |\ensurelanguage| solves
% the problem:\,\footnote{For instance, it will not work when the line is 
%      automatically capitalized.}
%
% \begin{sample}
% (With |\selectlanguage*{spanish}|)\\
% |\section{\ensurelanguage Sobre la confecci'on de t'itulos}|
% \end{sample}
% In typeset, writing and other modes it's ignored.
%
% \begin{cmd}
% |\ensuredcommand{<cmd>}{<text>}|
% \end{cmd}
% 
% Saves the <text> in <cmd> so that you can
% use it in a ``ensured'' form later. Neither |\selectlanguage| nor |\uselanguage|
% can be passed in an argument---you must save the text with this command and then
% release it. The language is the
% current one. No arguments are allowed, and <text> is fragile, but
% a construction like |\uselanguage{...}{\ensuredcommand...}| is valid.
%
% Spanish |\chaptername| uses a |\'i| which is not
% set by standard |OT1| and hence is provided by |spanish.ot1|.
% If you use |\chaptername| in a text with, say, the German |text|
% group you will get an accented dotted i. In this
% case, you can use |\ensuredcommand|.
% 
% \subsection{Extensions}
%
% The \textsf{polyglot} package provides \emph{extensions}
% so that its functionality will be extended to and from
% some package with the help of some commands
% in the |polyglot.cfg| file,
% sometimes with automatic loading. At the current moment,
% only font enconding extensions
% are implemented, which are automatically taken care of.
% (In future releases, you will be allowed
% to by-pass the current default settings, and add further extensions.)
%
% \subsubsection{Font Encoding Extensions}
% 
% With the help of this extension, you are allowed 
% to change some commands depending of font
% encodings. When a language is loaded, the package reads
% automatically the files named |<language-file>.<ENC>|,
% if they exist, where <language-file> is the exact name of the
% file |.ld| which the language is defined in, and <ENC>
% is the name of encodings declared. So, for instance, if you
% have preinstalled |T1| (besides |OMS|, etc.) and:
% \begin{sample}
% |\documentclass{article}|\\
% |\usepackage[LXX,OT1]{fontenc}|\\
% |\usepackage[spanish,german]{polyglot}|
% \end{sample}
% the following files will be read \emph{if they exist} (if not, nothing
% happens):
% \begin{sample}
% |spanish.t1   spanish.ot1  spanish.lxx  spanish.oms  |etc.\\
% | german.t1    german.ot1   german.lxx   german.oms  |etc.
% \end{sample}
% So, for example, |german.ot1| redefines some umlauted vowels in order to
% modify the accent position and to allow hyphenation.
% 
% Loading of these files may be inhibited by means of the option
% |nofontenc| when calling \textsf{polyglot}:
% \begin{sample}
% |\usepackage[spanish,german,nofontenc]{polyglot}|
% \end{sample}
% 
% \section{Developpers Commands}
%
% Some command names could seem inconsistent with that of the user commands. In
% particular, when you refer to a language in a document you are referring in fact
% to a dialect, which belogs to a language. As user, you cannot access to a 
% language and you instead access to a dialect named like the language.
% From now on, when we say ``current language'' we mean
% ``current language or dialect.'' For more details on dialects, see below.
%
% \subsection{General}
% 
% \begin{cmd}
% |\DeclareLanguage{<language>}|
% \end{cmd}
% 
% The first command in the |.ld| must be this one. Files don't always
% have the same name as the language, so this command makes things work.
% 
% \begin{cmd}
% |\DeclareLanguageCommand{<cmd-name>}{<group>}[<num-arg>][<default>]{<definition>}|
% \end{cmd}
% 
% Stores a command in a group of the current language. The definition will be
% activated when the group is selected, and the old definition, if any,
% will be stored for later recovery if the group is switched off.
% 
% There is a point to note (which applies also to the next commands).
% Suppose the following code:
% \begin{sample}
% At |spanish.ld|:\\
% |\DeclareLanguageCommand{\partname}{names}{Parte}|\\
% | |\\
% At document:\\
% |\selectlanguage*{spanish}|\\
% |\renewcommand{\partname}{Libro}|\\
% |\selectlanguage*{english}|\\
% |\partname|
% \end{sample}
% Obviously, at this point |\partname| is `Part'. But if you follow with
% \begin{sample}
% |\selectlanguage*{spanish}|\\
% |\partname|
% \end{sample}
% Surprise! \emph{Your} value of |\partname|, i.e., `Libro', is recovered. So
% you can customize easily these macros in your document, even if you switch
% back and forth between languages.
% 
% \begin{cmd}
% |\SetLanguageVariable{<var-name>}{<group>}{<value>}|
% \end{cmd}
% 
% Here <var-name> stands for the internal name of a counter or a lenght as
% defined by |\newconter| (|\c@...|) or |\newlenght|. The variable must be
% already defined. When the group is selected, the new value will be assigned to
% the variable, and the old one will be stored.
% 
% \begin{cmd}
% |\SetLanguageCode{<code-cmd>}{<group>}{<char-num>}{<value>}|
% \end{cmd}
% 
% Similar to |\SetLanguageVariable| but for codes. For instance:
% \begin{sample}
% |\SetLanguageCode{\sfcode}{text}{`.}{1000}|
% \end{sample}
%
% Languages with |\frenchspacing| should set the |\sfcodes| with this command, so
% that a change with |\nonfrenchspacing| is recovered after a switch.
% 
% \begin{cmd}
% |\DeclareLanguageSymbolCommand{<char>}{<group>}[<num-arg>][<default>]{<definition>}|
% \end{cmd}
% 
% First makes <char> active and then defines it just as
% |\DeclareLanguageCommand| does.
% 
% For instance, in French:
% \begin{sample}
% |\DeclareLanguageSymbolCommand{:}{spacing}{\unskip\,:}|
% \end{sample}
% Note that this command \emph{does not} change the category yet,
% and hence the previous command is valid. (Well, except if the |:|
% is already active. If you want to avoid any infinite loop, you can just
% say |{\unskip\,\string:}|).
%
% \begin{cmd}
% |\UpdateSpecial{<char>}|
% \end{cmd}
%
% Updates |\dospecials| and |\@sanitize|. First removes <char>
% from both lists; then adds it if it has categorie
% other than `other' or `letter'. With this method we avoid duplicated
% entries, as well as removing a character being usually special (for
% instance |~|).
%
% \subsection{Groups}
%
% \begin{cmd}
% |\DeclareLanguageGroup{<group>}|\\
% |\DeclareLanguageGroup*{<group>}|
% \end{cmd}
%
% Adds a new group. With the starred version, the group will be
% considered a |text| group, and hence included in the default
% of |\selectlanguage|.  Group names cannot begin with |no|
% because of the |no|-form convention given above.
% 
% \subsection{Ligatures}
% 
% \begin{cmd}
% |\DeclareLigatureCommand{<char-1>}{<char-2>}{<definition>}|
% \end{cmd}
% 
% Makes the pair |<char-1><char-2>| a shorthand for <definition>.
% For instance:
% \begin{sample}
% |\DeclareLigatureCommand{"}{u}{\"u}|
% \end{sample}
% There are some points to note:
% \begin{itemize}
% \item Spaces between <char-1> and <char-2> are not significant. So
% |"u| and \verb*=" u= will give the same result. Be careful then.
% \item If <char-1> is followed by a char which there is no
% ligature for, the original value (i.e., the value of <char-1> at
% |\selectlanguage|) is used.
%
% \item If <char-1> is followed by an ``argument'' which is
% empty or with more
% than one token, its original value is also used. This is very important
% in order to break ligatures. In German, you get `\,''und' with
% |"{}und| or |"{und}|; in French, you get `C'est' with
% |C'{}est| or |C'{est}|; in Spanish, you write 
% a non-breaking space before `n' with |~{}n|, and before `\~n', with
% |~{~n}| or |~~n|. If some package (there are in fact a lot) has defined,
% say, |^| with  some special function which requires an argument,
% this will be keept in most of cases (multi-token arguments), even
% when we define |^| as a ligature; if the argument has only a token
% you may trick the |^| with, say, |^{e{}}| (two tokens, `e' and
% empty). In math mode, the original math meaning is used
% (even if the |\mathcode| was |"8000|).
%
% \item Some languages, such as Spanish, make active \lc{} and \rc{} in
% order to
% declare the ligatures \lc\lc{} and \rc\rc{} for guillemets. If you define
% macros testing some counters or lengths are lesser or greater,
% load the package with option |loadonly| and select the language
% after these commands.
%
% \item Any definitionn attached to a 
% ligature char is preserved, even if not
% currently `active'. This makes switching a language very
% robust.\footnote{Don't try to change the catcode of a char to `other'
%   before selecting a language
%   in order to `lost' its definition---that won't work. Instead,
%   let it to relax if you really need it.
%   (In fact, \textsf{polyglot} uses three internal variables, the
%   first storing the text value, the second the
%   math value, and the third the definition, which is used
%   when the catcode was `active' or the mathcode was |"8000|.)}
%
% \item Yet, an error is given 
% when a ligature char has certain character codes
% at the selection, namely
% `escape' (|\|), `open' (|{|), `close' (|}|),
% `return' (|^^M|) or `comment' (|%|).
% This is a very unlikely situation and for the moment
% there is nothing I can do. Furthermore, `invalid' chars
% are validated (as `other') in the scope of the language.
% \end{itemize}
% 
% \begin{cmd}
% |\DeclareLigature{<char-1>}|
% \end{cmd}
% In some instances, you might wish to declare a ligature char before
% |\DeclareLigatureCommand| (which has an implicit |\DeclareLigature|).
% (I wonder when, but here it is.)
%
% \subsection{Dates}
% 
% \begin{cmd}
% |\DeclareDateFunction{<date-function-name>}{<definition>}|\\
% |\DeclareDateFunctionDefault{<date-function-name>}{<definition>}|\\
% |\DeclareDateCommand{<cmd-name>}{<definition>}|
% \end{cmd}
% By means of |\DeclareDateCommand| you can define commands like |\today|. The
% good news is that a special syntax is allowed in <definition> with date
% functions called with \lc<date-funtion-name>\rc. Here
% \lc<date-funtion-name>\rc{} stands for the definition given in
% |\DeclareDateFunction| for the current language.  If no such function for
% the language is given then the definition of |\DeclareDateFunctionDefault|
% is used. See |english.ld| for a very illustrative example. (The 
% \lc{} and \rc{} are  actual lesser and greater signs.)
% 
% Predeclared functions (with |\DeclareDateFunctionDefault|) are:
% \begin{itemize}
% \item \lc|d|\rc{} one or two digits day: 1, 2, \dots, 30, 31.
% \item \lc|dd|\rc{} two digits day: 01, 02, \dots
% \item \lc|m|\rc{} one or two digits month.
% \item \lc|mm|\rc{} two digits month.
% \item \lc|yy|\rc{} two digits year: 96, 97, 98, \dots
% \item \lc|yyyy|\rc{} four digits year: 1996, 1997, 1998, \dots
% \end{itemize}
% 
% Functions which are not predeclared, and hence should be declared
% by the |.ld| file, are:
% 
% \begin{itemize}
% \item \lc|www|\rc{} short weekday: mon., tue., wes., \dots
% \item \lc|wwww|\rc{} weekday in full: Monday, Tuesday, \dots
% \item \lc|mmm|\rc{} short month: jan., feb., mar., \dots
% \item \lc|mmmm|\rc{} month in full: January, February, \dots
% \end{itemize}
% 
% The counter |\weekday| (also |\value{weekday}|) gives a number between 1
% and 7 for Sunday, Monday, etc.
% 
% For instance:
% \begin{sample}
% |\DeclareDateFunction{wwww}{\ifcase\weekday\or Sunday\or Monday\or|\\
% |  Tuesday\or Wesneday\or Thursday\or Friday\or Saturday\fi}|\\
% |\DeclareDateCommand{\weektoday}{|\lc|wwww|\rc
%    |, |\lc|mmmm|\rc| |\lc|dd|\rc| |\lc|yyyy|\rc|}|
% \end{sample}
% 
% \subsection{Dialects}
%
% As stated above, |\selectlanguage| access to dialects rather than 
% languages. |\DeclareLanguage| declares both a language and a dialect
% with the same name, and selects the actual language.
%
% \begin{cmd}
% |\DeclareDialect{<dialect>}|\\
% |\SetDialect{<dialect>}|\\
% |\SetLanguage{<language>}|
% \end{cmd}
%
% |\DeclareDialect| declares a dialect, which shares all
% of declarations for the current actual language.
% With |\SetDialect| you set the dialect so that new declarations
% will belong only to
% this dialect. |\DeclareDialect| just declares but does not set it.
% 
% A dialect with the same name as the language is always implicit.
% You can handle this dialect exactly as any other dialect.
% In other words, after setting the dialect, new 
% declarations belong only to this one. If you want to return to the
% actual language, so that
% new declarations will be shared by all dialects, use |\SetLanguage|.
%
% Note that commands and variables declared for a language are
% set by |\selectlanguage| before those of dialects,
% doesn't matter the order you declared it.
%
% For example:
% \begin{sample}
% |\DeclareLanguage{english}|\\
% |\DeclareDialect{american}|\\
% Declarations\\
% |\SetDialect{english}|\\
% |\DeclareDateCommmand{\today}{...}|\\
% |\SetDialect{american}|\\
% |\DeclareDateCommand{\today}{...}|\\
% |\SetLanguage{english}|\\
% More declarations shared by both |english| and |american|
% \end{sample}
%
% \subsection{Font Encoding}
% 
% \begin{cmd}
% |\DeclareLanguageCompositeCommand{<cmd>}{<encoding>}{<letter>}|%
%                                 |[<arg-num>][<default>]{<definition>}|\\
% |\DeclareLanguageTextCommand{<cmd>}{<encoding>}|%
%                            |[<arg-num>][<default>]{<definition>}|
% \end{cmd}
% 
% The equivalent to |\DeclareTextCompositeCommand|
% and |\DeclareTextCommand| in this package. (That's
% all.) New values are
% stored in the |text| group. No default versions are provided;
% default values may be assigned to the |?| encoding.
% 
% A typical file looks like:
% \begin{sample}
% |\ProvidesFile{spanish.ot1}|\\
% |\def\spanish@acute#1{\allowhyphens\accent19 #1\allowhyphens}|\\
% |\DeclareLanguageCompositeCommand{\'}{OT1}{a}{\spanish@acute a}|\\
% |\DeclareLanguageCompositeCommand{\'}{OT1}{A}{\spanish@acute A}|\\
% (And so on.)
% \end{sample}
% 
% You may add files
% for your local encodings, and you can modify the files supplied
% with the package (mainly for |OT1|) provided you state clearly at
% their beginning the changes and does not distribute them as part of
% the \textsf{polyglot} package.
%
% We note a very important point here. Perhaps |\DeclareLanguageCompositeCommand|
% change the internal definition of <cmd> which is not recovered after
% you exit from a language. You will not notice that in a document at all, but if
% you are trying to access to the original value you must do it before
% selecting the language for the first time.
% Yet, if <cmd> has a particular definition declared for
% this language, the old value \emph{will} be restored, as you can expect.
% 
% |\PreLoadLanguage| loads
% these files always, but you cannot
% |\PreLoadLanguage|s with these encoding extensions for encoding schemes
% not preloaded. (As far as \LaTeX\ is concerned, they don't
% exist yet!) If you want them, there are two tactics:
% \begin{enumerate}
% \item  Preloading the encoding in the corresponding |.cfg|
% \LaTeXe\ file. Then both encoding a the language extensions to it are
% directly available in your \LaTeX\ instalation.
% \item Accepting the fact these files
% will be loaded when typesetting the
% document.
% \end{enumerate}
% In order to avoid a new loading, you must
% move the files preloaded to a folder where \LaTeX\ cannot find them.
% 
% \subsection{Interaction with Classes}
%
% \begin{cmd}
% |\pg@no<group>|\\
% (i.e. |\pg@nonames|, |\pg@nolayout|, |\pg@noligatures|, etc.)
% \end{cmd}
%
% Initially, these commands are not defined, but if they are, the
% corresponding groups are not loaded. This mechanism is intended
% for classes designed for a certain publication and with a
% very concrete layout which we don't want to be changed.
% You simply write in the class file
% \begin{sample}
% |\newcommand{\pg@nolayout}{}|
% \end{sample} 
% Note this procedure does not ever load the group---if
% you select it nothing happens.
%
% \section{Configuration}
% 
% There \emph{must} be a file called |polyglot.cfg| which will define the
% configuration for your system. This file consists of two parts, the first
% one being used by the installation procedure, and the second one mainly by
% |\usepackage|. When compilling \LaTeX, you must load in some point the file
% |polyglot.ltx| if you want to install hyphenation patterns other than
% English.
% 
% \begin{cmd}
% |\PreLoadPatterns{<patterns>}{<hyphens-file>}|
% \end{cmd}
% Defines a new language with the patterns in <hyphens-file>. Internally,
% these languages will be referred to as |\pgh@<language>|. 
% 
% \begin{cmd}
% |\PreLoadLanguage{<language>}[<file>]{<patterns>}{<more>}|
% \end{cmd}
% Loads a language \emph{at the time of installation} so that the
% language has not to be read by |\usepackage|. Even more, if you only
% want preloaded languages, you needn't call |polyglot.sty| in your
% document at all. The file read is |<file>.ld|
% or, if no optional argument, |<language>.ld|; and <patterns> has to be any
% set preloaded with |\PreLoadPatterns|. This command also preloads
% |polyglot.def|. In the part <more> you may insert commands just between
% the loading of the |.ld| file and  the
% final settings made by the command.
%
% In running
% time, this command is ignored.
% 
% \begin{cmd}
% |\PreLoadPolyGlot|
% \end{cmd}
% Preloads |polyglot.def|, so that the style is directly available
% in your system.
% 
% \begin{cmd}
% |\LoadLanguage{<language>}[<file>]{<patterns>}{<more>}|
% \end{cmd}
% Quite similar to |\PreLoadLanguage| but in running time (i.e., when read
% with |\usepackage|). In installation time
% this command is ignored.
% 
% \begin{cmd}
% |\LanguagePath{<file-path>}|
% \end{cmd}
% 
% Add a path to |\input@path|. (See |ltdirchk| and the
% \emph{Local Guide} for details.) If \textsf{polyglot} has been installed,
% this command will be ignored in running time. 
% 
% This is a tipical |polyglot.cfg|:
% \begin{sample}
% |\PreLoadPatterns{english}{hyphen.tex}|\\
% |\PreLoadPatterns{spanish}{sphyph.tex}|\\
% | |\\
% |\LoadLanguage{english}{english}|\\
% |\LoadLanguage{spanish}{spanish}|\\
% |\LoadLanguage{german}{english}|\\
% |\LoadLanguage{austrian}[german]{english}|\\
% |\LoadLanguage{french}{spanish}|
% \end{sample}
%
% \section{Installation}
%
% If you want install the package in your basic \LaTeX\ configuration,
% follow these steps:
% \begin{enumerate}
% \item First, run |polyglot.ins|. The files |polyglot.sty|,
% |polyglot.ltx|, |polyglot.def| and |polyglot.cfg| are generated.
% \item Create a file named |hyphen.cfg| which has to include the line
% \begin{sample}
% |\input{polyglot.ltx}|
% \end{sample}
% You may also rename |polyglot.ltx| as |hyphen.cfg|.
% \item Move the package and hyphen files where \LaTeX\ can find them.
% \item Modify |polyglot.cfg| following your requirements. At this
% stage, only |\LanguagePath|, |\PreLoadPatterns|, |\PreLoadPolyGlot|,
% and |\PreLoadLanguage| are mandatory, provided you want them and you
% won't remove nor modify later at all the |\PreLoadLanguage| commands.
% (Once installed
% you are allowed to add |\LoadLanguage|s to this file freely.)
% \item Run |latex.ltx|.
% \item If installation didn't failed, remove |polyglot.ltx| and
% |polyglot.def| as they are no longer needed.
% \end{enumerate}
%
% \section{Customization}
%
% ``Well, but I dislike |spanish.ld|.''
% You can customize easily a language once
% loaded, with new commands or by redefininig the existing ones. 
% \begin{itemize}
% \item If want to redefine a language command, simply select the
% group of this language which defines it
% (as with |\selectlanguage*|) and then
% redefine it with |\renewcommand|.
% \item If, in turn, you want to define a
% new command for a language, first
% make sure no language is selected
% (for instance, with |\unselectlanguage|).
% Then |\SetLanguage| and use the declaration
% commands provided by the
% package and described above.
% \end{itemize}
%
% A further step is creating a new file,
% by copying it, modifying the commands
% and, of course, renaming the file! Or with
% a file with extension |.ld| as:
% \begin{sample}
% |\ProvidesFile{...ld}|\\
% |\input{...ld} | the file you want customize\\
% |\selectlanguage*{...}|\\
% Commands to be redefined\\
% |\unselectlanguage|\\
% |\SetLanguage{...}|\\
% Commands to be created
% \end{sample}
% Then, add a line to |polyglot.cfg| with |\LoadLanguage|...
%
% \section{Errors}
%
% \begin{cmd}
% |Unknown group|
% \end{cmd}
% 
% You are trying to assign a command to an inexistent group.
% Perhaps you have misspelled it.
%
% \begin{cmd}
% |Missing language file|
% \end{cmd}
%
% Probable causes:
% \begin{itemize}
% \item Wrong configuration, |\PreLoadLanguage|
%       instead of |\LoadLanguage| with a language
%       not preloaded.
%
% \item The corresponding |.ld| file is missing.
%
% \item Wrong path.
% \end{itemize}
%
% \begin{cmd}
% |Unknown language|
% \end{cmd}
%
% You forgot requesting it. Note that dialects
% stand apart from languages; i.e., you have no
% access to |austrian| just requesting |german|.
%
% \begin{cmd}
% |Invalid option skipped|
% \end{cmd}
%
% In the starred version of |\selectlanguage|
% you must use the |no|-forms only.
%
% \begin{cmd}
% |Bad ligature|
% \end{cmd}
%
% A very unlikely error which is generated by a bug in the
% description file of the current
% language, but also if some package has changed some categories
% to very, very extrange values.
%
% \begin{cmd}
% |Bug found (<n>)|
% \end{cmd}
%
% You will only find this error when using
% the developpers commands---never with the user ones---or if
% there is a bug in description files
% written by others. In the latter case, contact
% with the author. The meaning of the parameter <n> is
% \begin{enumerate}
% \item \emph{Unknown group in declaration.} The group hasn't been
%       declared. Perhaps you misspelled it.
%
% \item \emph{Invalid group name.} Group names cannot begin
%        with |no| to avoid mistakes when disabling a group.
%
% \item \emph{Declaration clash.} You are trying to
%       redeclare a command or to set a new
%       value to a variable for this language.
%       If you want redefine it, select the language and simply
%       use |\renewcommand| (or |\set..|).
%       If you intend to define a new command
%       for the language, sorry, you must change its name.
%
% \item \emph{Invalid language/dialect setting.} Generated by
%       |\SetLanguage| or |\SetDialect| when the argument is not
%       a declared language/dialect.
% \end{enumerate}
% 
% Now we describe how polyglot generates \TeX{} 
% and \LaTeX{} errors because of intrinsic syntax
% problems.
%
% \begin{cmd}
% |Argument of \pg@text@ligature has an extra }|
% \end{cmd}
% 
% An usual problem with ligatures because the second
% letter is taken as a macro argument. If the first
% letter, for instance |"| in German, is followed
% by a |}| then |TeX| gets confused. For instance,
% \begin{sample}
% (Current language is German in full.)\\
% |\uselanguage[date]{english}{\emph{``\today"}}|
% \end{sample}
%
% Note that German ligatures are active. Fix:
% type |{}| just after |"|. (A ligature just before
% the closing |}| in |\uselanguage| is OK.)
%
% \begin{cmd}
% |TeX capacity exceeded, sorry [save_size=<n>]|
% \end{cmd}
%
% You are using to many ungrouped languages.
% Fix: use the environment version of |selectlanguage|
% or alternatively use |\unselectlanguage| before
% a new |\selectlanguage|.
%
% \section{Conventions}
% 
% I strongly encourage the following conventions:
% \begin{itemize}
% \item Concerning dates, see above.
%
% \item There must be a main ligature, which will precede some special
% features. For instance, the obvious main ligature in German is |"|, and
% so we have \verb=""=, |"-|, |"ck|, etc.
%
% \item Default values for encodings, in the |.ld| file. 
%
% \item A mandatory convention. The language names must consist of
% lowercase letters.
% 
% \item Don't name your own language files with the name of some language.
% I'd like to reserve these to official descriptions,
% supported by myself or a group.
% \end{itemize}
%
% \section{Languages}
% 
% Now follows a brief description of the languages provided. All of them
% include the group |names| and |\today| in |date|.
% 
% \subsection{\texttt{american}}
% 
% |english| dialect. Same as English with date in American format.
% 
% \subsection{\texttt{austrian}}
% 
% |german| dialect. Same as German with ``January'' in Austrian.
% 
% \subsection{\texttt{english}}
% 
% \begin{description}
% \item[date] Functions \lc|ddd|\rc{} (ordinal date), \lc|mmm|\rc,
% \lc|mmmm|\rc{}, \lc|www|\rc{} and \lc|wwww|\rc.
% \item[tools] |\arabicth{<counter>}|: ordinal form of <counter>.
% \end{description}
% 
% \subsection{\texttt{french}}
% 
% \begin{description}
% \item[date] Functions \lc|ddd|\rc{} (ordinal first), 
% \lc|mmmm|\rc{} and \lc|wwww|\rc{}.
% \item[layout] |itemize| with |--|. Paragraph after section
% indented.
% \item[ligatures] Accents |`a|, |`e|, |`u|, |'e|, |^a|,
% |^e|, |^i|, |^o|, |^u|, |"y|, |"e| and |/c| (also uppercase).
% Composite letters |"ae| and |"oe| (also uppercase).
% Guillemets with automatic
% spacing \lc\lc{} and \rc\rc. 
% \item[text] |OT1| guillemets and accents. Spaces before
% double punctuation signs. French spacing.
% \item[tools] |\sptext{<text>}|: small and raised text for
% abbreviations.
% \end{description}
% 
% \subsection{\texttt{german}}
% 
% \begin{description}
% \item[date] Functions \lc|mmm|\rc,
% \lc|mmm|\rc, \lc|www|\rc{} and \lc|wwww|\rc.
% \item[ligatures] Accents |"a|, |"e|, |"i|, |"o|,
% |"u| (also uppercase). Scharfes s |"s| and |"S|.
% Hyphenation |"ck|, |"ff|, |"ll|, etc. German quotes
% |"`| and |"'|. Guillemets |"|\lc{} and |"|\rc. Other |"-|,
% {\ttfamily\string"\string|} and |""|.
% \item[text] |OT1| guillemets, german quotes and accents 
% (with lower umlauts in a, o and u). 
% \end{description}
% 
% \subsection{\texttt{spanish}}
% 
% A new group |math| is defined for some functions names: |\lim|
% giving l\'\i m, |\tg|, etc.
% 
% \begin{description}
% \item[date] Functions \lc|mmm|\rc,
% \lc|mmmm|\rc, \lc|www|\rc{} and \lc|wwww|\rc.
% \item[layout] |itemize| with |---|. |enumerate| with ``1.'',
% ``\emph{a})'', ``1)'', ``\emph{a}$'$)''. |\fnsymbol|
% produces one, two, three\ldots{} asteriscs. |\alpha|
% includes the letter ``\~n''.
% \item[ligatures] Accents |'a|, |'e|, |'i|, |'o|,
% |'u|, |"u|, |'c| (\c{c}), |~n| $=$ |'n| (also uppercase).
% Hyphenation |'rr| and |'-|.
% \item[text] |OT1| guillemets and accents. French
% spacing. Space before |\%|.
% \item[tools] |\sptext{<text>}|: small and raised text for
% abbreviations (the preceptive dot is included). |\...|
% for ellipsis.
% \end{description}
% 
% \section{Comming soon}
% 
% \begin{itemize}
% \item More extension facilities.
%
% \item Time, besides date, will be supported.
%
% \item Multiple ligatures (with three or more chars).
%
% \item Ligatures in index entries, which will
% provide automatic ``alphabetize as''.
% (By expanding, say, French |"oeil| into
% |oeil@\oe il| or a similar
% device.)
%
% \item Plain \TeX\ compatibility. (Not a priority.)
%
% \item Modularity, so that only required commands will be loaded. (Since this
% is one of the goals of \LaTeX3, is very unlikely I implement this
% point before the release of the new \LaTeX.)
%
% \item Releasing of unnecessary commands, if required. (For example, if
% you are going to use just a language.)
%
% \item More languages. (My goal is not to provide a large set of language files
% but rather a set of tools for users---\TeX{} groups in particular.
% I will answer to people asking for help, and of course 
% contributions will be credited.)
%
% \end{itemize}
%
% \StopEventually{}
%
%<*driver>
\documentclass{article}
\usepackage{doc}

\addtolength{\topmargin}{-3pc}
\addtolength{\textwidth}{6pc}
\addtolength{\oddsidemargin}{-2pc}
\addtolength{\textheight}{7pc}

\def\lc{\texttt{\string<}}
\def\rc{\texttt{\string>}}

\DeclareRobustCommand{\cs}[1]{\texttt{\char`\\#1}}

\newenvironment{cmd}%
  {\par\addvspace{4.5ex plus 1ex}%
    \vskip-\parskip\small
    \renewcommand{\arraystretch}{1.2}%
    \noindent\hspace{-\leftmargini}%
    \begin{tabular}{|l|}\hline\ignorespaces}
  {\\\hline\end{tabular}\nobreak\par\nobreak
    \vspace{2.3ex}\vskip-\parskip}

\newenvironment{sample}{\small\quote}{\endquote}

\catcode`>=11
\catcode`<=\active
\def<#1>{\textit{#1}}
\catcode`|=\active
\gdef|{\verb|\def<{|\syntx}}
\gdef\syntx#1>{\textit{#1}|}

\raggedright
\parindent1em
\nofiles
\OnlyDescription

\begin{document}
\DocInput{polyglot.dtx}
\end{document}

%</driver>
%
% \section{The File |polyglot.ltx|}
%
% Internal code is still evolving.
%
%    \begin{macrocode}
%<*install>
\count19=-1 % The language allocator

\def\LanguagePath#1{\edef\input@path{\input@path{#1}}}

%    \end{macrocode}
%
% The |\csname| trick by-passes the |\outer| checking.
%
%    \begin{macrocode} 

\def\PreLoadPatterns#1#2{%
  \csname newlanguage\expandafter\endcsname\csname pgh@#1\endcsname
  \language\csname pgh@#1\endcsname
  \input{#2}}

\def\SetPatterns#1#2{\expandafter\chardef
   \csname pgh@#1\endcsname#2\relax}

\def\PreLoadPolyGlot{%
  \ifx\pg@add@to\@undefined\input{polyglot.def}\fi}

\def\PreLoadLanguage#1{\PreLoadPolyGlot
  \@ifnextchar[{\pg@load{#1}}{\pg@load{#1}[#1]}}

\def\pg@load#1[#2]{\pg@input{#1}{#2}}

\def\pg@@{pg-}

\def\pg@input#1#2#3#4{%
    \pg@to@list{#1}%
    \@ifundefined{pgh@#1}%
      {\expandafter\let\csname pgh@#1\expandafter\endcsname
         \csname pgh@#3\endcsname%
       \PackageInfo{polyglot}%
         {#1 with #3 patterns\@gobble}}\@empty
    \@ifundefined{\pg@@?#1}%
      {\InputIfFileExists{#2.ld}{}%
         {\pg@err{Missing language file}}%
       \pg@extensions{#2}}\@empty
    \edef\thelanguage{\csname\pg@@?#1\endcsname}#4}

\def\LoadLanguage#1#2#{\@gobbletwo}

\input{polyglot.cfg}

\language\z@

\let\LanguagePath\@gobble

\let\PreLoadPatterns\@gobbletwo

\def\PreLoadLanguage#1{%
  \@ifnextchar[{\pg@preld{#1}}{\pg@preld{#1}[#1]}}
\def\pg@preld#1[#2]#3#4{%
  \DeclareOption{#1}{\@ifundefined{pge@#2}%
     {\pg@extensions{#2}\@namedef{pge@#2}{}}\@empty}}

\def\LoadLanguage#1{\@ifnextchar[{\pg@load{#1}}{\pg@load{#1}[#1]}}

\def\pg@load#1[#2]#3#4{%
   \DeclareOption{#1}{\pg@input{#1}{#2}{#3}{#4}}}

%</install>
%    \end{macrocode}
%
% \section{The Files |polyglot.sty| and |polyglot.def|}
%
% These files are quite similar.
%
%    \begin{macrocode}
%<*package>

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{polyglot}[1997/02/05 Multilingual LaTeX Package]

\DeclareOption{nofontenc}{\let\pg@extensions\@gobble}

\DeclareOption{loadonly}{\let\pg@sel@opt\relax}

%    \end{macrocode}
%
% If we preloaded |polyglot| only this short code will
% be executed. We simply test if |\pg@add@to| is defined. 
%
%    \begin{macrocode}

\ifx\pg@add@to\@undefined\else  % ie, if preloaded
  \input{polyglot.cfg}
  \ProcessOptions
  \pg@sel@opt
\expandafter\endinput\fi

%</package>
%    \end{macrocode}
%
% Some shortcuts to save memory, and initial settings.
%
%    \begin{macrocode}
%<*preload|package>

\chardef\f@@r=4
\newcount\pg@count

\def\pg@@{pg-}
\def\pg@@text{pg-text-}
\def\pg@@math{pg-math-}

\def\pg@flag#1{\csname\pg@@#1-flag\endcsname}
\def\pg@@flag#1{\pg@@#1-flag}

\def\pg@last{{}{}}

\def\pg@err#1{\PackageError{polyglot}{#1}%
  {See polyglot documentation for explanation}}

%    \end{macrocode}
%
% If there is |\nofiles|, some commands are
% canceled to save memory and speed up typesetting.
%
%    \begin{macrocode}

\AtBeginDocument{\if@filesw\else
  \def\pg@file@setup#1#2#3{}%
  \let\pg@file@write\@gobble
  \let\pg@file@ends\relax\fi}

\def\pg@bug@err#1{\pg@err{Bug found (#1)}}

%    \end{macrocode}
%
% \subsection{Internal Tools}
%
% Language commands are stored at commands in the
% form |pg-!<language>-<group>| or |pg-<language>-<group>|.
% The best way to
% understand them is with |\show|. The next command
% add something (implicit second argument) to a group (|#1|)
% of the current language (\thelanguage|)
%
%    \begin{macrocode}

\def\pg@add@to#1{%
  \@ifundefined{\pg@@flag{#1}}%
    {\pg@err{Unknown group}}
    {\@ifundefined{pg@no#1}%
      {\@ifundefined{\pg@@\thelanguage-#1}%
        {\expandafter\let\csname\pg@@\thelanguage-#1\endcsname\@empty}%
        \@empty
       \expandafter\pg@after
            \csname\pg@@\thelanguage-#1\expandafter\endcsname}\@gobble}}

\@onlypreamble\pg@add@to

\def\pg@after#1#2{%
  \expandafter\def\expandafter#1\expandafter{#1#2}}

\def\pg@to@list#1{\@ifundefined{languagelist}%
  {\edef\languagelist{#1}}%
  {\edef\languagelist{\languagelist,#1}}}

\@onlypreamble\pg@to@list

\def\pg@process#1#2{%
  \begingroup\edef\pg@a{\endgroup
    \noexpand\pg@xproc\noexpand#1#2,\noexpand\@nil,}\pg@a}
\def\pg@xproc#1#2,{\ifx\@nil#2\else
  #1{#2}\expandafter\pg@xproc\expandafter#1\fi}

\def\pg@idend#1{#1\egroup}

%    \end{macrocode}
%
% The following command returns |pg-#1-#2| (dialect form) if defined.
% If not, it returns |pg-!#1-#2| (language form). |#1| is either
% |\thelanguage| or |\pg@lig|.
%
%    \begin{macrocode}

\long\def\pg@cmd#1#2{%
  \pg@@
  \expandafter\ifx\csname\pg@@?#1\endcsname\relax#1%
    \else\expandafter\ifx\csname\pg@@#1-\string#2\endcsname\relax
      \csname\pg@@?#1\endcsname
    \else#1\fi\fi-\string#2}

%    \end{macrocode}
%
% \subsection{Selecting a language}
%
% |\pg@ifno| tests if |#1#2| is |no|.
%
%    \begin{macrocode}

\def\pg@ifno#1#2#3#4{%
  \if#1n\if#2o#3\else\@firstoftwo\fi\else#4\fi}

\def\pg@step{\afterassignment\pg@groups\def\pg@elt##1}

\def\selectlanguage{%
  \@ifstar{\pg@sel@i*}{\pg@sel@i{}}}

\def\pg@sel@i#1{\begingroup\catcode`\ =9 %
  \@ifnextchar[{\pg@sel@ii{#1}{}}{\pg@sel@ii{#1}{}[]}}

\def\pg@sel@ii#1#2[#3]#4{%
  \endgroup
  \if!#1!%
    \edef\pg@a{{\expandafter\@firstoftwo\pg@last}{[#3]{#4}}}%
  \else
    \def\pg@a{{[#3]{#4}}{}}%
  \fi
  \ifx\pg@a\pg@last\else
    \let\pg@last\pg@a
    \aftergroup\pg@file@ends
    \pg@file@setup{#1}{#3}{#4}%
    \pg@sel@iii#1[#3]{#4}%
  \fi#2}

\def\pg@sel@iii#1[#2]#3{%
  \@ifundefined{pgh@#3}%
    {\pg@err{Unknown language}\language\z@}%
    {\language\csname pgh@#3\endcsname}%
  \pg@step{\ifcase\pg@flag{##1}\or\or
    \@gobble\or\pg@disable{##1}\else
    \advance\pg@flag{##1}-\tw@\fi}%
  \let\thelanguage\pg@main
  \def\pg@elt##1{\pg@a##1\pg@a}%
  \if!#1!%
    \def\pg@a##1##2##3\pg@a{%
      \pg@ifno##1##2%
        {\pg@ifgroup{##3}{\advance\pg@flag{##3}\f@@r}}%
        {\pg@ifgroup{##1##2##3}%
          {\pg@after\pg@d{\pg@elt{##1##2##3}}%
           \advance\pg@flag{##1##2##3}\f@@r}}}%
    \let\pg@d\@empty
    \if!#2!\pg@text@groups\else
      \pg@process\pg@elt{#2}\fi
    \pg@step{\ifnum\pg@flag{##1}=\tw@\pg@enable{##1}\fi}%
    \pg@step{\ifnum\pg@flag{##1}=\f@@r\pg@disable{##1}\fi}%
    \pg@step{\pg@count\pg@flag{##1}%
      \pg@flag{##1}\ifnum\pg@count>\thr@@\f@@r\else\z@\fi
      \ifodd\pg@count\advance\pg@flag{##1}\@ne\fi}%
    \edef\thelanguage{#3}%
    \def\pg@elt##1{\pg@enable{##1}\advance\pg@flag{##1}-\tw@}%
    \pg@d\let\pg@d\@undefined
  \else
    \pg@step{\ifnum\pg@flag{##1}=\z@\pg@disable{##1}\fi}%
    \def\pg@elt##1{\pg@a##1\pg@a}%
    \if!#2!\else%
      \def\pg@a##1##2##3\pg@a{%
        \pg@ifno##1##2%
          {\pg@ifgroup{##3}{\advance\pg@flag{##3}-\@m}}% Just a mark
          {\pg@err{Invalid option skipped}{}}}%
      \pg@process\pg@elt{#2}%
    \fi
    \edef\pg@main{#3}%
    \edef\thelanguage{#3}%
    \pg@step{\ifnum\pg@flag{##1}<\z@\pg@flag{##1}\@ne
      \else\pg@flag{##1}\z@\pg@enable{##1}\fi}%
  \fi}

\def\uselanguage{\bgroup\begingroup\catcode`\ =9
   \@ifnextchar[{\pg@sel@ii{}\pg@idend}%
     {\pg@sel@ii{}\pg@idend[]}}

\def\unselectlanguage{\@ifstar{\selectlanguage[]{\pg@main}}%
  {\pg@unsel@i\pg@unsel@ii}}

\def\pg@unsel@i{%
  \c@pg@depth\z@\pg@file@ends
   \def\pg@elt##1{%
     \expandafter\let\csname\pg@@slot##1\endcsname\@empty}%
   \pg@elt{}\pg@streams}

\def\pg@unsel@ii{%
  \language\z@
  \pg@step{\ifcase\pg@flag{##1}\or\or
    \@gobble\or\pg@disable{##1}\fi}%
  \pg@step{\ifnum\pg@flag{##1}=\z@\pg@disable{##1}\fi}%
  \pg@step{\pg@flag{##1}\@ne}%
  \let\thelanguage\@empty\let\pg@main\@empty
  \def\pg@last{{}{}}}

\def\pg@enable#1{%
  \let\pg@switch\@firstoftwo
  \def\pg@group{#1}%
  \edef\pg@a{\csname\pg@@?\thelanguage\endcsname}%
  \expandafter\edef\csname\pg@@/#1\endcsname
      {\edef\noexpand\thelanguage{\pg@a}%
       \expandafter\noexpand\csname\pg@@\pg@a-#1\endcsname}%
  \def\pg@b##1\pg@b{%
    \let\thelanguage\pg@a
    \@nameuse{\pg@@\thelanguage-#1}%
    \edef\thelanguage{##1}%
    \expandafter\pg@after\csname\pg@@/#1\endcsname
      {\edef\thelanguage{##1}%
       \csname\pg@@##1-#1\endcsname}%
    \@nameuse{\pg@@\thelanguage-#1}}%
  \expandafter\pg@b\thelanguage\pg@b}

\def\pg@disable#1{%
  \let\pg@switch\@secondoftwo
  \csname\pg@@/#1\endcsname
  \expandafter\let\csname\pg@@/#1\endcsname\relax}

\def\pg@sel@opt{%
  \@tempswatrue
  \def\pg@d##1{\@ifundefined{pgh@##1}\@empty
      {\if@tempswa
       \PackageInfo{polyglot}%
             {Selecting document language:\MessageBreak
              ##1\@gobble}%
       \selectlanguage*{##1}\@tempswafalse\fi}}%
  \ifx\@classoptionslist\@empty\else
    \pg@process\pg@d\@classoptionslist\fi
  \ifx\@curroptions\@empty\else
    \if@tempswa\pg@process\pg@d\@curroptions\fi\fi
  \let\pg@d\@undefined}

\@onlypreamble\pg@sel@opt

%    \end{macrocode}
%
% \subsection{Wrinting to files}
%
%    \begin{macrocode}

\let\pg@immediate\relax

\def\pg@@slot{pg@slot@}

\def\pg@file@setup#1#2#3{%
  \advance\c@pg@depth\@ne
  \def\pg@elt##1{%
     \expandafter\pg@after\csname\pg@@slot##1\endcsname
       {\addtocounter{\pg@@slot\the\pg@count}\@ne
        \pg@immediate\write\pg@count
          {\pg@begin\noexpand\selectlanguage#1[#2]{#3}}}}%
  \pg@elt\@empty\pg@streams}

\def\pg@file@write#1{%
   \pg@count=#1\relax
   \@ifundefined{c@\pg@@slot\the\pg@count}%
     {\newcounter{\pg@@slot\the\pg@count}%
      \pg@slot@\let\pg@elt\relax
      \xdef\pg@streams{\pg@streams\pg@elt{\the\pg@count}}}{}%
     {\@nameuse{\pg@@slot\the\pg@count}}%
   \expandafter\gdef\csname\pg@@slot\the\pg@count\endcsname{}}

\def\pg@file@ends{%
  \def\pg@elt##1%
    {\ifnum\value{\pg@@slot##1}>\c@pg@depth
     \pg@immediate\write##1{\pg@end}%
     \addtocounter{\pg@@slot##1}\m@ne
     \expandafter\pg@elt\else\expandafter\@gobble\fi{##1}}%
  \pg@streams\let\pg@elt\relax}%

\let\pg@streams\@empty
\let\pg@slot@\@empty

\let\pg@begin\begingroup
\let\pg@end\endgroup

\newcounter{pg@depth}

\AtEndDocument{\unselectlanguage
  \let\pg@immediate\immediate}

%    \end{macromode}
%
% Now three \LaTeX\ commands are redefined. (Sad.) The
% first and the second to write a |\selectlanguage| if necessary.
% The third to sincronize |\bibitem| with the 
% language selection, since
% originally is |\immediate|.
%
%    \begin{macrocode}

\long\def\protected@write#1#2#3{%
  \pg@file@write{#1}%
  \begingroup
    \let\thepage\relax
    #2%
    \let\LanguageLigature\string
    \let\protect\@unexpandable@protect
    \edef\reserved@a{\write#1{#3}}%
    \reserved@a
    \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}

\long\def\@writefile#1#2{%
  \@ifundefined{tf@#1}\relax
    {\pg@file@write{\csname tf@#1\endcsname}%
     \@temptokena{#2}%
     \pg@immediate\write\csname tf@#1\endcsname{\the\@temptokena}}}

\def\@lbibitem[#1]#2{\item[\@biblabel{#1}\hfill]%
  \if@filesw
    \protected@write\@auxout{}%
      {\string\bibcite{#2}{\protect\pg@ensure\pg@last#1}}%
  \fi\ignorespaces}

\def\DeclareLanguageCommand#1#2{%
  \@ifundefined{\pg@cmd\thelanguage{#1}}%
   {\pg@add@to{#2}{\pg@switch@cmd#1}%
    \expandafter\newcommand
      \csname\pg@@\thelanguage-\string#1\endcsname}%
   {\pg@bug@err3}}

\@onlypreamble\DeclareLanguageCommand

\def\pg@switch@cmd#1{%
  \pg@switch
    {\ifx#1\@undefined
       \expandafter\pg@after
         \csname\pg@@/\pg@group\endcsname{\let#1\@undefined}%
     \fi}{}%
  \let\pg@c=#1%
  \expandafter\let\expandafter
    #1\csname\pg@@\thelanguage-\string#1\endcsname
  \expandafter\let
    \csname\pg@@\thelanguage-\string#1\endcsname=\pg@c}

\def\SetLanguageVariable#1#2{%
  \@ifundefined{\pg@cmd\thelanguage{#1}}%
   {\pg@add@to{#2}{\pg@switch@var{#1}}
    \@namedef{\pg@@\thelanguage-\string#1}}%
   {\pg@bug@err3}}

\@onlypreamble\SetLanguageVariable

\def\pg@switch@var#1{%
  \edef\pg@c{\the#1}%
  #1=\csname\pg@@\thelanguage-\string#1\endcsname\relax
  \expandafter\edef\csname\pg@@\thelanguage-\string#1\endcsname{\pg@c}}

%    \end{macrocode}
%
% \subsection{Special codes}
%
%    \begin{macrocode}

\def\SetLanguageCode#1#2#3{\pg@count=#3\relax
  \edef\pg@a{\noexpand\SetLanguageVariable
       {\noexpand#1\the\pg@count}}%
  \pg@a{#2}}

\@onlypreamble\SetLanguageCode

\begingroup
\catcode`~=\active
\gdef\DeclareLanguageSymbolCommand#1#2{%
  \SetLanguageCode{\catcode}{#2}{`#1}{\active}%
  \def\pg@a{#2}%
  \begingroup \lccode`~=`#1 \lccode`#1=`#1
  \lowercase{\endgroup
    \pg@add@to\pg@a{\UpdateSpecial{#1}}%
    \DeclareLanguageCommand{~}\pg@a}}
\endgroup

\@onlypreamble\DeclareLanguageSymbolCommand

%    \end{macrocode}
%
% \subsection{Declaring things}
%
%    \begin{macrocode}

\def\DeclareLanguage#1{%
  \def\thelanguage{!#1}%
  \@namedef{\pg@@?#1}{!#1}%
  \def\pg@main{!#1}}

\def\DeclareDialect#1{%
  \expandafter\let\csname\pg@@?#1\endcsname\pg@main}

\@onlypreamble\DeclareLanguage
\@onlypreamble\DeclareDialect

\def\SetLanguage#1{%
  \@ifundefined{\pg@@?#1}%
     {\pg@bug@err4}%
     {\edef\thelanguage{!#1}}}

\def\SetDialect#1{
  \@ifundefined{\pg@@?#1}%
     {\pg@bug@err4}%
     {\edef\thelanguage{#1}}}

\@onlypreamble\SetLanguage
\@onlypreamble\SetDialect

%    \end{macrocode}
%
% \subsection{Groups}
%
%    \begin{macrocode}

\def\pg@ifgroup#1{\@ifundefined{\pg@@flag{#1}}%
  {\pg@bug@err1\@gobble}}

\def\DeclareLanguageGroup{%
  \@ifstar{\pg@newgroup\pg@c}%
          {\pg@newgroup\pg@text@groups}}

\def\pg@newgroup#1#2{%
  \def\pg@a##1##2##3\pg@a{%
    \pg@ifno##1##2}%
  \pg@a#2\pg@a
    {\pg@bug@err2}%
    {\@ifundefined{\pg@@flag{#2}}%
       {\pg@after\pg@groups{\pg@elt{#2}}%
        \pg@after#1{\pg@elt{#2}}%
        \expandafter\newcount\csname\pg@@flag{#2}\endcsname
        \pg@flag{#2}\@ne}%
       {\PackageInfo{polyglot}%
         {Ignoring group declaration: #2.^^J%
          Already declared\@gobble}}}}

\@onlypreamble\DeclareLanguageGroup
\@onlypreamble\pg@newgroup

\let\pg@groups\@empty
\let\pg@text@groups\@empty
\let\pg@c\@empty

\DeclareLanguageGroup*{names}
\DeclareLanguageGroup*{layout}
\DeclareLanguageGroup*{date}

\DeclareLanguageGroup{ligatures}
\DeclareLanguageGroup{text}
\DeclareLanguageGroup{tools}

%    \end{macrocode}
%
% \section{Date}
%
%    \begin{macrocode}

\def\DeclareDateFunctionDefault#1{%
  \expandafter\providecommand\csname\pg@@ DF-#1\endcsname}

\def\DeclareDateFunction#1{%
  \expandafter\DeclareLanguageCommand
    \csname\pg@@ DF-#1\endcsname{date}}

\@onlypreamble\DeclareDateFunctionDefault
\@onlypreamble\DeclareDateFunction

\begingroup
\catcode`<=\active
\gdef\DeclareDateCommand#1{%
  \begingroup
  \let\protect\@unexpandable@protect
  \catcode`<=\active
  \def<##1>{\expandafter\noexpand\csname\pg@@ DF-##1\endcsname}%
  \def\pg@a{%
   \edef\pg@b{\endgroup%
    \noexpand\DeclareLanguageCommand{\noexpand#1}{date}{\pg@c}}
    \pg@b}%
  \afterassignment\pg@a\def\pg@c}
\endgroup

\@onlypreamble\DeclareDateCommand

\newcount\weekday

\let\c@day\day
\let\c@weekday\weekday
\let\c@month\month
\let\c@year\year

\def\SetWeekDay{\pg@count=\year\divide\pg@count4
  \weekday=\pg@count
  \multiply\pg@count4
  \advance\pg@count-\year
  \ifnum\pg@count=\z@
        \ifnum\month<\thr@@\advance\weekday -1\fi\fi
  \advance\weekday\year
  \advance\weekday-1899
  \advance\weekday\ifcase\month\or0 \or31 \or59 \or90 \or120
        \or151 \or181 \or212 \or243 \or293 \or304 \or334 \fi
  \advance\weekday\day
  \pg@count=\weekday
  \divide\pg@count7
  \multiply\pg@count7
  \advance\weekday-\pg@count
  \advance\weekday\@ne}

\SetWeekDay

\DeclareDateFunctionDefault{d}{\the\day}
\DeclareDateFunctionDefault{dd}{\ifnum\day<10 0\fi\the\day}

\DeclareDateFunctionDefault{m}{\the\month}
\DeclareDateFunctionDefault{mm}{\ifnum\month<10 0\fi\the\month}

\DeclareDateFunctionDefault{yy}{\expandafter\@gobbletwo\the\year}
\DeclareDateFunctionDefault{yyyy}{\the\year}

%    \end{macrocode}
%
% \subsection{Ligatures}
%
%    \begin{macrocode}

\def\pg@lig{\ifcase\pg@flag{ligatures}\pg@main\or\or
    \@gobble\or\thelanguage\fi}

\def\pg@cs@lig{%
  \ifmmode\expandafter\pg@math@ligature
  \else\expandafter\pg@text@ligature\fi}

\def\LanguageLigature{%
  \ifx\protect\@unexpandable@protect
    \expandafter\noexpand
  \else\ifx\protect\@typeset@protect
    \expandafter\expandafter\expandafter\pg@cs@lig
  \else\expandafter\expandafter\expandafter\protect
  \fi\fi}

\begingroup
\catcode`~=\active
\gdef\DeclareLigature#1{%
  \pg@add@to{ligatures}{\pg@switch{\pg@set@lig{#1}}{}}%
  \begingroup \lccode`~=`#1 \lccode`#1=`#1
  \lowercase{\endgroup
    \DeclareLanguageSymbolCommand{#1}{ligatures}%
             {\LanguageLigature~}}}
\endgroup

\@onlypreamble\DeclareLigature

%    \end{macrocode}
%\begin{verbatim}
%\def\DeclareLigatureSubtitution#1#2{%
%  \@ifundefined{\pg@@root}\@empty
%    {\pg@err{Ligature subtitution in dialect}%
%       {You cannot subtitute a ligature after^^J%
%        \string\GenerateDialects}}%
%  \pg@add@to{ligatures}%
%       {\@namedef{\pg@@text\string#1}{#2}}}
%\end{verbatim}
%
% The optional argument will be used in index
% entries. Not yet implemented.
%
%    \begin{macrocode}

\def\DeclareLigatureCommand#1#2{%
  \@ifnextchar[%
    {\pg@declare@lig{#1}{#2}}%
    {\pg@declare@lig{#1}{#2}[]}}

\def\pg@declare@lig#1#2[#3]{%
  \@ifundefined{\pg@cmd\thelanguage{#1}}%
    {\DeclareLigature{#1}}\@empty
  \@ifundefined{\pg@cmd\thelanguage{#1-\string#2}}%
   {\@namedef{\pg@@\thelanguage-\string#1-\string#2}}%
   {\pg@bug@err3}}

\@onlypreamble\DeclareLigatureCommand

%    \end{macrocode}
%
% We need take care of categorie codes because they can
% be changed by a package or by the user. Most of them
% perform the same action does not matter its char code;
% however, 11 and 12 mean ``I print myself'' and 13
% means ``I'm a command''. The right char is 
% set by |\lccode|. Here |^^<letter>| is conventional, and
% is used for letter (|L|), and other (|O|).
% If the setting is valid, |\pg@b| expands to
% |\let \pg@text@<char> <other char>| but if not it
% expands simply to |\let \pg@text@<char>| which
% gobbles |\@gobbletwo| and makes the error to be
% expanded.
%
%    \begin{macrocode}

\begingroup
\catcode`\$=3 \catcode`\&=4
\catcode`\#=6
\catcode`\^=7 \catcode`\_=8
\catcode`\ =10 \gdef\pg@space{ }
\catcode`\^^L=11 \catcode`\^^O=12
\catcode`\~=13
\gdef\pg@set@lig#1{\begingroup
  \lccode`^^L=`#1 \lccode`^^O=`#1 \lccode`~=`#1 \lccode`#1=`#1
  \lowercase{\endgroup
  \edef\pg@b{\noexpand\let
      \expandafter\noexpand\csname\pg@@text\string#1\endcsname
    \ifcase\catcode`#1 \or\or\or$\or&\or
      \or####\or^\or_\or\or\noexpand\pg@space
      \or ^^L\or^^O\or\noexpand~\or\or^^O\fi}%
    \pg@b\@gobbletwo\pg@lig@err{\string#1}%
    \expandafter\let\csname\pg@@math\string#1\expandafter
      \endcsname\csname\pg@@text\string#1\endcsname
    \ifnum\catcode`#1>10 \ifnum\catcode`#1<13 \ifnum\mathcode`#1="8000
      \expandafter\let\csname\pg@@math\string#1\endcsname=~%
    \fi\fi\fi}}
\endgroup

\def\pg@lig@err{\pg@err{Bad ligature}}

%    \end{macrocode}
%
% In the following lines note the change of categories!
% This command checks there are exactly one token
% in the argument. Commands are long to handle the
% case the ligature comes at the end of a paragraph.
%
%    \begin{macrocode}

\begingroup
\catcode`\%=12 \catcode`\&=14
\long\gdef\pg@text@ligature#1#2{&
  \expandafter\if\expandafter%\@gobble#2%\@empty
    \expandafter\pg@ligature\expandafter#1&
  \else
    \csname\pg@@text\string#1\expandafter\endcsname
  \fi{#2}}
\endgroup

\long\def\pg@ligature#1#2{%
  \expandafter\ifx\csname\pg@cmd\pg@lig{#1-\string#2}\endcsname\relax
    \csname\pg@@text\string#1\expandafter\endcsname\expandafter#2%
  \else
    \csname\pg@cmd\pg@lig{#1-\string#2}\expandafter\endcsname
  \fi}

\long\def\pg@math@ligature#1{%
  \@nameuse{\pg@@math\string#1}}

\def\UpdateSpecial#1{\expandafter\pg@update@special
  \csname\string#1\endcsname}

\def\pg@update@special#1{%
  \def\pg@b##1##2{\ifnum`#1=`##2 \else
     \noexpand##1\noexpand##2\fi}%
  \def\pg@c##1##2{\ifnum\catcode`##2<\active
     \ifnum\catcode`##2>10 \else\@firstoftwo\fi
       \else\noexpand##1\noexpand##2\fi}%
  \def\do{\pg@b\do}%
  \def\@makeother{\pg@b\@makeother}%
  \edef\dospecials{\dospecials\pg@c\do#1}%
  \edef\@sanitize{\@sanitize\pg@c\@makeother#1}%
  \let\do\relax
  \def\@makeother##1{\catcode`##1=12\relax}}

\begingroup
\catcode`*=\active \catcode`^=7
\catcode`~=\active \catcode`'=12
\lccode`*=`^ \lccode`^=`^ \lccode`~=`' \lccode`'=`'
\lowercase{%
\gdef\pr@m@s{%
  \ifx~\@let@token\let\@let@token='\fi
  \ifx*\@let@token\let\@let@token=^\fi
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else\ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
  \else
      \egroup
  \fi\fi}}
\endgroup

%    \end{macrocode}
%
% \section{Tools}
%
% Take, for instance, catalan. We may say:
% |\DeclareLigatureCommand{`}{a}{\`a}|.
% This command cancels any possibility to say:
% |\counterx=`a|. The following commands allow us
% to subtitute |\charnumber| by |`|:
% |\counterx=\charnumber a|. The same applies to
% |"| (|\DeclareLigatureCommand{"}{A}{\"A}|) or |'|.
%
%    \begin{macrocode}

\begingroup
\catcode`"=12 \catcode`'=12 \catcode``=12
\gdef\hexnumber{"}
\gdef\octalnumber{'}
\gdef\charnumber{`}
\endgroup

\def\allowhyphens{\ifhmode\nobreak\hskip\z@skip\fi}

\providecommand\@tabacckludge[1]{%
  \expandafter\@changed@cmd\csname#1\endcsname\relax}

\def\ensurelanguage{\ifx\glossary\relax
  \protect\pg@ensure\pg@last\fi}

\def\pg@ensure#1#2{%
  \def\pg@a{{#1}{#2}}%
  \ifx\pg@a\pg@last\else
    \def\pg@a{#1}%
    \ifx\pg@a\@empty\def\pg@c{\pg@unsel@ii}\else
      \def\pg@c{\pg@sel@iii*#1}\fi
    \def\pg@a{#2}%
    \ifx\pg@a\@empty\else
     \def\pg@a{#1}\edef\pg@b{\expandafter\@firstoftwo\pg@last}%
     \ifx\pg@b\pg@a\expandafter\def\else\expandafter\pg@after\fi
     \pg@c{\pg@sel@iii{}#2}%
    \fi
    \expandafter\pg@c
  \fi}
  
\def\ensuredcommand#1#2{%
  \expandafter\gdef\expandafter#1\expandafter
    {\expandafter{\expandafter\pg@ensure\pg@last#2}}}


%    \end{macrocode}
%
% Two \LaTeX\ commands for marks are redefined. (Sad.)
%
%    \begin{macrocode}

\def\markboth#1#2{%
     \gdef\@themark{{\ensurelanguage#1}{\ensurelanguage#2}}{%
     \let\protect\@unexpandable@protect
     \let\label\relax \let\index\relax \let\glossary\relax
     \mark{\@themark}}\if@nobreak\ifvmode\nobreak\fi\fi}
     
\def\@markright#1#2#3{%
  \gdef\@themark{{#1}{\ensurelanguage#3}}}

%    \end{macrocode}
%
% \section{Font Encoding Commands}
%
%    \begin{macrocode}

\def\DeclareLanguageCompositeCommand#1#2#3{%
  \pg@add@to{text}{\pg@composite{#1}{#2}}%
  \expandafter\DeclareLanguageCommand
    \csname\expandafter\string\csname#2\endcsname
    \string#1-\string#3\endcsname{text}}

\def\pg@composite#1#2{%
  \@ifundefined{\pg@cmd\thelanguage{#1}}%
    {\expandafter\let\csname\pg@@\thelanguage-\string#1\endcsname#1%
     \expandafter\pg@after\csname\pg@@/text\endcsname
         {\expandafter\let\expandafter
            #1\csname\pg@@\thelanguage-\string#1\endcsname}}{}%
  \expandafter\let\expandafter\reserved@a\csname#2\string#1\endcsname
  \expandafter\expandafter\expandafter\ifx
  \expandafter\@car\reserved@a\relax\relax\@nil \@text@composite \else
      \edef\reserved@b##1{%
         \def\expandafter\noexpand
            \csname#2\string#1\endcsname####1{%
               \noexpand\@text@composite
               \expandafter\noexpand\csname#2\string#1\endcsname
               ####1\noexpand\@empty\noexpand\@text@composite
               {##1}}}%
      \expandafter\reserved@b\expandafter{\reserved@a{##1}}%
   \fi}

\@onlypreamble\DeclareLanguageCompositeCommand

%    \end{macrocode}
%
% The following code was written but eventually
% discarded. It was intended for an argument
% expanded in full with some others not expanded
% at all.
% \begin{verbatim}
% \def\pg@expand#1{\edef\pg@b{#1}%
%   \afterassignment\pg@@expand\def\pg@c##1\pg@c}
% \pg@@expand{\expandafter\pg@c\pg@b\pg@c}
% \end{verbatim}
% For example, in |\SetLanguageCode|
% \begin{verbatim}
% \pg@expand{\the\count@}{\SetLanguageVariable{#1##1}}{#2}
% \end{verbatim}
%
%    \begin{macrocode}

\def\DeclareLanguageTextCommand#1#2{%
  \@ifundefined{\pg@cmd\thelanguage{#1}}%
    {\DeclareLanguageCommand{#1}{text}%
                           {\pg@text@cmd{#1}{#2}}%
     \expandafter\DeclareLanguageCommand
       \csname#2\string#1\endcsname{text}}%
    {\expandafter\let\expandafter\pg@a
       \csname\pg@@\thelanguage-\string#1\endcsname
     \expandafter\in@\expandafter\pg@text@cmd
       \expandafter{\pg@a}%
     \ifin@\def\pg@a{\expandafter\DeclareLanguageCommand
       \csname#2\string#1\endcsname{text}}%
       \expandafter\pg@a
     \else\pg@bug@err3\fi}}

\@onlypreamble\DeclareLanguageTextCommand

\def\pg@text@cmd#1#2{%
  \csname#2-cmd\expandafter\endcsname
  \expandafter#1%
  \csname#2\string#1\endcsname}
  
%    \end{macrocode}
%
% \subsection{Extensions handling}
%
% Not yet implemented. |\pge@<language>| will keep
% track of loaded extensions; now is just a flag.
% The next command is provisional. (If you read the manual
% you have guessed it.) 
%
%    \begin{macrocode}

\def\pg@extensions#1{%
  \edef\cdp@elt##1##2##3##4{%
    \def\noexpand\DeclareCompositeLanguageCommand####1{%
      \noexpand\pg@composite{####1}{##1}}%
    \def\noexpand\DeclareTextLanguageCommand####1{%
      \noexpand\pg@text{####1}{##1}}%
    \noexpand\InputIfFileExists{#1.##1}{}{}}%
  \cdp@list}


%</preload|package>
%<*package>
%    \end{macrocode}
%
% \subsection{Loading requested languages}
%
% Some commands will be defined if you didn't
% use the installation procedure.
%
%    \begin{macrocode}

\ifx\PreLoadPatterns\@undefined

  \def\LanguagePath#1{\edef\input@path{\input@path{#1}}}

  \let\PreLoadPatterns\@gobbletwo

  \def\SetPatterns#1#2{\expandafter\chardef
     \csname pgh@#1\endcsname=#2\relax}

  \def\PreLoadLanguage#1#2#{\@gobbletwo}

  \def\LoadLanguage#1{\@ifnextchar[{\pg@load{#1}}%
                {\pg@load{#1}[#1]}}

  \def\pg@load#1[#2]#3#4{%
   \DeclareOption{#1}{\pg@input{#1}{#2}{#3}{#4}}}

  \def\pg@input#1#2#3#4{%
    \pg@to@list{#1}%
    \@ifundefined{pgh@#1}%
      {\expandafter\let\csname pgh@#1\expandafter\endcsname
         \csname pgh@#3\endcsname%
       \PackageInfo{polyglot}%
         {#1 with #3 patterns\@gobble}}\@empty
    \@ifundefined{\pg@@?#1}%
      {\InputIfFileExists{#2.ld}{}%
         {\pg@err{Missing language file}}%
       \pg@extensions{#2}}\@empty
    \edef\thelanguage{\csname\pg@@?#1\endcsname}#4}

\fi

%</package>
%<*preload>

\everyjob\expandafter{\the\everyjob\SetWeekDay}

%</preload>
%<*preload|package>

\@onlypreamble\PreLoadPatterns
\@onlypreamble\SetPatterns
\@onlypreamble\PreLoadLanguage
\@onlypreamble\LoadLanguage
\@onlypreamble\pg@input
\@onlypreamble\pg@load

%</preload|package>
%<*package>

\input{polyglot.cfg}
\ProcessOptions
\pg@sel@opt

%</package>
%    \end{macrocode}
%
% \section{A basic |polyglot.cfg| file}
%
%    \begin{macrocode}
%<*config>

\SetPatterns{english}{0}

\LoadLanguage{english}{english}{}
\LoadLanguage{american}[english]{english}{}
\LoadLanguage{french}{english}{}
\LoadLanguage{german}{english}{}
\LoadLanguage{austrian}[german]{english}{}
\LoadLanguage{spanish}{english}{}
\LoadLanguage{hebrew}[r_hebrew]{english}{}
%</config>
%    \end{macrocode}
\endinput
% \Finale