meteor-messageformat icon indicating copy to clipboard operation
meteor-messageformat copied to clipboard

mf() versus {{mf }} bug?

Open tvld opened this issue 10 years ago • 13 comments

I have a template with this: <h1>{{mf "more-title" "More about us"}}</h1> And a js file with this: console.log(mf('more-title'));

Result: the log output shows more-title Expected: More about us

Info: mf_extract -v shows:

  ./views/moreinfo/client/moreinfo.html
   more-title: "More about us"

   ./views/global/main/client/moreinfo.js
   more-menutitle: "About"

tvld avatar Jul 05 '15 23:07 tvld

Do you see the same behaviour if you specify a locale?

I don't recall the exact behaviour in v1... on the server you need to specify the locale, although in v2 (and maybe the last release we made in v1), we can detect it automatically per-user inside of publish functions & method calls. However, it should be falling back to native either way, which clearly exists... it's possible that fallback code in broken is v1 (a bug) but realistically it never really makes sense to call mf() on the server without specifying a locale to use.

gadicc avatar Jul 06 '15 06:07 gadicc

Thank you, but I must admit I am not sure what you mean exactly, but I have in:

/lib/languages/message_format.js mfPkg.init('en');

Is that what you mean with specify the locale? Or should I use some Session as well?

If v2 does not have this issue, I suppose we better wait for that and until then use a work-around on this problem...?

tvld avatar Jul 06 '15 08:07 tvld

Just had a quick look at the v0 code, this should be working fine. Is it possible you're using that line super early before the database has fully loaded? Where are you using this exactly? If you wrap it in Meteor.startup(), do you see any difference?

gadicc avatar Jul 06 '15 08:07 gadicc

I use this in the navbar. I populate the menu titles with js:

var menuItems= [
        {    menu_title: mf('more-title'),
             menu_url: "/moreinfo" },
        {     menu_title: mf( 'other-title'), 
              menu_url: "/otherpage"}
    ];

console.log(mf('more-title'));

/***  populate menus */
Template.NavTopbar.helpers({
    menu_items: menuItems
});
<template name="MoreInfo">
     <h1>{{mf "more-title" "More about our solution"}}</h1>
</template>
<template name="Otherrrrrr">
     <h1>{{mf "other-title" "Welcome to my Other page"}}</h1>
</template>
<template name="NavTopbar">
    <ul class="nav navbar-nav">
        {{#each menu_items}}
            <li>    <a href="{{menu_url}}">{{menu_title}}</a> </li>
        {{/each}}
    </ul>
</template>

As soon as I use mf('more-title') , the template renders more-title and no longer More about our solution

tvld avatar Jul 06 '15 08:07 tvld

If you type console.log(mf('more-title')); in the JavaScript console, does it work?

I think you're just trying to access the lang data before it's had a chance to load. What if you just make it all reactive?

Template.NavTopbar.helpers({
  menu_items: function() {
    return [
        { menu_item: mf('more-menutitle', 'About'), 
            menu_title: mf('more-title'),
            menu_url: "/moreinfo" },

        { menu_item: "Google", 
            menu_url: "http://www.google.com"}
    ];
  }
});

You'd need this setup anyway in case the user ever changes languages :)

gadicc avatar Jul 06 '15 08:07 gadicc

Oh and thanks, the code helped... I was going in the totally wrong direction until I saw exactly what you were trying to do.

gadicc avatar Jul 06 '15 08:07 gadicc

console.log(mf('more-title'))
more-title
undefined

Ps - Tried to clarify example above a bit more

tvld avatar Jul 06 '15 08:07 tvld

Hey, thanks for your patience.

So you're saying that the template displays correctly, but then after using the console.log line, the template then breaks? That definitely sounds like something quite serious, calling mf() should not be able to change anything. Did I understand that right?

Either way, did you try making your helper reactive and see how that affects things? (Like in the example I gave, where the helper returns a function that depends on mf() (which is reactive), rather than on on a prebuilt, static object? This is a change you'd have to make anyway if you want the user to be able to change languages.

gadicc avatar Jul 06 '15 15:07 gadicc

Your solution is the same. And no, it is not really breaking. What happens is as soon as we have mf('more-title'); it takes that and ignores the {{mf "more-title" "More about our solution"}} If the labels dont match anymore, for example I make it mf('mmmmore-title'); {{mf "more-title" "More about our solution"}} show correctly.

As I can reproduce 100% accurate on my local.. but.... hold your horses, it seems that on http://meteorpad.com/pad/jajJDwuX6qJxomr2v/Copy%20of%20Leaderboard it is running fine, while I just cut and past the code !!!

Can you see this meteorpad? First time I use it

tvld avatar Jul 06 '15 16:07 tvld

Small bug... maybe. It seems mf_extract -v does not ignore javascript // comments. If behind // js code, it is parsed as well. That said, do I need* mf_extract* via npm at all? I removed mf_extract also from on dev, and MF still working...

tvld avatar Jul 06 '15 16:07 tvld

Ok, sorry Gadi, I rebuilt same code on Cloudi9 and there I found bug as well. I got worried it was in my own dev ;)

screenshot from 2015-07-06 21 28 29 screenshot from 2015-07-06 21 29 12

tvld avatar Jul 06 '15 19:07 tvld

@tvld thanks for the all the hunting and detailed reproduction! I'm looking into this and will report back when I have more info. I'm pretty sure I see what's going on.

And yes, we probably shouldn't be extracting from within comments :>

mf will work fine without mf_extract installed, assuming it's run once... you just won't be able to update your translations unless you do it by hand. In the in-development v2, we did away with mf_extract and we just update on any save in the dev environment.

gadicc avatar Jul 07 '15 15:07 gadicc

My pleasure. Let me know if I can help. Tom

tvld avatar Jul 08 '15 08:07 tvld