-
I'm trying to display collections on my homepage, in the current pages locale. Been trying various things but nothing I try actually even displayed anything... module.exports = {
layout: "page",
tags: ["products_{{ locale }}"],
permalink: "{{ locale }}/{{ slug or page.fileSlug }}/",
}; I'm trying to create Collections by localizing the tag associated with each collection. <div class="content flow">
{% set posts = collections["products_" + locale] %}
{% for post in posts %}
{{ locale }}
<strong>{{post.data.title}}</strong>
{% endfor %}
</div> The above leads to no output at all, whereas when I remove the locale from the tags in my datafile, and also from the loop, the following is output (which is also expected): // english homepage
<div class="content flow">en <strong>Cool List</strong> en <strong>Coole Liste</strong></div>
// german homepage
<div class="content flow">de <strong>Cool List</strong> de <strong>Coole Liste</strong></div> This shows that the locale setup works, but my brain not so much :D Expected result would be though: // english homepage
<div class="content flow">en <strong>Cool List</strong></div>
// german homepage
<div class="content flow">de <strong>Coole Liste</strong></div> Ideally, what I would like to achieve is to re-use all my layouts and have 11ty automatically spit out the content only for the currently processed language. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 20 replies
-
I have 3 very rough debugging ideas. But first, a couple little filters: eleventyConfig.addFilter("inspect", require("util").inspect);
eleventyConfig.addFilter("keys", (obj) => Object.keys(obj).sort());
|
Beta Was this translation helpful? Give feedback.
-
Thanks @pdehaan I've done all the above on my {% set posts = collections["products_" + locale] %}
{{ posts | inspect }}
{% for post in posts %}
<strong>{{post.data.title}}</strong>
{% endfor %}
{{ collections | keys | inspect }}
{{ tags | inspect }} And this is the output on the page:
I would have expected at least some other tags to exist, because i'm also setting tags in other pages: module.exports = {
layout: "page",
tags: ["pages"],
permalink: "{{ locale }}/{{ page.fileSlug }}/",
}; But in regards to the localization, this is indeed all undefined and / or not substituted. |
Beta Was this translation helpful? Give feedback.
-
This "kinda" works haha :) I am a little bit lost myself now, what you came up with gives us the desired result of multi-lingual collections. To elaborate, I added the following to my const locales = ["de", "en", "es", "fr"];
const collectionMap = [
{category: "products", locales: locales},
{category: "articles", locales: locales},
{category: "reviews", locales: locales}
];
for (const {category, locales} of collectionMap) {
for (const locale of locales) {
const key = `${category}_${locale}`;
config.addCollection(key, collectionApi => collectionApi.getFilteredByTag(key));
}
} Then added this to my {{ collections | keys | inspect | safe }}
{% for lander in collections["products_" + locale] %}
<strong>{{lander.data.title}}</strong>
{% endfor %} Then I removed the module.exports = {
layout: "page",
permalink: "{{ locale }}/{{ slug or page.fileSlug }}/",
}; And the result displayed on my homepage was just this:
I then tried adding the module.exports = {
layout: "page",
tags: ["products_de"],
permalink: "{{ locale }}/{{ slug or page.fileSlug }}/",
}; And this time the actual output on the German homepage was what I would expect (Minus the fact that now obvs. all collections are hardcoded to be "products_de"). <div class="content flow">[ 'all', 'articles_de', 'articles_en', 'articles_es', 'articles_fr', 'pages', 'products_de', 'products_en', 'products_es', 'products_fr', 'reviews_de', 'reviews_en', 'reviews_es', 'reviews_fr' ]
<strong>Cool List</strong>
<strong>Coole Liste</strong>
</div> I'm totally confused now, I thought that 11ty creates Collections one of two ways:
Now, we used the I'm totally in love with 11ty, but it seems there is still a lot I don't know. |
Beta Was this translation helpful? Give feedback.
-
Ahh, sorry! I re-read what you wrote before, and realized you already posted the answer to what confused me :) I did exactly that now, and low and behold: All homepages display the content in their own language/or are empty if there is no content for that language. Perfect :) Thanks so much for your help. I'm curious if this will all just work now when I start adding localized categories. |
Beta Was this translation helpful? Give feedback.
I have 3 very rough debugging ideas.
But first, a couple little filters:
{{ collections | keys | inspect }}
. That should hopefully tell us which collection names are currently created. My guess is that there isn't acollections.products_en
created.{{ tags | inspect }}
on one of your pages to make sure that it's doing what you think it's doing.{{ posts | inspect }}
right after{% set posts = collections["products_" + locale] %}
and confirm that it returns an array of some products. I'm guessing the value is an …