\ExplSyntaxOn % Use tilde (~) for spaces.

% \addto\captions<language> cannot handle macros with parameters
% We need to make our own parameters
% |1| represents parameter 1

\cs_new_protected:Npn \__rpg_caption:nn #1#2
  {
    \group_begin:
      \str_set:Nx \l_tmpa_str { #1 }
      \str_set:Nx \l_tmpb_str { #2 }
      \exp_args:NNnx \str_replace_all:Nnn \l_tmpa_str { |1| } { #2 }
      \str_use:N \l_tmpa_str
    \group_end:
  }

% Document-level version for backwards compatibility
\NewDocumentCommand{\RpgCaption}{ m m }
  { \__rpg_caption:nn {#1} {#2} }

% Define all strings as new macros instead of hardcoding them in the
% TeX files. This then allows us to add captions for multilanguage support.

% If you add or update a caption here, add it to languages/_template.sty
% as well.

%% Monster
% Basics
\newcommand\armorclassname{Armor~Class}
\newcommand\hitpointsname{Hit~Points}
\newcommand\speedname{Speed}

% Ability Scores
\newcommand\strstatname{STR}
\newcommand\dexstatname{DEX}
\newcommand\constatname{CON}
\newcommand\intstatname{INT}
\newcommand\wisstatname{WIS}
\newcommand\chastatname{CHA}

% Details
\newcommand\skillsname{Skills}
\newcommand\dimmname{Damage~Immunities}
\newcommand\dvulname{Damage~Vulnerabilities}
\newcommand\dresname{Damage~Resistances}
\newcommand\cimmname{Condition~Immunities}
\newcommand\savesname{Saving~Throws}
\newcommand\sensesname{Senses}
\newcommand\defaultsensesname{passive~Perception~10}
\newcommand\languagesname{Languages}
\newcommand\challengename{Challenge}
\newcommand\pbonname{Proficiency~Bonus}
\newcommand\xpname{XP}

% Attacks
\newcommand\unitsname{ft.}
\newcommand\weaponname{Weapon}
\newcommand\spellname{Spell}
\newcommand\meleeattackname{Melee~|1|~Attack}
\newcommand\rangedattackname{Ranged~|1|~Attack}
\newcommand\meleeorrangedattackname{Melee~or~Ranged~|1|~Attack}
\newcommand\orname{or}
\newcommand\tohitname{|1|~to~hit}
\newcommand\defaulttargetsname{one~target}
\newcommand\reachname{reach}
\newcommand\rangename{range}
\newcommand\hitname{Hit}
\newcommand\damagename{damage}
\newcommand\plusname{plus}

% Spell Levels
\newcommand\spellcantripsname{Cantrips}
\newcommand\spellfirstlevelname{1st~level}
\newcommand\spellsecondlevelname{2nd~level}
\newcommand\spellthirdlevelname{3rd~level}
\newcommand\spellfourthlevelname{4th~level}
\newcommand\spellfifthlevelname{5th~level}
\newcommand\spellsixthlevelname{6th~level}
\newcommand\spellseventhlevelname{7th~level}
\newcommand\spelleighthlevelname{8th~level}
\newcommand\spellninthlevelname{9th~level}

% Spell Slots
\newcommand\spellatwillname{at~will}
\newcommand\spelloneslotname{1~slot}
\newcommand\spelltwoslotsname{2~slots}
\newcommand\spellthreeslotsname{3~slots}
\newcommand\spellfourslotsname{4~slots}

% Innate spellcasting
\newcommand\innateatwillname{At~will}
\newcommand\numberperdayname{|1|/day}
\newcommand\numberperdayeachname{|1|/day~each}

% Spell Header
\newcommand\spellcastingtimename{Casting~Time}
\newcommand\spellrangename{Range}
\newcommand\spellcomponentsname{Components}
\newcommand\spelldurationname{Duration}

% Miscellaneous
\newcommand\pageabbreviationname{pg.}
\newcommand\dname{d}
\newcommand\tocchapterabbreviationname{Ch.}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Adding languages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To add captions for other languages:
% 1. Copy and paste languages/_template.sty to languages/<language>.sty
% 2. Substitute the <language> token in the file with the language name
%    (i.e. \addto\captionsfrench).
% 3. Replace the English strings with the translations and comment any captions
%    that you do not translate. Please keep the captions in the same order.
% 4. Add \RequirePackage{lib/languages/<language>} to the following function.
\cs_new_protected_nopar:Npn \__rpg_input_languages:
  {
    \RequirePackage{lib/languages/italian}
    \RequirePackage{lib/languages/japanese} % Requires LuaTeX or XeTeX
    \RequirePackage{lib/languages/ngerman}
    \RequirePackage{lib/languages/russian}
    \RequirePackage{lib/languages/spanish}
    \RequirePackage{lib/languages/french}
    \RequirePackage{lib/languages/portuguese}
}

% Delay loading captions until after the user has imported a language package.
% Enables using babel/polyglossia with the rpgbook document class as well as the
% rpg package.
%
% Both language packages use \AtBeginDocument. Use \AtEndPreamble (etoolbox)
% instead to side-step load order issues.
\AtEndPreamble{
  % Check if either babel or polyglossia have been loaded,
  % in which case load the string captions
  \@ifpackageloaded{babel}{
    \__rpg_input_languages:
  }{}
  \@ifpackageloaded{polyglossia}{
    \__rpg_input_languages:
  }{}
}
