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
.jsfile?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
selectsor will it hit performance?If you do manage to get rid of
new Function, you could do things like this though (nbeing a helper shared by all functions for all locales):