Use a consistent language for headers and footers with babel

babelfancyhdrright-to-left

I have a Hebrew document with some English parts, and I am using the fancyhdr package to have headers and footers. I am using \begin{otherlanguage}{english} \end{otherlanguage} for my English parts. The problem is that since this environment basically does \selectlanguage{english}, if my text spans more than one page, then the headers and the footers also change to English, which I don't like. The main language is Hebrew, and the headers and footers should always be in Hebrew even if the page happens to be in English. Here's a MWE:

\documentclass[a4paper, 12pt]{article}

\usepackage[bidi=basic, layout=tabular]{babel}

\babelprovide[main, import, alph=letters, Alph=letters]{hebrew}
\babelprovide[import]{english}

\babelfont[hebrew]{rm}{David CLM}
\babelfont[hebrew]{sf}{David CLM}

\usepackage{geometry}
\usepackage{blindtext}

\geometry{left=1cm, right=1cm, top=2cm, bottom=2cm}
\setlength{\headheight}{15pt}

\makeatletter
\newif\if@RTL \@RTLtrue
\makeatother

\usepackage{fancyhdr}
\chead{שלום 1}
\lhead{}
\rhead{}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
\pagestyle{fancy}

\begin{document}
\begin{otherlanguage}{english}
    \Blindtext[5][3]
\end{otherlanguage}
\end{document}

This issue causes my Hebrew headers to disappear since English mode doesn't use the Hebrew font (and it shouldn't).
Another side effect of this issue is that if you get to pages of more than one digit (increase the \Blindtext parameters to see this), then the page number in the footer is also reversed: instead of page 10, it shows page 01.

How can I configure the headers and footers to be in the same language regardless of which language is selected?

Best Answer

It’s a situation that occurs sometimes, so babel provides the package option headfoot to deal with it:

\usepackage[bidi=basic, layout=tabular, headfoot=hebrew]{babel}
Related Question