\ProvidesPackage{babel}
\RequirePackage{keyval}
%%%%
%%%% A simple command that takes language (say l) and 
%%%%    a) checks for existence and includes l.hva 
%%%%    b) calls the command \l@babel (in l.hva)
%%%%    c) sets current language to l
%
\newcommand{\InitiateOption}[1]{%
\@ifundefined{bbl@#1@loaded}
{\let\csname bbl@#1@loaded\endcsname\relax%
\newenvironment{#1}{\begin{foreign@language@block}{#1}}{\end{foreign@language@block}}%
\newenvironment{#1*}{\begin{foreign@language@block}{#1}}{\end{foreign@language@block}}%
\@iffileexists{#1.hva}
  {\input{#1.hva}\def\bbl@main@language{#1}}
  {\hva@warn{Hevea babel '#1' not found, loading 'english' instead}%
   \input{english.hva}\def\bbl@main@language{english}}}
{}}
%%%Default option (parses main=<lang>)
\define@key{babel}{main}{\def\bbl@realmain@language{#1}\InitiateOption{#1}}
\newcommand{\DefaultOption}[1]{\@setkeys{babel}{#1}}
\DeclareOption{@defopt}{\DefaultOption}%
%
%%%% Processing information for each optional argument
%
\DeclareOption{english}{\InitiateOption{english}}%
\DeclareOption{USenglish}{\InitiateOption{english}}%
\DeclareOption{french}{\InitiateOption{french}}%
\DeclareOption{frenchb}{\InitiateOption{french}}%
\DeclareOption{francais}{\InitiateOption{french}}%
\DeclareOption{german}{\InitiateOption{german}}%
\DeclareOption{greek}{\InitiateOption{greek}}%
\DeclareOption{ngerman}{\InitiateOption{german}}%
\DeclareOption{austrian}{\InitiateOption{austrian}}% 
\DeclareOption{czech}{\InitiateOption{czech}}%
\DeclareOption{american}{\InitiateOption{english}}
\DeclareOption{croatian}{\InitiateOption{croatian}}%
\DeclareOption{catalan}{\InitiateOption{catalan}}%
\DeclareOption{brazil}{\InitiateOption{brazil}}%
\DeclareOption{danish}{\InitiateOption{danish}}%
\DeclareOption{dutch}{\InitiateOption{dutch}}%
\DeclareOption{esperanto}{\InitiateOption{esperanto}}%
\DeclareOption{finnish}{\InitiateOption{finnish}}%
\DeclareOption{galician}{\InitiateOption{galician}}%
\DeclareOption{italian}{\InitiateOption{italian}}%
\DeclareOption{magyar}{\InitiateOption{magyar}}%
\DeclareOption{norsk}{\InitiateOption{norsk}}%
\DeclareOption{nynorsk}{\InitiateOption{nynorsk}}%
\DeclareOption{polish}{\InitiateOption{polish}}%
\DeclareOption{portuguese}{\InitiateOption{portuguese}}%
\DeclareOption{romanian}{\InitiateOption{romanian}}%
\DeclareOption{russian}{\InitiateOption{russian}}%
\DeclareOption{slovak}{\InitiateOption{slovak}}%
\DeclareOption{slovene}{\InitiateOption{slovene}}%
\DeclareOption{spanish}{\InitiateOption{spanish}}%
\DeclareOption{swedish}{\InitiateOption{swedish}}%
\DeclareOption{turkish}{\InitiateOption{turkish}}%
%
%%%% Processing babel options postponed for later
%
%\ProcessOptions*
%
%%%% One variable for each supported language, to keep track of and to 
%%%% change the current language. Might as well have used english itself 
%%%% instead of \english@language!!! 
%
\newcommand\english@language{english}%
\newcommand\french@language{french}%
\newcommand\german@language{german}%
\newcommand\ngerman@language{ngerman}%
\newcommand\greek@language{greek}%
\newcommand\american@language{american}%
\newcommand\austrian@language{austrian}%
\newcommand\brazil@language{brazil}%
\newcommand\catalan@language{catalan}%
\newcommand\croatian@language{croatian}%
\newcommand\czech@language{czech}%
\newcommand\danish@language{danish}%
\newcommand\dutch@language{dutch}%
\newcommand\esperanto@language{esperanto}%
\newcommand\finnish@language{finnish}%
\newcommand\galician@language{galician}%
\newcommand\italian@language{italian}%
\newcommand\magyar@language{magyar}%
\newcommand\norsk@language{norsk}%
\newcommand\nynorsk@language{nynorsk}%
\newcommand\polish@language{polish}%
\newcommand\portuguese@language{portuguese}%
\newcommand\romanian@language{romanian}%
\newcommand\russian@language{russian}%
\newcommand\slovak@language{slovak}%
\newcommand\slovene@language{slovene}%
\newcommand\spanish@language{spanish}%
\newcommand\swedish@language{swedish}%
\newcommand\turkish@language{turkish}%
%
%%%% Initializing \current@language before loading babel language packages.
%
\let\current@language\english@language
%
%%%% Support for the 'babel' command to select languages.
%
%Save definition of double quote (altered by german)
\let\@hevea@saved@dquote\@hevea@dquote
\newcommand{\selectlanguage}[1]{%
\def\language@packages{#1}%
\let\@hevea@dquote\@hevea@saved@dquote%Restore inocuous definition
  \ifthenelse{\equal{#1}{german}} {%
	\let\current@language\german@language    \german@babel    } {%
  \ifthenelse{\equal{#1}{greek}} {%
	\let\current@language\greek@language    \greek@babel    } {%
  \ifthenelse{\equal{#1}{ngerman}} {%
	\let\current@language\ngerman@language   \german@babel    } {%
  \ifthenelse{\equal{#1}{english}} {%
 	\let\current@language\english@language   \english@babel   } {%
  \ifthenelse{\equal{#1}{french}} {%
	\let\current@language\french@language    \french@babel    } {%
  \ifthenelse{\equal{#1}{american}} {%
	\let\current@language\american@language  \american@babel  } {%
  \ifthenelse{\equal{#1}{austrian}} {%
	\let\current@language\austrian@language  \austrian@babel  } {%
  \ifthenelse{\equal{#1}{brazil}} {%
	\let\current@language\brazil@language    \brazil@babel    } {%
  \ifthenelse{\equal{#1}{catalan}} {%
	\let\current@language\catalan@language   \catalan@babel   } {%
  \ifthenelse{\equal{#1}{croatian}} {%
	\let\current@language\croatian@language  \croatian@babel  } {%
  \ifthenelse{\equal{#1}{czech}} {%
	\let\current@language\czech@language     \czech@babel     } {%
  \ifthenelse{\equal{#1}{danish}} {%
	\let\current@language\danish@language    \danish@babel    } {%
  \ifthenelse{\equal{#1}{dutch}} {%
	\let\current@language\dutch@language     \dutch@babel     } {%
  \ifthenelse{\equal{#1}{esperanto}} {%
	\let\current@language\esperanto@language \esperanto@babel } {%
  \ifthenelse{\equal{#1}{finnish}} {%
	\let\current@language\finnish@language   \finnish@babel   } {%
  \ifthenelse{\equal{#1}{galician}} {%
	\let\current@language\galician@language  \galician@babel  } {%
  \ifthenelse{\equal{#1}{italian}} {%
	\let\current@language\italian@language   \italian@babel   } {%
  \ifthenelse{\equal{#1}{magyar}} {%
	\let\current@language\magyar@language    \magyar@babel    } {%
  \ifthenelse{\equal{#1}{norsk}} {%
	\let\current@language\norsk@language     \norsk@babel     } {%
  \ifthenelse{\equal{#1}{nynorsk}} {%
	\let\current@language\nynorsk@language   \nynorsk@babel   } {%
  \ifthenelse{\equal{#1}{polish}} {%
	\let\current@languagepolish\@language    \polish@babel    } {%
  \ifthenelse{\equal{#1}{portuguese}} {%
	\let\current@language\portuguese@language  \portuguese@babel  } {%
  \ifthenelse{\equal{#1}{romanian}} {%
	\let\current@language\romanian@language  \romanian@babel  } {%
  \ifthenelse{\equal{#1}{russian}} {%
	\let\current@language\russian@language   \russian@babel   } {%
  \ifthenelse{\equal{#1}{slovak}} {%
	\let\current@language\slovak@language    \slovak@babel    } {%
  \ifthenelse{\equal{#1}{slovene}} {%
	\let\current@language\slovene@language   \slovene@babel   } {%
  \ifthenelse{\equal{#1}{spanish}} {%
	\let\current@language\spanish@language   \spanish@babel   } {%
  \ifthenelse{\equal{#1}{swedish}} {%
	\let\current@language\swedish@language   \swedish@babel   } {%
  \ifthenelse{\equal{#1}{turkish}} {%
	\let\current@language\turkish@language   \turkish@babel   } {%
  }}}}}}}}}}}}}}}}}}}}}}}}}}}}}%
}%
%
\newenvironment{foreign@language@block}[1]%
{\let\outer@language=\current@language%
  \@open{div}{class="foreignlanguage" lang="\map@lang@code{#1}"}%
  \selectlanguage{#1}}%
{\@close{div}\selectlanguage{\outer@language}}
\newenvironment{foreign@language@inline}[1]%
{\let\outer@language=\current@language%
  \@open{span}{class="foreignlanguage" lang="\map@lang@code{#1}"}%
  \selectlanguage{#1}}%
{\@close{span}\selectlanguage{\outer@language}}
\newcommand{\foreignlanguage}[2]{%
  \begin{foreign@language@inline}{#1}#2\end{foreign@language@inline}
}
\newenvironment{hyphenrules}[1]{\@span{lang="\map@lang@code{#1}"}}{}
%
%%%% Support for the 'babel' command for querying the current language.
%
\newcommand{\iflanguage}[3]{%
  \ifthenelse{\equal{#1}{\current@language}}{#2}{#3}%
}%
%
%%%% Processing the optional arguments of the '\usepackage{babel}' command
%%%% For each option (language l), checks for l.hva and 'includes' it.
%
\ProcessOptions*%
\ifu\bbl@realmain@language\else\let\bbl@main@language\bbl@realmain@language\fi
\selectlanguage{\bbl@main@language}
