% --------------------------------------------------------------------------
% the TRANSLATIONS package
% 
%   a simple translator
%
% This package is part of the exsheets bundle
% --------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://bitbucket.org/cgnieder/exsheets/
% E-Mail: contact@mychemistry.eu
% --------------------------------------------------------------------------
% Copyright 2012 Clemens Niederberger
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% 
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Clemens Niederberger.
% --------------------------------------------------------------------------
% The translations package consists of the files
%  - translations.sty
% --------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% --------------------------------------------------------------------------
\def\@trnslt@date{2012/11/08}
\def\@trnslt@version{v0.4}

\ProvidesPackage{translations}[\@trnslt@date\space \@trnslt@version\space a simple translator]
\RequirePackage{etoolbox}

% --------------------------------------------------------------------------
% message handling
\def\@trnslt@error@message{%
  For details have a look at the `exsheets' manual.}

\def\@trnslt@create@message#1{%
  \ifstrequal{#1}{Error}
    {%
      \lowercase{\csdef{@trnslt@#1}}##1{%
        \csuse{Package#1}{translations}{##1}{\@trnslt@error@message}}%
    }{%
      \lowercase{\csdef{@trnslt@#1}}##1{%
        \csuse{Package#1}{translations}{##1}}%
    }}
\@trnslt@create@message{Error}
\@trnslt@create@message{Warning}
\@trnslt@create@message{WarningNoLine}
\@trnslt@create@message{Info}

\def\@trnslt@err@unknown@lang#1{%
  \@trnslt@error{Unknown language `#1'}}

\def\@trnslt@warn@unknown@lang#1{%
  \@trnslt@warning{Unknown language `#1'}}

\def\@trnslt@err@already@defined#1#2{%
  \@trnslt@error{The #2 translation for `#1' is already defined.}}

\def\@trnslt@err@not@defined#1#2{%
  \@trnslt@error{The \@trnslt@language{#2} translation for `#1' is not defined yet.}}

% --------------------------------------------------------------------------
% check if babel or polyglossia is used
\AtEndPreamble{
  \@ifpackageloaded{babel}{}{
    \@ifpackageloaded{polyglossia}{}
      {\@trnslt@warning{No language package found. I am going to use `english'
        as default language.}}
  }
  \ifdef\languagename{}
    {\def\languagename{english}}
  \def\@trnslt@current@language{\languagename}
  \ifdef\bbl@afterfi{}
    {\long\def\bbl@afterfi#1\fi{\fi#1}}
}

% --------------------------------------------------------------------------
% \DeclareLanguage and \DeclareLanguageAlias
% #1: language
\newrobustcmd*\DeclareLanguage[1]{%
  \@trnslt@declare@language{#1}}
\@onlypreamble\DeclareLanguage

\def\@trnslt@declare@language#1{%
  \csdef{@trnslt@language@#1}{#1}}

% #1: alias
% #2: language
\newrobustcmd*\DeclareLanguageAlias[2]{%
  \@trnslt@declare@languagealias{#1}{#2}}
\@onlypreamble\DeclareLanguageAlias

\def\@trnslt@declare@languagealias#1#2{%
  \ifcsdef{@trnslt@language@#2}
    {}{%
      \@trnslt@warn@unknown@lang{#2}%
      \@trnslt@declare@language{#2}%
    }%
  \csletcs{@trnslt@language@#1}{@trnslt@language@#2}}

\def\@trnslt@language#1{%
  \csuse{@trnslt@language@#1}}

% --------------------------------------------------------------------------
% \DeclareTranslation, \NewTranslation and \RenewTranslation
% #1: language
% #2: word
% #3: replacement
\newrobustcmd*\DeclareTranslation[3]{%
  \@trnslt@declare@translation{#2}{#1}{#3}}
\@onlypreamble\DeclareTranslation

\def\@trnslt@declare@translation#1#2#3{%
  \ifcsdef{@trnslt@language@#2}
    {%
      \csdef{@trnslt@word@#1@\@trnslt@language{#2}}{#3}%
      \csdef{@trnslt@word@#1@literal}{#1}%
    }
    {\@trnslt@err@unknown@lang{#2}}}

\def\@trnslt@if@translation#1#2{%
  \ifcsdef{@trnslt@word@#1@\@trnslt@language{#2}}
    {\expandafter\@firstoftwo}
    {\expandafter\@secondoftwo}}

\newrobustcmd*\NewTranslation[3]{%
  \@trnslt@new@translation{#2}{#1}{#3}}
\@onlypreamble\NewTranslation

\def\@trnslt@new@translation#1#2#3{%
  \@trnslt@if@translation{#1}{#2}
    {\@trnslt@err@already@defined{#1}{#2}}
    {\@trnslt@declare@translation{#1}{#2}{#3}}}

\newrobustcmd*\RenewTranslation[3]{%
  \@trnslt@renew@translation{#2}{#1}{#3}}
\@onlypreamble\RenewTranslation

\def\@trnslt@renew@translation#1#2#3{%
  \@trnslt@if@translation{#1}{#2}
    {\@trnslt@declare@translation{#1}{#2}{#3}}
    {\@trnslt@err@not@defined{#1}{#2}}}

% --------------------------------------------------------------------------
% \GetTranslationFor and \GetTranslation
% these need to be expandable!
% #1: language
% #2: word
\newcommand*\GetTranslationFor[2]{%
  \@trnslt@get@translation@for{#2}{#1}}

\newcommand*\GetTranslation[1]{%
  \@trnslt@get@translation@for{#1}{\@trnslt@current@language}}

% #1: word #2: language
\def\@trnslt@get@translation@for#1#2{%
  \@trnslt@if@translation{#1}{#2}
    {\csuse{@trnslt@word@#1@\@trnslt@language{#2}}}
    {%
      \@trnslt@warning{Translation for `#1' in #2 unknown.}%
      \csuse{@trnslt@word@#1@literal}%
    }}

% --------------------------------------------------------------------------
% \SaveTranslationFor and \SaveTranslation
\newrobustcmd*\SaveTranslationFor[3]{%
  \@trnslt@save@translation@for{#1}{#3}{#2}}

\newrobustcmd*\SaveTranslation[2]{%
  \@trnslt@save@translation@for{#1}{#2}{\@trnslt@current@language}}

\def\@trnslt@save@translation@for#1#2#3{%
  \edef#1{%
    \@trnslt@if@translation{#2}{#3}
      {\csuse{@trnslt@word@#2@\@trnslt@language{#3}}}
      {}%
  }}

% --------------------------------------------------------------------------
% \LoadDictionary and \LoadDictionaryFor
\newrobustcmd*\LoadDictionary[1]{%
  \@trnslt@load@dictionary@for{#1}{\@trnslt@current@language}}
\@onlypreamble\LoadDictionary

\newrobustcmd*\LoadDictionaryFor[2]{%
  \@trnslt@load@dictionary@for{#1}{#2}}
\@onlypreamble\LoadDictionaryFor

\def\@trnslt@load@dictionary@for#1#2{%
  \AtBeginDocument{%
    \InputIfFileExists{#1-\@trnslt@language{#2}.trsl}
      {\@trnslt@info{loading dictionary `#1' for `#2'.}}
      {\@trnslt@warning{File `#1-\@trnslt@language{#2}.trsl' not found.}}%
  }}

\newrobustcmd*\ProvideDictionaryFor[2]{%
  \@trnslt@provide@dictionary@for{#1}{#2}}
\@onlypreamble\ProvideDictionaryFor

\def\@trnslt@provide@dictionary@for#1#2{%
  \def\@trnslt@dictionary@name{#2}%
  \def\@trnslt@dictionary@lang{#1}%
  \@ifnextchar[
    {\@trnslt@provide@dictionary@version}
    {\ProvidesFile{#2-#1.trsl}[(#1 translation file `#2')]}}

\def\@trnslt@provide@dictionary@version[#1]{%
  \ProvidesFile
    {\@trnslt@dictionary@name-\@trnslt@dictionary@lang.trsl}%
    [(\@trnslt@dictionary@lang\space translation file `\@trnslt@dictionary@name') #1]}

% --------------------------------------------------------------------------
% predefined languages
\DeclareLanguage{albanian}
\DeclareLanguage{bulgarian}
\DeclareLanguage{catalan}
\DeclareLanguage{croatian}
\DeclareLanguage{czech}
\DeclareLanguage{danish}
\DeclareLanguage{dutch}
\DeclareLanguage{english}
\DeclareLanguage{finnish}
\DeclareLanguage{french}
\DeclareLanguage{german}
\DeclareLanguage{greek}
\DeclareLanguage{hebrew}
\DeclareLanguage{hungarian}
\DeclareLanguage{icelandic}
\DeclareLanguage{italian}
\DeclareLanguage{norwegian}
\DeclareLanguage{polish}
\DeclareLanguage{portuges}
\DeclareLanguage{romanian}
\DeclareLanguage{russian}
\DeclareLanguage{serbocroatian}
\DeclareLanguage{slovak}
\DeclareLanguage{slovenian}
\DeclareLanguage{spanish}
\DeclareLanguage{swedish}
\DeclareLanguage{turkish}
\DeclareLanguage{ukrainian}

\DeclareLanguageAlias{Albanian}{albanian}
\DeclareLanguageAlias{Bulgarian}{bulgarian}
\DeclareLanguageAlias{Catalan}{catalan}
\DeclareLanguageAlias{Croatian}{croatian}
\DeclareLanguageAlias{Czech}{czech}
\DeclareLanguageAlias{Danish}{danish}
\DeclareLanguageAlias{Dutch}{dutch}
\DeclareLanguageAlias{Finnish}{finnish}
\DeclareLanguageAlias{francais}{french}
\DeclareLanguageAlias{Francais}{francais}
\DeclareLanguageAlias{canadien}{french}
\DeclareLanguageAlias{Canadien}{canadien}
\DeclareLanguageAlias{French}{french}
\DeclareLanguageAlias{american}{english}
\DeclareLanguageAlias{American}{american}
\DeclareLanguageAlias{australian}{english}
\DeclareLanguageAlias{Australian}{australian}
\DeclareLanguageAlias{british}{english}
\DeclareLanguageAlias{British}{british}
\DeclareLanguageAlias{canadian}{english}
\DeclareLanguageAlias{Canadian}{canadian}
\DeclareLanguageAlias{English}{english}
\DeclareLanguageAlias{UKenglish}{english}
\DeclareLanguageAlias{USenglish}{english}
\DeclareLanguageAlias{austrian}{german}
\DeclareLanguageAlias{Austrian}{austrian}
\DeclareLanguageAlias{German}{german}
\DeclareLanguageAlias{germanb}{german}
\DeclareLanguageAlias{naustrian}{austrian}
\DeclareLanguageAlias{ngerman}{german}
\DeclareLanguageAlias{Greek}{greek}
\DeclareLanguageAlias{polutonikogreek}{greek}
\DeclareLanguageAlias{Hebrew}{hebrew}
\DeclareLanguageAlias{Hungarian}{hungarian}
\DeclareLanguageAlias{magyar}{hungarian}
\DeclareLanguageAlias{Magyar}{magyar}
\DeclareLanguageAlias{Icelandic}{icelandic}
\DeclareLanguageAlias{Italian}{italian}
\DeclareLanguageAlias{norsk}{norwegian}
\DeclareLanguageAlias{Norsk}{norsk}
\DeclareLanguageAlias{Norwegian}{norwegian}
\DeclareLanguageAlias{nynorsk}{norwegian}
\DeclareLanguageAlias{Nynorsk}{nynorsk}
\DeclareLanguageAlias{Polish}{polish}
\DeclareLanguageAlias{brazil}{portuges}
\DeclareLanguageAlias{Brazil}{brazil}
\DeclareLanguageAlias{brazilian}{portuges}
\DeclareLanguageAlias{Brazilian}{brazilian}
\DeclareLanguageAlias{Portuges}{portuges}
\DeclareLanguageAlias{portuguese}{portuges}
\DeclareLanguageAlias{Portuguese}{portuguese}
\DeclareLanguageAlias{Romanian}{romanian}
\DeclareLanguageAlias{Russian}{russian}
\DeclareLanguageAlias{Serbocroatian}{serbocroatian}
\DeclareLanguageAlias{Slovak}{slovak}
\DeclareLanguageAlias{Slovenian}{slovenian}
\DeclareLanguageAlias{Spanish}{spanish}
\DeclareLanguageAlias{Swedish}{swedish}
\DeclareLanguageAlias{Turkish}{turkish}
\DeclareLanguageAlias{Ukrainian}{ukrainian}

\endinput