[Tex/LaTex] \today and internationalization

babellanguages

I am writing a LaTeX document with internationalization support as explained on Wikibooks. Here is my minimal file:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[english,frenchb,spanish]{babel}

%% Required package.                                                                                                                                                   
\usepackage{ifthen}

%% TeX function that generates the language commands.                                                                                                                  
\def\localedef#1#2{
\ifthenelse{ \equal{\locale}{#1} }{
  \selectlanguage{#2}
  \expandafter\def\csname#1\endcsname ##1{##1}
  }{
  \expandafter\def\csname#1\endcsname ##1{}
  }
}

%% Selected language. Can be placed anywhere before the language commands.                                                                                             
\providecommand\locale{fr}

%% Language commands.                                                                                                                                                  
\localedef{en}{english}
\localedef{fr}{frenchb}
\localedef{es}{spanish}

\usepackage{hyperref}

\begin{document}

\title{\en{Example of \LaTeX \ document with multi-language support}                                                                                                   
\fr{Exemple de document \LaTeX \ avec support multi-langues}                                                                                                           
\es{Ejemplo de documento con soporte multi-idioma}}
\author{\en{Author}\fr{Auteur}\es{Autor}}
\date{\today}
\maketitle

\en{Hello, this document was written based on explanations available here}
\fr{Bonjour, ce document a été écrit en suivant les explications disponibles ici}
\es{Buenos dias, este documento fue elaborado sobre la base de las explicaciones disponibles aquí}
:

\url{https://en.wikibooks.org/wiki/LaTeX/Internationalization}

\end{document}

As you can see, I can easily switch between languages and here I chose French (\providecommand\locale{fr}). When I compile with pdflatex and visualize with evince, everything works fine except that the date, given by \today, is in Spanish:

enter image description here

What did I do wrong?

ps: I'm on Xubuntu 14.04 and all my packages are up-to-date.

Best Answer

Rewriting your selection code a little you can make the choice dependent on the language active at the beginning of the document. This way you just need to write, e.g.

\AtBeginDocument{\selectlanguage{french}}

to choose the French variant.

Sample output

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{etoolbox}
\usepackage[english,french,spanish]{babel}
\usepackage[hyphens]{url}
\usepackage{hyperref}

\AtBeginDocument{\selectlanguage{french}}

\newcommand{\localedef}[2]{\csdef{#1}##1{\iflanguage{#2}{##1}{}\ignorespaces}}
\localedef{en}{english}
\localedef{fr}{french}
\localedef{es}{spanish}

\begin{document}

\title{\en{Example of \LaTeX\ document with multi-language support}
\fr{Exemple de document \LaTeX\ avec support multi-langues}
\es{Ejemplo de documento \LaTeX\ con soporte multi-idioma}}
\author{\en{Author}\fr{Auteur}\es{Autor}}
\date{\today}
\maketitle

\en{Hello, this document was written based on explanations available here}
\fr{Bonjour, ce document a été écrit en suivant les explications disponibles ici}
\es{Buenos dias, este documento fue elaborado sobre la base de las explicaciones disponibles aquí}:
\begin{quote}
  \url{https://tex.stackexchange.com/questions/186033/today-and-internationalization}
\end{quote}

\end{document}

The above \localedef just detects whether the current language fits with your provided abbreviation and if so outputs its text, otherwise it outputs nothing. I have added an \ignorespaces to the end of this command as, like in your example, the different variants often start on new lines, which potentially adds an undesirable space to the output. I have used commands from etoolbox for simplicity.

Note that frenchb is now depricated and one should use french instead.