mmistakes / minimal-mistakes

:triangular_ruler: Jekyll theme for building a personal site, blog, project documentation, or portfolio.
https://mmistakes.github.io/minimal-mistakes/
MIT License
12.05k stars 24.98k forks source link

Page locale #4862

Closed suhlig closed 3 weeks ago

suhlig commented 1 month ago

This is a feature.

Summary

This PR allows for a page to specify a content locale that is used only for that one page.

Compared to the similar PR https://github.com/mmistakes/minimal-mistakes/pull/4679, this set of changes is relatively small, as I am not going for localising anything. Instead, the focus is on expressing the actual locale of the content, as it is written in the page and declared as locale in the front matter, in the containing element's lang attribute.

Fallback to site.locale is present where applicable.

This PR has an associated commit that writes the site locale (lang attribute of the html element) in the style the W3C recommends (language and region separated by dash), instead of just cutting off the region. Let me know if you'd prefer this one as a separate PR.

Context

I am running my blog mostly in English, but a few pages are in German. I am running a spell checker across all pages before publishing them, and having an accurate locale on each page is crucial for that.

Example

A site that has locale: en_US in its _config.yaml generates the following html element:

<html lang="en-US" ...>

A page may specify that its content is written in German:

---
title: Eine Seite in Deutsch
locale: de_DE
---

Das ist der Inhalt...

With this PR, the rendered HTML looks like this:

<html lang="en-US"...>
<article class="page" ... lang="de-DE">
  <h1>Eine Seite in Deutsch</h1>
  ...

Everything is still being treated as written in the site's locale en-US, but this one page declares its main content correctly as de-DE.

iBug commented 4 weeks ago

Nice job. Can this be changed so that when a page-level override isn't present, it won't emit the lang attribute at all?

suhlig commented 4 weeks ago

Hmm, I did not think about it that way. My thought was that we always know the language (at least the default system fallback would be available).

But if it's a blocker to merging the PR I can certainly make that change.

iBug commented 4 weeks ago

My take on this is that it's a per-page settings override, so the lang tag need not be present if nothing was overridden. A secondary reason is to keep the changes at a minimal level for existing users, such that the output HTML doesn't change unnecessarily across theme versions.

suhlig commented 4 weeks ago

Makes sense. I'll update the PR.

suhlig commented 3 weeks ago

I pushed another commit that emits the lang attribute only if page.locale is set. I marked it as fixup so that it stays separate while we are discussing it, but at rebase time it should probably be squashed with the previous one.