# Change language on a per field basis, part 2: Fonts, case transformation and sorting

A similar question to Change language on a per field basis, but: (1) I use different fonts for Latin and Greek, and (2) I use automatic sentence case for titles.

1. Languages of the same font work fine with the autolang=hyphen package option. Different fonts (\greekfont in this case) do not. autolang=other(*) also causes translation of bibliography terms like “in” or “volume”, which I do not want to do.
2. Not modifying the case gives no problems for ad-hoc \selectlanguage{greek} for single bib fields. Modifying the case treats the command as the first letter, causing the “actual” title to start lowercase.
3. Using \selectlanguage causes the entry to sort the author as beginning with “greek”, between “Greej” and “Greel”.
4. If the author is printed in changed order (“Smith, J.” ⇒ “J. Smith”), the first name/initial is not affected by \selectlanguage.

I think one possible solution might involve a new version of autolang that only affects certain fields, only affects user-given fields, or excludes internal bibliography strings.

How to set the language for single bibliography entry fields and get it to otherwise behave like regular entries?

MWE:

\documentclass{article}
\usepackage{filecontents,fontspec,polyglossia}
\setmainfont{Latin Modern Mono}
\newfontfamily\greekfont{GFS Didot}[Color=CC0000]
\setmainlanguage{english}
\setotherlanguage{greek}
\usepackage[backend=biber,autolang=hyphen]{biblatex}
\DeclareFieldFormat{titlecase}{\MakeSentenceCase{#1}}
\begin{filecontents}{\jobname.bib}
@other{sortexample1,
author={Greej},
title={Latin alphabet},
langid={english},
}
@other{sortexample2,
author={Greel},
title={Latin alphabet},
langid={english},
}
@article{greekexample,
author={\selectlanguage{greek}Ἀριστοτέλης, Ἀ.},
journal={\selectlanguage{greek}Αὐτῆς},
title={\selectlanguage{greek}Ὑπῆρξε},
year={2000},
volume={5},
langid={greek},
}
\end{filecontents}

\begin{document}
\nocite{*}
\printbibliography
\end{document}


• This is probably not directly relevant to your question, but before I have a look at it please let me point out that biblatex's polyglossia support has some very rough edges: tex.stackexchange.com/q/432347/35864 – moewe Jul 31 '18 at 14:17
• Phenomenon 4 is 'easily' explained. If you write author={\selectlanguage{greek}Ἀριστοτέλης, Ἀ.}, you create a person whose family name is \selectlanguage{greek}Ἀριστοτέλης and whose first name is Ἀ. If you then ask for family-given name order you end up printing Ἀ. \selectlanguage{greek}Ἀριστοτέλης, so the \selectlanguage only kicks in for the family name and not for the given name initial. – moewe Jul 31 '18 at 14:24
• I know Polyglossia doesn't use class options, but passing the languages there, too, might be helpful, in that some packages will pick those options up. (Don't know how Biblatex handles this. Since @moewe didn't mention it, I guess it is irrelevant here.) – cfr Jul 31 '18 at 23:28

This is tricky. By default biblatex surrounds the entire bibliography item with one language environment. That means that the entire entry - not just the data, but also the automatically generated text and punctuation - are in the same language.

I can definitely see the appeal in what you want to get, especially if you have to switch the language for the correct font.

There are in principle two ways to do this

1. Wrap the entire entry in the otherlanguage environment and switch back the language for all bibstrings. This sounds appealing and could probably be implemented fairly straightforward. But this would typeset the entry basically entirely in the foreign language and only goes back to the main language for strings: All punctuation marks will be typeset in red in your example.

2. Patch all field formats so that they switch the language to the source language at the innermost level. We can't simply change at a higher level because \DeclareFieldFormat{volume}{\bibstring{volume}~#1} would come out in the wrong language. So we essentially turn this into \DeclareFieldFormat{volume}{\bibstring{volume}~<foreignlanguage>#1<end-foreignlanguage>}. This keeps the language changes to a minimum and confined only to the actual entry data.

• Your MWE contained one case for which this does not work: \DeclareFieldFormat{titlecase}{\MakeSentenceCase{#1}}, \MakeSentenceCase can't deal with the language environment being passed to its argument.

MWE implementing option 2

\documentclass{article}
\usepackage{filecontents,fontspec,polyglossia}
\setmainfont{Latin Modern Mono}
\newfontfamily\greekfont{GFS Didot}[Color=CC0000]
\setmainlanguage{english}
\setotherlanguage{greek}
\usepackage[backend=biber,autolang=hyphen]{biblatex}

\makeatletter
\def\blx@beglang@fake{%
\blx@clearlang
\begingroup
\ifdef\abx@field@langid
{\ifdef\abx@field@langidopts
{\csname\abx@field@langid\expandafter\endcsname\expandafter[\abx@field@langidopts]}
{\csname\abx@field@langid\endcsname}}
{}}

\def\blx@endlang@fake{%
\csname end\abx@field@langid\endcsname
\endgroup}

\renewrobustcmd*{\blx@imc@printfield}[2][]{%
\blx@imc@iffieldundef{#2}
{\blx@nounit}
{\blx@getformat\blx@theformat{ffd}{#1}{#2}%
\ifdefvoid\blx@theformat
{\blx@nounit}
{\blx@begunit
\let\blx@theformat@outer\blx@theformat
\def\blx@theformat##1{\blx@theformat@outer{\blx@beglang@fake ##1\blx@endlang@fake}}%
\edef\currentfield{#2}%
\expandafter\expandafter
\expandafter\blx@theformat
\expandafter\expandafter
\expandafter{\csname abx@field@#2\endcsname}%
\blx@endunit}}}

\long\def\blx@nameparser@i#1#2{%
\begingroup
\ifblank{#1}
{}
{\setkeys{blx@opt@name}{#1}}%
\setkeys{blx@opt@namepart}{#2}%
\blx@beglang@fake\blx@theformat{#2}\blx@endlang@fake
\endgroup}

\long\def\blx@listparser#1{%
\ifblank{#1}
{\blx@listbreak}
{\ifnum\c@listcount<\c@liststart
\else
\blx@beglang@fake\blx@theformat{#1}\blx@endlang@fake
\fi
\ifnum\c@listcount>\c@liststop
\expandafter\blx@listbreak
\fi
\blx@listparser}}
\makeatother

\DeclareFieldFormat{titlecase}{#1}
\begin{filecontents}{\jobname.bib}
@other{sortexample1,
author={Greej},
title={Latin alphabet},
langid={english},
}
@other{sortexample2,
author={Greel},
title={Latin alphabet},
langid={english},
}
@book{greekexample,
author={Ἀριστοτέλης, Ἀ.},
publisher={Αὐτῆς},
title={Ὑπῆρξε},
year={2000},
volume={5},
langid={greek},
}
\end{filecontents}