projectfluent / fluent

Fluent — planning, spec and documentation
https://projectfluent.org
Apache License 2.0
1.4k stars 45 forks source link

Compiling fluent #345

Open ecstrema opened 2 years ago

ecstrema commented 2 years ago

I couldn't find any info regarding compiling fluent to the target languages.

Is it possible, for example, to transform this:

key = Hello, {$userName}!

Into this (javascript implementation here);

key(props) => "Hello, " + props.userName + "!"

This greatly helps reducing bundle size. It is done by messageFormat and a few others and would be great to have.

spookylukey commented 2 years ago

For Javascript, see https://github.com/eemeli/fluent-compiler - although I have significant doubts about the implementation, as I'm not convinced it is sufficiently checking input in for the case of potentially malicious ftl files.

For Python, there is my fluent-compiler package, although at the moment it doesn't output Python code to save for later use, it simply uses compilation as an implementation strategy.

ecstrema commented 2 years ago

Good to know. I'll have a look at both of them.

eemeli commented 2 years ago

I wrote the JS compiler a while ago, but haven't touched it in years. IIRC, it might be targeting an earlier version of the Fluent spec even. I wrote it largely following on from the JS messgeformat design, i.e. as a build-time tool, so I didn't really consider malicious FTL files at all in its design.

My current main focus around this is on the Unicode MessageFormat 2 (MF2) spec, and eventually getting that to be the basis for Intl.MessageFormat in JavaScript. This is relevant for compiling messages, as it'll include a data model representation of messages which will have native formatter support in JS as well as other languages. In other words, we'll be able to compile Fluent and other syntaxes into the MF2 data model during the application build, rather than executable JS source, and feed that to a formatter provided by the runtime.