eleventy-plugin-i18n icon indicating copy to clipboard operation
eleventy-plugin-i18n copied to clipboard

i18n filter not working in liquid templates

Open katrin-krieger opened this issue 3 years ago • 2 comments

Hi! First of all, thank you so much for this plugin, great job! I have encountered an issue, that when I use the i18n filter with liquid templates, eleventy throws an error.

I used the plugin demo code to reproduce the error, so I'm quite sure it's not my project. What I did was that I added {{ 'hello' | i18n }} to the file index.njk located in en-gb/ to test the filter and see if it picks up the translation from i18n/index.js

So what happens is, that if I run eleventy on the plugin demo code, it runs smooth as expected and serves the generated pages.

However, if I save the index.njk as a liquid template index.liquid and explicitly tell eleventy to use liquid as templating engine, I run into the following error:

> Having trouble rendering liquid template ./src/en-gb/index.liquid

`TemplateContentRenderError` was thrown
> Cannot read property 'page' of undefined, file:./src/en-gb/index.liquid, line:3

`RenderError` was thrown
> Cannot read property 'page' of undefined

`TypeError` was thrown:
    TypeError: Cannot read property 'page' of undefined
        at /home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/eleventy-plugin-i18n/.eleventy.js:16:40
        at /home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/@11ty/eleventy/src/BenchmarkGroup.js:30:26
        at Object.render (/home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/liquidjs/dist/liquid.common.js:1914:26)
        at /home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/liquidjs/dist/liquid.common.js:1807:21
        at Array.reduce (<anonymous>)
        at Object.evalValue$$1 [as evalValue] (/home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/liquidjs/dist/liquid.common.js:1806:29)
        at Object._callee4$ (/home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/liquidjs/dist/liquid.common.js:1786:30)
        at tryCatch (/home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/liquidjs/dist/liquid.common.js:108:40)
        at Generator.invoke [as _invoke] (/home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/liquidjs/dist/liquid.common.js:319:22)
        at Generator.prototype.<computed> [as next] (/home/kati/git/eleventy-plugin-i18n-demo-master/node_modules/liquidjs/dist/liquid.common.js:156:21)
Wrote 0 files in 0.09 seconds (v0.11.1)

I didn't change anything else, just renaming the index.njk to index.liquid and putting liquid instead of njk as value for markdownTemplateEngine in .eleventy.js

My eleventy version is 0.11.1, the plugin version is 0.1.0

What am I missing here?

Cheers Katrin

katrin-krieger avatar Mar 09 '21 10:03 katrin-krieger

Hey @katrin-krieger, thanks for the feedback and error report. I was able to reproduce this locally using liquid.

I'll have to do some digging and work out why the page context might different between templating languages.

Something in this ballpark being available in njk but not liquid 🤔

adamduncan avatar Mar 18 '21 19:03 adamduncan

I did a little more digging on this. Seems the use of this in filters is possible in Nunjucks, but untested in other templating languages.

Related: #1047, #741, #1591

We might have to revisit this assumption when v1.0.0 transforms are released

adamduncan avatar Apr 08 '21 20:04 adamduncan