%
% TRANSLATION COMMANDS
%
% The lower case commands use \fqname based keys, the upper case absolute keys.
% Example:
%   \dt[example]{german}{Beispiel}  % defines the key \fqname:example
%   \ger[example]{Beispiel}         % defines the key \fqname:example
%   \DT[example]{german}{Beispiel}  % defines the key example
%   \Ger[example]{Beispiel}         % defines the key example
%
% For ease of use in the ttext environment and the optional argument of the \Part, \Section, ... commands,
% all "define translation" commands use \fqname as default key

% Get a translation
% expandafter required because the translation commands dont expand anything
% shortcuts for translations
% 1: key

\newcommand{\IfTranslationExists}[1]{%
    % only check english. All translations must be defined for english
    \def\tempiftranslation{\IfTranslation{english}}%
    \expandafter\tempiftranslation\expandafter{#1}%
}
\newcommand{\iftranslation}[1]{%
    \IfTranslation{english}{\fqname:#1}
    % \expandafter\IfTranslationExists\expandafter{\fqname:#1}
}

\newcommand{\gt}[1]{%
    \iftranslation{#1}{%
        \expandafter\GetTranslation\expandafter{\fqname:#1}%
    }{%
        \detokenize{\fqname}:\detokenize{#1}%
    }%
}
\newrobustcmd{\GT}[1]{%\expandafter\GetTranslation\expandafter{#1}}
    \IfTranslationExists{#1}{%
        \expandafter\GetTranslation\expandafter{#1}%
    }{% ??
        \detokenize{#1}%
    }%
}

% Define a translation and also make the fallback if it is the english translation
% 1: lang, 2: key, 3: translation
\newcommand{\addtranslationcustom}[3]{
    \ifstrequal{#1}{english}{
        \addtranslationfallback{#2}{#3}
        \immediate\write\translationsaux{\noexpand\addtranslationfallback{#2}{\detokenize{#3}}}
        % \immediate\write\translationsaux{\noexpand\addtranslationfallback{#2}{{#3}}}%
    }{}
    \immediate\write\translationsaux{\noexpand\addtranslation{#1}{#2}{\detokenize{#3}}}
    \addtranslation{#1}{#2}{#3}
}

% Define a new translation
% [1]: key, 2: lang, 3: translation
\newcommand{\dt}[3][\fqname]{
    \ifstrempty{#3}{}{ % dont add empty translations so that the fallback will be used instead
        % hack because using expandafter on the second arg didnt work
        \def\tempaddtranslation{\addtranslationcustom{#2}}
        \ifstrequal{#1}{\fqname}{
            % \expandafter\tempaddtranslation\expandafter{\fqname}{#3}
            \addtranslationcustom{#2}{#1}{#3}
        }{
            \addtranslationcustom{#2}{\fqname:#1}{#3}
            % \expandafter\tempaddtranslation\expandafter{\fqname:#1}{#3}
        }
    }
}
% Define a new translation
% [1]: key, 2: lang, 3: translation
\newcommand{\DT}[3][dummy]{
    \ifstrempty{#3}{}{ % dont add empty translations so that the fallback will be used instead
        % hack because using expandafter on the second arg didnt work
        \def\tempaddtranslation{\addtranslationcustom{#2}}
        \ifstrequal{#1}{dummy}{
            % \expandafter\tempaddtranslation\expandafter{\fqname}{#3}
            \addtranslationcustom{#2}{\fqname}{#3}
        }{
            % \expandafter\tempaddtranslation\expandafter{#1}{#3}
            \addtranslationcustom{#2}{#1}{#3}
        }
    }
}
% [1]: key, 2: translation
\newcommand{\ger}[2][\fqname]{\dt[#1]{german}{#2}}
\newcommand{\eng}[2][\fqname]{\dt[#1]{english}{#2}}

\newcommand{\Ger}[2][\fqname]{\DT[#1]{german}{#2}}
\newcommand{\Eng}[2][\fqname]{\DT[#1]{english}{#2}}

