8

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.

  • 2
    The active document language is the last option you choose for babel. Since you have \usepackage[english,frenchb,spanish]{babel} the language is Spanish. – clemens Jun 22 '14 at 12:42
  • @cgnieder Ok, thanks. But do you have any idea how I could adapt this with what I want to do? Should I do \date{\en{June 22, 2014}\fr{22 Juin 2014}\es{22 de junion de 2014}} and change it by hand? That's a bit of a pain, right? – tflutre Jun 22 '14 at 12:54
  • why don't you just change the order of the options? \usepackage[english,spanish,frenchb]{babel} – clemens Jun 22 '14 at 13:08
  • @cgnieder you're right, I can do that. But I'd have hoped there was a better way. Feel free to add your answer so that I can choose it. – tflutre Jun 22 '14 at 14:56
5

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.

  • Hmm, when you do \babeltags{en=english}, the \en command gets defined and you're redefining it to something else. – egreg May 13 '18 at 16:41
  • @egreg Oh, I see - the tag defines the {en} environment which involves defining an \en command. OK - I'll update to avoid the tags shorthand. – Andrew Swann May 14 '18 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.