Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview in the browser does not get updated even after beforeCreate is being called #1344

Closed
ASHFAQPATWARI opened this issue Sep 1, 2024 · 3 comments

Comments

@ASHFAQPATWARI
Copy link

  • Maizzle Version: 5.0.0-beta.23
  • Node.js Version: v20.9.0

I am trying to add i18next to the maizzle config.
I have added a translations.json file and added it to server watch array. My code to add i18next looks like this

import i18next from "i18next";
   
  /** @type {import('@maizzle/framework').Config} */
  export default {
    t: i18next.t,
    lang: "en",
    build: {
      content: ["src/templates/**/*.html"],
      static: {
        source: ["src/images/**/*.*"],
        destination: "images",
      },
    },
    server: {
      port: 8080,
      reportFileSize: true,
      watch: ["./src/images/translations.json"],
    },
    async beforeCreate({ config }) {
      try {
        // TODO: setting config should work from here as expected. Commenting code here as config is coming as undefined
        // config.language = process.env.TEMPLATELANGUAGE || "en";
        console.log(
          "------------------------------------- running beforecreate --------------------------------"
        );
        
      const translations = await import("./src/images/translations.json", {
        assert: { type: "json" },
      });
      const lang = process.env.TEMPLATELANGUAGE || "en";
      // Await is important here to not let build advance before i18next has had time to finish initialization.
      await i18next.init({
        lng: lang,
        fallbackLng: "en",
        debug: false,
        resources: translations.default,
      });
    } catch (error) {
      console.log("error in config.js beforeCreate", error);
    }
  },
  async beforeRender({ html, config, matter }) {
    console.log("process.env ", process.env.TEMPLATELANGUAGE);
    config.language = process.env.TEMPLATELANGUAGE || "en";
  },
};

ISSUE 1 - However on changing the translations file, preview in browser is not refreshed not even on refresh. I need to start the server again to see the changes.
ISSUE 2 - config in beforeCreate function is coming as undefined therefore we cant add anything to it

@ASHFAQPATWARI
Copy link
Author

@cossssmin Can you suggest a better approach for doing localization in Maizzle.

@cossssmin
Copy link
Member

beforeCreate runs only once, try dynamically importing the JSON in beforeRender. I'll look into issue 2, thanks for letting me know 👍

cossssmin added a commit that referenced this issue Sep 10, 2024
@cossssmin
Copy link
Member

Fixed the beforeCreate issue with the config, will be available in next beta. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants