adamduncan / eleventy-plugin-i18n

Eleventy plugin to assist with internationalization and dictionary translations
MIT License
103 stars 10 forks source link

Extend dictionaries on a per-page basis #24

Open chriskirknielsen opened 1 year ago

chriskirknielsen commented 1 year ago

As proposed in #23 I extended this plugin instead of providing a custom implementation: easier to see what's going on!

This adds the ability to extend the dictionary on a per-page basis by reading the available data in the context (e.g. data added in the frontmatter) and pulling it in under the i18n key.

This lets you add translations in the frontmatter for a specific page that might use a layout shared across languages, enabling you to add one-off translations that won't clutter up your base dictionaries.

Example

📄 src/en/about.njk

---
title: About
layout: layouts/about.njk
i18n:
  favouriteFood: Lasagna
---

Hi, I am an example.

📄 src/fr/about.njk

---
title: À Propos
layout: layouts/about.njk
i18n:
  favouriteFood: Lasagnes
---

Bonjour, je suis un exemple.

📄 src/_includes/layouts/about.njk

<aside>😋 = {{ 'favouriteFood' | i18n }}</aside>

The favouriteFood key does not exist in en.json or fr.json but can be accessed by adding it to each localised content file.