Context
It all started two years ago. I was working on a new PWA for a big social network written from scratch that needed a i18n module...
For further actions, you may consider blocking this person and/or reporting abuse
I assume it would be more code on the wire if you server-rendered these functions?
Have your considered rendering them inside a Service Worker and returning the
.js
file?Interesting idea to do it on the server-side/service-worker/build-tool !
Yet I'm not sure there is much gain, the generated function weighs more than the string representation. And it will mean for every update of the library you'll have to re-generate all your functions in case of signature mismatch (and possible fixes on the generated function).
But yeah, it would also be possible with this method to remove the compiler from the code and gain some extra bytes :)
By the way, here is how to extract the function if needed :
With a little more work of the compiler, there are things that could make the function shorter:
Can you clarify to me what the
(a||(a=="0"?0:""))
is doing?Yeah some optimizations can definitively cleanup the generated function. I just wanted to avoid spending much time (and file size) just to prettify the output.
The
var p=a||{};
for example can be removed in case of raw string (that's not actually the case).About the
(a||(a=="0"?0:""))
, it's actually to avoid printing "undefined", "null" in a translation, but keep "0" working :I'm not well-versed in
i18n
, is that the expected behavior ofpet, select, other{{pet}}
? Empty string for entirely missing key?Can I suggest this way of inlining
selects
or will it hit performance?If you do manage to get rid of
new Function
, you could do things like this though (n
being a helper shared by all functions for all locales):Depend of the i18n libs, some are printing undefined, some others are keeping
"{variable}"
in translation.As for me, I think it's a better user experience to have an empty string than a variable name (else the website seems broken).
Should probably report an error in prod(in addition to whatever behavior you suggest)/fail in dev?
Imo the key name is less bad than an empty string, I have been on broken sites/apps where I could complete my flow in part thanks to the variable names in the template. But that's a nitpick.
Ok, I note the suggestion, I'll try to implement it this week end, something like the
onMissingKey
is working :Just saw your comment about SELECT optimization.
I already did some tests with it working using an object mapping to values, but it doesn’t work well with nested expressions.
With nested expressions, you can’t really pre-cache objects and it will execute all the possible branches code before doing the resolution leading to performance issue.
With nested expressions you can have functions for some branches and strings for some. :v
Don't know about performance, but it is usually very compact, and you just need one helper:
Not really so compact if you transpile it to ES5.
Here is an example of a complete solution (if I don't miss a thing ?).
The translation :
The global functions
The generated function demo:
I'll probably do a branch to see if it's a good candidate.
should probably be an object, passed by reference instead of individually through arguments?
It's more about the fact that the browser can store the parsed and even potentially optimized function in cache, not just the string form, when you go through normal pathways.
new Function()
is rather esoteric, and means it will definitely do a parse per instantiation, as well as cause some deoptimization around the instantiation.Furthermore, using the library as is requires
'unsafe-eval'
CSP directive on your entire page, which you otherwise might be able to avoid.