yanthomasdev / lunaria

The missing localization management toolchain for open-source projects
https://lunaria.dev
MIT License
113 stars 6 forks source link

Lunaria config programmatically [Feature Request] #128

Closed emeraldsquirrel closed 6 months ago

emeraldsquirrel commented 7 months ago

Right now the Lunaria config is a .json file, it would be really nice if I could provide it as something programmatic, like .mjs.

The reason is that in an Astro Starlight project I have to define locales in 3 different places/ways, and I would rather programmatically do this to reduce errors.

Once for Astro Starlight in astro.config.mjs:

defineConfig({
  integrations: [ 
    starlight({ 
      defaultLocale: startlightDefaultLocale, 
      locales: starlightLocales,
    }),
  ],
})

Then for Astro i18n in astro.config.mjs:

defineConfig({
  i18n: {
    defaultLocale: astroDefaultLocale, 
    locales: astroLocales,
  },
})

And finally for Lunaria in lunaria.config.json:

{
  "defaultLocale": ...
  "locales": ...
}

Currently I use a locales.ts file that can be imported into my astro.config.mjs then use helper functions to transform my list into the proper format for either Astro or Starlight.

Preferably I would be able to specify the Lunaria config in something like a .mjs file so I could bring in my config at runtime instead.

Note I have a workaround: I can add lunaria.config.json to my .gitignore, and add a helper script that generates the lunaria configuration file from my locales.ts and a templated config file. This happens as an additional build step before I call Lunaria itself. I feel this is less than ideal, but will continue this way if that's the recommended path.

Thank you for your time! Thoughts?

yanthomasdev commented 6 months ago

Sorry for the wait @emeraldsquirrel, for the time being I don't expect to add this feature, as this would make it impossible for the configuration to be statically analyzable, which would break a few possible integrations with Lunaria.

Since your case uses Starlight, the @lunariajs/starlight package has a sync option that automatically updates your config based on your locales whenever you build, so maybe that would help you! This might improve even more if Starlight uses Astro's own i18n configuration in the future, so that we can infer from it inside Lunaria itself, without needing the @lunariajs/starlight package (we can't do this with Starlight's configuration because it's processed and not returned when we evaluate the Astro config)

emeraldsquirrel commented 5 months ago

Thank you for the response. I didn't know about @lunariajs/starlight so I will explore that for the integration. Thank you, that will save me a lot of work!

I still find it annoying that astro, starlight, and lunaria all keep their locale information in different formats and locations. I understand the tradeoff you've picked even if I don't personally agree with it (perhaps something like Zod).

Thank you for taking the time to consider my proposal, I'm really glad Lunaria exists.