3

Within the babel package, for each language, the month names are already defined as \month<roman number>name commands.

I was looking then for a solution that would provide me with the month name in the proper language using only babel and tried this:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[italian, english, french]{babel}

\newcommand{\monthnametoday}[1][\languagename]{{\expandafter\selectlanguage\expandafter{#1}\csname month\romannumeral\month name\endcsname\space -- \today\relax\par}}

\begin{document}

\noindent\monthnametoday (expected: ``septembre'')\\
\monthnametoday[italian] (expected: ``settembre'')\\
{\begin{otherlanguage}{english}
\monthnametoday (expected: ``september'')
\end{otherlanguage}}
\end{document}

Unfortunately, the result is not what I would expect :/ I obtain:

compilation result

As you can see, the \today command gets properly translated in the selected language. But the \monthXXname macros are stuck in the main language :/

Any way I could use them for that purpose? How? I've looked at the documentation of babel and its code, and did not find any hint.

Cheers

3

french is the only language that already uses this new interface. You must add the definitions for the other languages yourself:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[italian, english, french]{babel}
\StartBabelCommands*{italian}{date}
  \SetStringLoop{month#1name}{%
      gennaio,febbraio,marzo,aprile,maggio,giugno,%
    luglio,agosto,settembre,ottobre,novembre,%
    dicembre}
 \EndBabelCommands     

\StartBabelCommands*{english}{date}
  \SetStringLoop{month#1name}{%
    January,February,March,April,May,June,%
      July,August,September,October,November,%
      December}
 \EndBabelCommands


\newcommand{\monthnametoday}[1][\languagename]{{\expandafter\selectlanguage\expandafter{#1}\csname month\romannumeral\month name\endcsname\space -- \today\relax\par}}

\begin{document}


\noindent\monthnametoday (expected: ``septembre'')\\
\monthnametoday[italian] (expected: ``settembre'')\\
{\begin{otherlanguage}{english}
\monthnametoday (expected: ``september'')
\end{otherlanguage}}
\end{document}

enter image description here

  • oh my it is indeed only defined for French :/ too bad. Thanks for your answer. That means babel cannot be used as an out-of-the-box solution for mutli-language month names :/ – mhavel Sep 6 '18 at 8:08
  • a question remains though: why babel is not constructing its today command based on localized month names as for French: that would allow to build flexible custom multi-language dates. Instead each local today is "hard coded" :/ – mhavel Sep 6 '18 at 8:13
  • Another thing. babel relies on .ini files for each languages. From what I see, they all define proper month / day names (long and short versions). There must be a way to use that existing information... – mhavel Sep 6 '18 at 8:24
  • One of the goals of the ini files is to provide the ‘new style’ dates and captions for languages not definining them. See my comment above. – Javier Bezos Sep 14 '18 at 17:14
1

Ok I found a neat way, using only babel, but instead of using the default .ldf files, it uses the .ini files.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{babel} % do not load languages here
\babelprovide[import]{french} % load french .ini file
\babelprovide[import]{english} % load english .ini file
\babelprovide[import, main]{italian} % load italian .ini file, and make it the main language

\newcommand{\monthnametoday}[1][\languagename]{{\expandafter\selectlanguage\expandafter{#1}\csname month\romannumeral\month name\endcsname\space -- \today\relax\par}}

\begin{document}

\noindent\monthnametoday (expected: ``settembre'')\\
\monthnametoday[french] (expected: ``septembre'')\\
{\begin{otherlanguage}{english}
\monthnametoday (expected: ``september'')
\end{otherlanguage}}
\end{document}

Very simple and working perfectly :)

enter image description here

  • 1
    You can use both methods at the same time. That way you won't lost the additional features provided by the ldf files. – Javier Bezos Sep 14 '18 at 17:09
  • In other words, \usepackage[italian]{babel} \babelprovide[import]{italian}is legitimate. – Javier Bezos Sep 14 '18 at 17:17
  • oh cool. I did not test all the features babel provide, so good to know you can combine both. – mhavel Sep 17 '18 at 11:06

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.