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

Message: Support ICU MessageFormat using slexaxton/messageformat.js #321

Closed
wants to merge 3 commits into from

Conversation

rxaviers
Copy link
Member

@rxaviers
Copy link
Member Author

The first issue I hit passing Globalize's plural fn to MessageFormat is that MessageFormat's compiled fn can't find variable Globalize. Because, new Function creates a new scope and the compiled code expects the plural function to be globally available.

@rxaviers
Copy link
Member Author

Can we change the compiled function to actually expect the plural function as a parameter? I'm hacking/experimenting that now.

@SlexAxton
Copy link
Contributor

Hey @rxaviers - looking at this now. Things are looking pretty green. What's your read on what we should change in messageformat to support your use-case?

@rxaviers
Copy link
Member Author

rxaviers commented Oct 1, 2014

@SlexAxton, thanks for asking.

I need to pass a plural function to messageformat. Currently, this is only possible if the function is self-contained or if it refers to globals. A simple test case demonstrating what I mean: https://gist.github.com/rxaviers/ec8443f489f1d8d0d454.

So, the very first minimal change I need is to be able to pass the plural function as a parameter to the compiled function. That's the easiest part and I should be able to create a custom new Function myself using mf.precompile(mf.parse()). Although, both the parse and runtime functions refer a global variable.

In this PR, I add a "runtime" hack to change that by replacing such occurrences, and it works as expected.

I've initiated a change in this branch. But, some tests still fail (I'm running tests with make publish && make).

Overall, is this something that could be changed in messageformat in your opinion?

Ideally, I wondered if I could access the core MessageFormat parts more directly. If your code were modularized like modernizr or this repo, I could access the source pieces instead of the bundle. Perhaps that would make it easier. Since, MessageFormat currently involves a build step, nothing would change on that regard. What do you think?

@rxaviers
Copy link
Member Author

rxaviers commented Oct 1, 2014

@SlexAxton If you are open to changing your API slightly (approximating to Strawman Draft), we could have messageformat/messageformat#65. This would definitely solve the above.

@eemeli
Copy link

eemeli commented Oct 1, 2014

@rxaviers, I really like what you did replacing i18n with local functions. I hadn't thought of that when I put together the current formulation.

So I sort of completed what you started, here. That includes updates to bin/messageformat.js and the tests, so it compiles and tests out all right. I took the liberty to refactor your var runtimeFunctions and mf.renderRuntime to an object mf.runtime with a toString() method (where mf = new MessageFormat(...)), as I thought it made more semantic sense that way. Also, var g is now f.lc, and includes all the defined locales -- they're needed to make the locale choice a runtime rather than compile time option, so bin/messageformat.js can produce output supporting multiple simultaneous locales.

Internally, mf.runtime.toString() currently refers to MessageFormat.locale to make sure that MessageFormat.locale = ... won't create problems if run after declaring mf, but I'm not sure if that isn't too much handholding, and hobbling the possibility to define a custom plural function via mf.runtime.lc. What do you think?

Ideally, I wondered if I could access the core MessageFormat parts more directly. If your code were modularized like modernizr or this repo, I could access the source pieces instead of the bundle.

I know the question wasn't addressed to me, but can you clarify what you mean? Because I don't think there's really that much that can be modularized further from messageformat.js...

@SlexAxton
Copy link
Contributor

Yea, I'm definitely open to supporting those use cases (especially not relying on local or global variables only).

I'm a big fan of this change: https://github.com/eemeli/messageformat.js/compare/fix-x-plural-fn?expand=1 -- When we were initially discussing how to do this, this was what I was trying to advocate for (having the functions just available on the runtime instead of compiled into the output), so I'm very much in favor of these changes.

Ideally, I wondered if I could access the core MessageFormat parts more directly. If your code were modularized like modernizr or this repo, I could access the source pieces instead of the bundle.

I agree both that modularization is a good thing, but also with @eemeli in that I'm not sure what we could pull out.

@rxaviers
Copy link
Member Author

rxaviers commented Oct 1, 2014

Awesome, excellent guys. Thank you for the prompt answers. @eemeli thanks for the super speedy commits. By the way, mf.runtime and mf.runtime.toString() have been nice changes.

I've updated this branch using the latest code and I could remove all the hacks! Great!

Internally, mf.runtime.toString() currently refers to MessageFormat.locale to make sure that MessageFormat.locale = ... won't create problems if run after declaring mf, but I'm not sure if that isn't too much handholding, and hobbling the possibility to define a custom plural function via mf.runtime.lc. What do you think?

It works for me :). We could rethink that on messageformat/messageformat#65.

@SlexAxton and @eemeli,

Ideally, I wondered if I could access the core MessageFormat parts more directly. If your code were modularized like modernizr or this repo, I could access the source pieces instead of the bundle.

I don't want to distract you with that. So, we can first focus on messageformat/messageformat#65. So, please, ignore it for now.

@rxaviers rxaviers force-pushed the fix-265-message-format branch 2 times, most recently from 8afd6bd to 18a5d50 Compare October 8, 2014 21:07
@jzaefferer
Copy link
Contributor

@SlexAxton any update on the license? That's the only blocker for this to land (adding support for message format to Globalize).

@SlexAxton
Copy link
Contributor

If someone can PR adding the necessary license to the messageformat repo,
I'll merge it right away.

On Thu, Dec 11, 2014, 9:11 AM Jörn Zaefferer [email protected]
wrote:

@SlexAxton https://github.com/SlexAxton any update on the license?
That's the only blocker for this to land (adding support for message format
to Globalize).


Reply to this email directly or view it on GitHub
#321 (comment).

@rxaviers
Copy link
Member Author

@rxaviers rxaviers force-pushed the fix-265-message-format branch 2 times, most recently from cce9b7a to 56edc4f Compare December 17, 2014 10:22
@@ -15,6 +15,7 @@
"devDependencies": {
"make-plural": "eemeli/make-plural.js#2.1.2",
"es5-shim": "3.4.0",
"messageformat": "SlexAxton/messageformat.js#302a46a38d511de3b499f9ce0d18289e9fbd92b3",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be replaced with a tag once the liencese change is tagged, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct! Or at least to point to debeaf4 (new license in place)

@@ -1,6 +1,6 @@
{
"scripts": {
"preinstall": "npm install cldr-data-downloader",
"postinstall": "./node_modules/cldr-data-downloader/bin/download.js -i bower_components/cldr-data/index.json -o bower_components/cldr-data/"
"postinstall": "node ./node_modules/cldr-data-downloader/bin/download.js -i bower_components/cldr-data/index.json -o bower_components/cldr-data/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't really belong in this PR, does it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops it doesn't. I've extracted this change and landed this piece on master. Rebased.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it still showing the diff here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated origin/master. But, the it still show up to me also. Although:

It doesn't show up to me running git diff origin/master..origin/fix-265-message-format. So, a github delay/issue?


QUnit.test( "should return the loaded translation", function( assert ) {
assert.messageFormatter( "pt", "amen", "Amém" );
assert.messageFormatter( "zh", "amen", "阿门" );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add an assertion that uses the root locale.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is, merge this test with the one below, since they use the same test data/setup.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are in two different sections, because they test two different levels of implementation support.

@rxaviers
Copy link
Member Author

All done.

@rxaviers rxaviers closed this in 6968078 Dec 19, 2014
@rxaviers rxaviers deleted the fix-265-message-format branch January 12, 2015 18:50
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

Successfully merging this pull request may close these issues.

5 participants