-
I'm trying to create a localized version of an Eleventy site, into English and French. I have a base template,
In the same
I'm trying to set the The code I've marked in bold (just after the opening
Can anyone point me in the direction of how the affected code should be written to point to the right header component, based on locale, please? I've tried various ways to do it, but so far with no joy... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
@alexlibby This worked for me: https://github.com/pdehaan/11ty-2190 A few notes:
But feel free to check out my repo, which should have a working copy of the sample code above. // ./src/_data/site.js
module.exports = {
name: "Creative42",
baseUrl: "http://localhost:8080/",
en: {
componentsFolder: "components/en",
header: "components/en/header.njk",
},
fr: {
componentsFolder: "components/fr",
header: "components/fr/header.njk",
},
}; Fun aside if you're bored… If your template doesn't specify a {% set localeHeader = site[locale].header %}
{% include localeHeader %} Nunjucks seems to handle the I added some debugging code with my French template, with the locale='fr'
site[locale]={
componentsFolder: 'components/fr',
header: 'components/fr/header.njk'
}
site[locale].header='components/fr/header.njk'
localeHeader='components/fr/header.njk'
...
{% include 'components/fr/header.njk' %} But if I comment out the locale=undefined
site[locale]=undefined
site[locale].header=undefined
localeHeader=undefined
...
{% include undefined %}
So my /en/index.njk (with an undefined
Nunjucks does have a very useful "ignore missing" option to suppress errors if a template does not exist: https://mozilla.github.io/nunjucks/templating.html#include One workaround is to default the {% set localeHeader = site[locale].header or "_missing_file_" %}
{% include localeHeader ignore missing %} Or you could use the Nunjucks built-in {% set localeHeader = site[locale].header | default("default-header.njk") %}
{% include localeHeader ignore missing %} |
Beta Was this translation helpful? Give feedback.
-
Hi pdehaan, Many thanks for taking the time to go through and get it working - I've run up a copy of your repo, and it does indeed seem to do as I was looking for, so will try to transcribe that into my code over the next couple of days. In terms of the locale in the second half of your reply: the code I have was based on having something like
...and so on. However, when I tried to specify a locale, I don't have an explicity site[locale] value in my code, so guess that would be why it was failing. In addition, I was referencing site.js as my data dictionary from the root of the site, and not from within the en folder - could this be why I was getting nothing back when trying to concatenate values together, as it was expecting to get the locale from the same level, not a level higher? |
Beta Was this translation helpful? Give feedback.
@alexlibby This worked for me: https://github.com/pdehaan/11ty-2190
A few notes:
en.header
andfr.header
keys. I copy/pasted the code above, but noticed the data only includeden.componentsFolder
reference which pointed to a header template.But feel free to check out my repo, which should have a working copy of the sample code above.