forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.64k stars 339 forks source link

[feat] extract locale phrases AOT #446

Closed Zetanova closed 1 year ago

Zetanova commented 1 year ago

The option to extract localized phrases from the template files at AOT is missing.

The current implantation updates the locale files be usage of email.send.

it is possible to run a job with Email({ send: false, preview: false }), but only the phrases that got used in the rendering by the template engine will be included.

In near all i18n features the phrases will get extracted ahead-of-time by statically analyzing the source code and looking for string pattern like t('...') and extracting them out to a locale file

Example:

var type = 'unknown'
case type 
  when 'typeA'
    h1= t('message of typeA')
  when 'typeB'
    h1= t('message of typeB')
  when 'typeC'
    h1= t('message of typeC')
  default 
    h1= t('unknown message type')

Result:

Only 'unknown message type' will get added to the locale files.

Expected:

All phrases should be included in the locale file.

titanism commented 1 year ago

This is out of scope for this project and is related to #445. See our comment there with reference.

If you want to extract phrases, then have a build script or test that will compile and test all emails (providing fake/virtual locals to pass to the templates). Doing this will cause the i18n locale files to be updated/created/synced. Afterwards, you can run mandarin.translate() as in our example (see #445 comment).

Zetanova commented 1 year ago

I already did it before writing the feature request.

The problem is simply the test count can get very high, child-nodejs workers would be needed to execute only a view complex templates in reasonable time.

The best workaround to the problem that I come up with is to simply specify dummy variables with all possible phrases in the template itself.

One other option that I found is to extract the phrases by regex matching, it is how it mostly done with all other i18n libs and set this phrases as static phrases into the i18n config instance. But I didn't investigate further

titanism commented 1 year ago

Specifying dummy variables is the way to go - it will not only test your template, but also let you get the i18n locales synced. I do not think regex matching is the best approach.