Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create new custom functions as the built in functions...? #78

Closed
fernandogmar opened this issue Dec 20, 2017 · 4 comments
Closed

Comments

@fernandogmar
Copy link

Hi again

I am trying to do this kind of function:

game_message = { FLAG($is_ready) ->
    *[inactive]  Insert coin
     [active]   Choose your hero
}

But no idea how to do it... I don't know how to get the flag_value:

    const functions = {
        FLAG(flag_value) {
            return flag_value? 'active' : 'inactive';
        }
    };
    const mc = new MessageContext(config.locale, { functions });
@stasm
Copy link
Contributor

stasm commented Dec 20, 2017

(Please file issues about the JavaScript implementation in https://github.com/projectfluent/fluent.js.)

Fluent Functions, including custom builtins, take positional arguments as well as keyword arguments, which isn't supported by JavaScript out of the box. So instead, we make them accept two arguments: an array of positional args and an object keyword args.

See the example of NUMBER here: https://github.com/projectfluent/fluent.js/blob/dddb5db19bd5fc6a95a251cd7059cf26f187548d/fluent/src/builtins.js#L17

And here's the line which calls Functions: https://github.com/projectfluent/fluent.js/blob/dddb5db19bd5fc6a95a251cd7059cf26f187548d/fluent/src/resolver.js#L417

I think your example should work if you change the FLAG function as follows:

// Destructure the array of positional arguments to flag_value.
FLAG([flag_value]) {
    return flag_value? 'active' : 'inactive';
}

@stasm
Copy link
Contributor

stasm commented Dec 20, 2017

BTW this is a really nice usage of custom builtins!

@fernandogmar
Copy link
Author

fernandogmar commented Dec 20, 2017

Thanks!
I was having a look previously to NUMBER builtin... but I didn't get the right answer 😅 And probably because I had to fix another issue first...

So now your suggestion does the trick really well ;)

// Destructure the array of positional arguments to flag_value.
FLAG([flag_value]) {
    return flag_value? 'active' : 'inactive';
}

Thanks again for your fast answer ;) I will post anything related with the implementation on fluent.js next time ;)

@stasm
Copy link
Contributor

stasm commented Dec 20, 2017

I'm happy I was able to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants