grunt-assemble-i18n icon indicating copy to clipboard operation
grunt-assemble-i18n copied to clipboard

`i18n` helper inside `each` helper results in: The 'language' parameter is not defined

Open rauberdaniel opened this issue 8 years ago • 16 comments

I am trying to use the i18n helper inside an each helper loop. However, trying to do this will result in: Warning: The 'language' parameter is not defined Use --force to continue.. Maybe this is some issue about the scope?

{{#each press.releases}}
    <li class="press-release">
        <h3>{{i18n title}}</h3> <!-- Should use the title of the press release as the translation key -->
    </li>
{{/each}}

I’m using [email protected] and [email protected]

Note: It also does not work if I use a fixed string as translation key, e.g. {{i18n "site.title"}}.

rauberdaniel avatar Mar 15 '16 09:03 rauberdaniel

@rauberdaniel Thanks for the issue! If you're reporting a bug, please be sure to include:

  • The version of assemble you are using.
  • Your assemblefile.js (This can be in a gist)
  • The commandline output. (Screenshot or gist is fine)
  • What you expected to happen instead.

assemblebot avatar Mar 15 '16 09:03 assemblebot

This is due to the nature of handlebars. The each block creates a new context and that context doesn't have a language parameter. A workaround is to specify the language from the parent context.

{{#each press.releases}}
    <li class="press-release">
        <h3>{{i18n title language=../language}}</h3> <!-- Should use the title of the press release as the translation key -->
    </li>
{{/each}}
``

LaurentGoderre avatar Mar 15 '16 13:03 LaurentGoderre

@LaurentGoderre Thank you for your help! By doing this I receive a Warning: No strings found for language 'en' Use --force to continue. (also when using a fixed string instead of the title variable (which is definitely available)). Are the given translation JSON files also not available in the new context? Any idea how I can make them available there?

rauberdaniel avatar Mar 15 '16 16:03 rauberdaniel

i18n is a bit complicated. There is the handlebars helper that allows to output translated strings and independently but related the assemble extensions allow to create several language pages using a common template. I'm not sure which part is failing.

Are you using the both or just the handlebars helper? Also, do you have a repo I can see?

LaurentGoderre avatar Mar 15 '16 16:03 LaurentGoderre

if title is on the page context, try doing {{i18n ../title language=../language}} inside the each block. also, try doing {{log this}} to see the context inside and outside the block to help debug

jonschlinkert avatar Mar 15 '16 17:03 jonschlinkert

When was {{log}} added??? That would have save me some much time and grief!!!!

LaurentGoderre avatar Mar 15 '16 17:03 LaurentGoderre

When was {{log}} added??? That would have save me some much time and grief!!!!

haha, you might want to sit down @LaurentGoderre. I believe log has been there since the very early days of assemble. but I know the feeling lol

jonschlinkert avatar Mar 15 '16 19:03 jonschlinkert

At least I know now

LaurentGoderre avatar Mar 15 '16 19:03 LaurentGoderre

@jonschlinkert No, title is in the each context, so that should be fine. The warning (which maybe should be an error) says that there are no strings found at all for language 'en'… missing just one string would return a different warning (No string for key … for language 'en').

@LaurentGoderre Actually I think that I’m only using the helper, which probably would mean I’m totally wrong here, right? :confused:

rauberdaniel avatar Mar 16 '16 09:03 rauberdaniel

I just logged the page context and the each context and noticed that the translations are actually stored in the de and en properties of the page context, which probably is the issue because they do not get passed to the each context (like the language property). Any Idea on how to achieve this?

rauberdaniel avatar Mar 16 '16 09:03 rauberdaniel

I vaguely remember there was a way around this issue where you could redefine the context.

It might be easier to investigate if there is a way to look up the chain for the translation but I remember not being able to last time I looked into it

LaurentGoderre avatar Mar 16 '16 13:03 LaurentGoderre

Can you try this:

{{#each press.releases}}
    <li class="press-release">
        <h3>{{#with ..}}{{i18n title}}{{/with}}</h3> <!-- Should use the title of the press release as the translation key -->
    </li>
{{/each}}

LaurentGoderre avatar Mar 16 '16 13:03 LaurentGoderre

@LaurentGoderre Haven’t tried it yet but I guess the problem there will be that title also will come from the page context, which would be wrong. However, I found a way which currently works for me:

Instead of putting the translations in de.json and en.json I can put the translations for each press release in the press.releases object itself, e.g.

{
  "releases": [
    {
      "date": "2016-03-16",
      "de": {
        "title": "Deutscher Titel",
        "teaser": "deutsch…"
      },
      "en": {
        "title": "English Title",
        "teaser": "english…"
      }
    }
  ]
}

rauberdaniel avatar Mar 16 '16 17:03 rauberdaniel

That is a crafty way! I never thought of doing it this way

LaurentGoderre avatar Mar 16 '16 17:03 LaurentGoderre

I really like it!

LaurentGoderre avatar Mar 16 '16 17:03 LaurentGoderre

how to use {{#with}} in nested loops??? like {{#each....}} {{#each....}} {{#with}} {{/each}} {{/each}}

It doesn't work. PLease help

deepali52 avatar Mar 11 '20 04:03 deepali52