sile-typesetter / sile

The SILE Typesetter — Simon’s Improved Layout Engine
https://sile-typesetter.org
MIT License
1.61k stars 97 forks source link

Fluent scope leakage issue in bibliography #2054

Closed Omikhleia closed 2 weeks ago

Omikhleia commented 3 weeks ago

Language changes in bilbliography (e.g. caused by an "et al") leak beyond the expected scope.

Minimal example

Bibliography

@phdthesis{Thesis,
  author  = {Smith, Jane and Doe, John and Wilbur, Norbert and Mac, Donald},
  title   = {A Thesis},
  school  = {Institute of Technobabble},
  year    = {2016},
}

@book{Book,
  author     = {Doe, John and Smith, Jane},
  translator = {Pumpernickel, Peter},
  title      = {A Book},
  year       = {2020},
}

Document:

\begin[papersize=a6]{document}
\use[module=packages.bibtex]
\language[main=en]
\loadbibliography[file=temp/biblite.bib]

\reference{Book}

\reference{Thesis}

\reference{Book}

\end{document}

Obtained

image

The third book reference has latin text where it shoudn't.

Analysis

The bibliography package uses fluent:get_message() directly. Due to fluent not being stateless and having its own global local state, there's a scope leakage. (It does ring an old bell #1477...)

Of course, a possible quick fix would be to systematically invoke fluent:set_locale(SILE.settings:get("document.language")) before any call to fluent:get_message() in the bibliography package -- I'll leave it to the readers to judge whether it is a sane and nice solution...

Omikhleia commented 3 weeks ago

a possible quick fix would be

N.B. Now that we have hooks on settings, it might also be possible to switch fluent's global locale in such a hook...

alerque commented 3 weeks ago

Using a setting hook for this makes a lot of sense now that we have that mechanism, and I just tried it and it eliminated a lot of cognitive overhead about internals and hanky-panky calls to set the locale needed to get things done as expected.