assemble / grunt-assemble-i18n

Assemble middleware for adding i18n support to projects.
24 stars 8 forks source link

problem with translation strings for pages #59

Open lhtdesignde opened 9 years ago

lhtdesignde commented 9 years ago

Hi guys,

i'm using this plugin quite heavily at the moment and am happy that it works for me almost 100%. So great work! I do have one compiling issue with the pages feature though. I have around 8 pages which have a custom title. I'm generating the navigation out of the page titles:

{{#withSort pages "data.sortorder"}}
    {{#is data.language ../language}}
    <li class="nav-item item-{{this.basename}}{{#if this.isCurrentPage}} active{{/if}}">
        <a href="{{relative dest this.dest}}">{{ data.title }}</a>
    </li>
    {{/is}}
{{/withSort}}
title: '<%= i18n[language].H_I_02 %>'
layout: 'lyt-default.hbs'
description: 'press page'
sortorder: 3

in the json:

    "H_I_01": "FAQs",
    "H_I_02": "Press",
    "H_I_03": "Contact",
    "H_I_04": "Newsletter",

For around half of the pages the translation of the titles works but for some pages I get just the string rendered out into the navigation. So on some pages I get the following navigation: FAQs | <%= i18n[language].H_I_02 %> | Contact | Newsletter OR <%= i18n[language].H_I_01 %> | <%= i18n[language].H_I_02 %> | Contact | Newsletter

I'm not sure how this happened. All pages have the same implementation of the title string. Also weird that on the page itself the string is fine. So in this example above, if I'd click the weird string which is supposed to be press, the navigation shows correctly on the press page.

I tried to reduce the complexity of how the pages are rendered out (without sort, without url etc) but it's always the same result.

Do you have any idea what went wrong? In which area would I need to look, to fix this?

Thanks!

LaurentGoderre commented 9 years ago

I encountered this too but I forgot how I solved it! The problem is that Assemble processes page in order and only do the replacements when rendering the page. What you are seeing is that the translation works for page that have been processed before that current page but the ones after have not yet been replaced.

LaurentGoderre commented 9 years ago

Ok, so how I fixed this is I created a custom plugin registered on the render:pre:pages stage loops through all the page and perform the replacement manually for the title. It kinds of bypass this plugin just for titles.

lhtdesignde commented 9 years ago

Do you maybe have an example of this custom plugin?

LaurentGoderre commented 9 years ago

Here is the plugin I wrote for this:

/**
 * postprocess
 * @param  {Object}   params
 * @param  {Function} callback
 */

var async = require('async');
var _ = require('lodash');

var options = {
  stage: 'render:pre:pages'
};

module.exports = function (params, callback) {
  'use strict';

  var assemble       = params.assemble;
  var grunt          = params.grunt;

  var options        = assemble.options.permalinks;
  var pages          = assemble.options.pages;

  async.forEach(pages, function(page, next) {

    // TODO: traversal of the data object to expand more than just the title property
    page.data.title = _.template(page.data.title)(page.data);

    grunt.verbose.ok('Pre-processed:'.yellow, page.dest);
  });

  callback();
};

module.exports.options = options;
lhtdesignde commented 8 years ago

sorry for the late answer. wasn't able to try it until now but this works. thank you!

d-rogaczewski commented 8 years ago

hey, i have the same problem but i don't know how to build this plugin. Do you can explain how i can use your plugin? would be greatly appreciated.