adamduncan / eleventy-plugin-i18n

Eleventy plugin to assist with internationalization and dictionary translations
MIT License
103 stars 10 forks source link

Reloading translations after a change #25

Open sguter90 opened 1 year ago

sguter90 commented 1 year ago

It would be very nice to detect changes in the translation files.

Via translations configuration you can only set an object containing translations. Every time a translation changes, the eleventy server needs to be restarted when using watch.

I worked around this issue by defining a Proxy:

    eleventyConfig.addPlugin(i18n, {
        translations: new Proxy({}, {
            get(target, prop, receiver) {
                let data = require('./src/_data/i18n');
                return data[prop];
            }
        }),
    });

Eleventy detects the change by itself and reloads the page which then fetches translation for EVERY i18n filter call. This works for small setups as a very dirty workaround.

So what I was thinking of was is adding a eleventy.before hook to the plugin which then resets the registered translations (or re-initalizes the filter) if one of the translations files changed (defined via a new translationFiles property).

Do you think that this makes sense?

adamduncan commented 1 year ago

Hey @sguter90, thanks for raising and proposing a workaround.

Is this a similar issue to what PR https://github.com/adamduncan/eleventy-plugin-i18n/pull/18 was looking to solve? (A little additional context there from the time)

Is it something that folks can handle via 11ty's built-in addWatchTarget API? IMO it might make more sense to try to solve that on the framework side as opposed to here on the plugin side.

Hope that helps 👍

sguter90 commented 1 year ago

Hi @adamduncan , yes, #18 would provide a way to solve my problem 👍 I'm fetching translations from a repote API and write them to a data file with a parallel running process. Everytime the translation file changes a rebuild is triggered by eleventy.

As far as I know addWatchTarget also just triggers the build logic but not restarts the watch process where plugins are added. (which re-imports js dependencies)

Because you linked https://github.com/11ty/eleventy/issues/1312 I've checked if eleventy version v2.0.2-alpha.1 solves this issue. But this doesn't seem to change anything regarding require statements. (or I'm missing something)