Elmar

Nuxt I18n Types

Oct 24, 2024

To ensure full type-safety and enhance the development experience with Nuxt modules, I developed a module that automatically generates types for @nuxtjs/i18n.

      const  = ("i18n-types");
const  = (import.meta.);

export default ({
  : {
    : "i18n-types",
  },

  async (, ) {
    const  = ..?. ?? [];
    const  = ..?. ?? "lang";

    .("builder:watch", (, ) => {
      if (.(`app/${}`)) {
        ({
          : () => . === "types/i18n-types.d.ts",
        });
      }
    });

    ({
      : "types/i18n-types.d.ts",
      () {
        const  = .();
        const  = .(() => {
          const  = typeof  === "string" ?  : .;
          const  = .(`../app/${}/${}.json`);
          const  = (, "utf-8");
    
          return .();
        });
    
        const  = (.(.));
    
        const  = `
          import { DefineLocaleMessage } from "vue-i18n";
          declare module 'vue-i18n' {
            export type MessageId = 
              | ${.(() => `"${}"`).("\n    | ")};
    
            interface DefineLocaleMessage {
              ${.(() => `"${}": string`).("\n")}
            }
          }`;
    
        const  = .();
        .(`I18n types generated in ${.( - )}ms`);
    
        return "template";
      },
    });
  },
});

function (: string[][]) {
  return .((, ) => .(() => .()));
}