polyglot icon indicating copy to clipboard operation
polyglot copied to clipboard

Best practice running over all posts by category - ignoring the language

Open nbartels opened this issue 2 months ago • 1 comments

So I have a jekyll project with posts in English and the same in German, German is the default language. This is working fine, I got the language switcher running and everything, but there is one blind spot - the post search.

I use lunr.js to build a index and search posts.

The problem is, I have a data json file, that is build with jekyll and there I iterate over the posts and convert them in a custom JSON structure I need for the search. Because not all posts categories are searchable, I use {% for post in site.categories.mycat1 %} as loop. The problem is, that only the default language is used and so I only get the German posts in my search index.

Is there a way to access all posts with site.categories.X independent of the language? or can I at least define the language, so I run over the posts in English and then over the German posts?

Maybe some complete other approach is needed?

Would be great to get some input ...

nbartels avatar Oct 18 '25 01:10 nbartels

I had the same issue in my template. My solution was to create a json structure for every language inside a js, and only load the js for that language when needed (for my case I needed a whole js). What I did was:

  1. Created _scripts/search.liquid.js where I loop through all posts on the specific language. For this, I added this to its front matter:
---
permalink: /assets/js/search-data.js
---
  1. Add this directory to be included in _config.yml
  2. When loading the scripts for the page, I get the correct scripts from its path:
<script
  src="{% if site.active_lang != site.default_lang %}{{ '/assets/js/search-data.js' | prepend: site.active_lang | relative_url }}{% else %}{{ '/assets/js/search-data.js' | relative_url }}{% endif %}"
></script>

Also note that, in my case, my default language don't use any extra path (e.g., en-us), that's why I do this if ... else above.

george-gca avatar Nov 17 '25 15:11 george-gca