globalizejs / globalize-compiler

Globalize.js runtime compiler for your formatters and parsers
Other
10 stars 14 forks source link

Unclear what this thing does #36

Closed catamphetamine closed 6 years ago

catamphetamine commented 6 years ago

From the docs it's not clear what exactly does this compiler do.

Suppose my app formats currencies for two languages. Raw CLDR data may be bundled and loaded at runtime, but as I assume the purpose of this compiler is to compile that CLDR data into a smaller subset (or isn't it) leaving just the fields required. What will be the pseudocode command for doing that? And where to should the output files be fed.

sunsande commented 6 years ago

Hi catamphetamine ,

I found it also not easy or intuitive, but somehow got it to work. Here my 2 Cents:

Assume we want to globalize web app (here single html file for simplicity). In this app we will use only the following Globalize functions:

Globalize.parseDate and Globalize.parseNumber

so we prepare a single file app.js like the following (just to feed the compiler. One can also use the production js files, which is not so convenient as they could be many and in different folders so I prefer to summarize the functions I would need):

Globalize.parseDate("");
Globalize.parseNumber("");

In between we installl the globalize-compiler via the npm with the required dependencies.

Assume we need only german cldr data. So we run only the following command:

globalize-compiler -l de -o cldr_de.js app.js

Here globalize-compiler will parse the app.js and extract the calls to the Globalize functions. Then it will generate the cldr_de.js where only the cldr data needed for the functions found in app.js will be stored - in some javascript code - have a look at it ...

Then the tricky part in the web app. One needs the following includes in the html file:

<script src="globalize-runtime.js"></script>
<script src="globalize-runtime/message.js"></script>
<script src="globalize-runtime/number.js"></script>
<script src="globalize-runtime/plural.js"></script>
<script src="globalize-runtime/date.js"></script>
<script src="globalize-runtime/currency.js"></script>
<script src="globalize-runtime/relative-time.js"></script>
<script src="globalize-runtime/unit.js"></script>

and then

<script src="js/jquery.js"></script>
<script src="cldr_de.js"></script>

Note that these are not the standard Globalize includes. Furthermore - if you take the Globalize versions of this files along with the globalize.js instead of globalize-runtime.js the example won't work.

I did not find any explanation why is that. So when I work in Visual Studio I cannot just take the Globalize package from NuGet and work with it, but rather I have to install globalize-compiler and then copy the globalize-runtime files to my project so I can use the compiled cldr data (cldr_de.js).

It seems strange and maybe I am doing it wrong, but this is the only way I got it to work and also I didn't get any feedback on the questions I posted ...

Please share if you get something more.

Best regards, Alex

catamphetamine commented 6 years ago

@sunsande Oh, thanks, now I get how this whole thing works.

@globalizejs My issue is resolved, but @sunsande seems to still have the question about globalize-runtime import.

rxaviers commented 6 years ago

When formatting or parsing, there's actually a two step process: (a) the formatter (or parser) creation and (b) its execution, where creation takes considerably more time (more expensive) than execution (a bit more can be found in Performance docs). Globalize-compiler allows you to perform the (a) phase in your build environment and then run the (b) phase directly in your production environment (by using the runtime modules + your compiled code (in your case, cldr_de.js).

I never used NuGet and therefore cannot provide any help there, sorry.

@sunsande please did you post your questions in SO too? If so, please do you have the link?

rxaviers commented 6 years ago

I'm closing this issue, let's follow up questions in SO.

sunsande commented 6 years ago

Here the link to Stack Overflow (SO)

The answers:

Yes it is NuGet issue. I've already notified the owners of the NuGet jquery-globalize package to include the globalize-runtime modules with the standard NuGet jquery-globalize package. Of course I could use the npm (this is what I'm doing now - and it was not easy and fast to find out!), but NuGet is standard in the VisualStudio world and one would not like to get the half of the packages from here and the other half from there - and it is not easy to remember that if you install the library from one source it will work and from the other it won't ... - this just needs a fix.

Basically, globalize-runtime is smaller than globalize and it should be used along with precompiled formatters and parsers. Differently from the regular globalize lib, globalize-runtime can't create new formatters and parsers. I am open to merge PRs clarifying that in the docs. Thanks for your help and question – Rafael Xavier

Thanks to @rxaviers