Repository navigation
Description
Depends on #57 ($hooks.page).
A plugin can ship config partials ($partials), but today those get only their arguments, while a file partial in partials/ also gets $page, $pages, $section, and $tags. This issue makes config partials see page values, and builds @bongo/i18n on that and on $hooks.page.
Core: config partials get page values
Today a $partials function gets only its arguments, while a file partial in partials/ also gets $page, $pages, $section, and $tags. A plugin can ship only config partials. So config partials should get the same values as file partials, merged the same way: page values, then arguments, then the config ($site, $utils, $data, $assets). Calls from Markdown or a loader still get no page values, as for file partials. An existing $partials function only sees more keys.
Plugin: @bongo/i18n
import { i18n } from '@bongo/i18n';
export default {
$site: { url: 'https://example.com/', title: 'My site', lang: 'en' },
plugins: [i18n({ languages: { en: '/', fr: '/fr/', de: '/de/' } })],
};languagesmaps each language to the folder of its pages. The language of$site.langis the default, and thex-default.- The language of a page comes from its folder, or from
langin its frontmatter. - Translations match by their path without the language folder (
/fr/contact/and/contact/are both/contact/), or by a sharedkeyin frontmatter when the slugs differ.
The plugin's page hook sets, for example on /fr/a-propos/:
{
title: 'À propos',
key: 'about',
lang: 'fr',
versions: [
{ lang: 'en', path: '/about/', url: 'https://example.com/about/', title: 'About' },
{ lang: 'fr', path: '/fr/a-propos/', url: 'https://example.com/fr/a-propos/', title: 'À propos' },
],
}versions lists every version, the page itself included, in the order of languages, as hreflang needs. A page with no translation has only itself.
Templates:
<html lang="{{ $page.params.lang }}">
<head>
{{#partial src="hreflang"}}
</head>
<body>
{{#each $page.params.versions.filter(v => v.lang !== $page.params.lang) as v}}
<a href="{{ v.path }}" hreflang="{{ v.lang }}" lang="{{ v.lang }}">{{ v.title }}</a>
{{/each}}{{#partial src="hreflang"}} writes one <link rel="alternate" hreflang> for each version, plus x-default, and nothing for a page with no translation. A sitemap loops page.params.versions for its xhtml:link elements. Layout text stays in data/.
| Today (Translations guide) | With the hook and the plugin | |
|---|---|---|
| A page's language | lang: in each translated page |
from its folder |
| Matching | key: in each page |
by path; key: only when slugs differ |
| hreflang in the layout | about 8 lines of expressions | {{#partial src="hreflang"}} |
| Work per build | pages × pages | pages (one index) |
Benchmark
Use the benchmark from #57. Generate a two-language version of the large site, and record the time and the peak memory of bongo build with @bongo/i18n, next to the numbers of #57. The plugin must build its translation index once per build, so its work grows with the page count, not with pages × pages.
Work
- Config partials get page values, with tests
-
@bongo/i18n, with tests - The Translations guide shows the plugin first, and the plain version for sites without it
- Benchmark with the plugin, numbers in the PR
Activity