jlarmstrongiv / astro-i18n-aut

The i18n integration for Astro πŸ§‘β€πŸš€
https://www.npmjs.com/package/astro-i18n-aut
MIT License
125 stars 12 forks source link

Can I import translations from json files? #11

Closed tiwka19 closed 1 year ago

tiwka19 commented 1 year ago

is it possible to store translations in json files and access them from astro files?

jlarmstrongiv commented 1 year ago

Sure! It’s just normal Astro with the additional renders for each locale.

You can import from a single json translation file:

import { getLocale } from "astro-i18n-aut";
import { DEFAULT_LOCALE } from "../consts";
import translations from "../translations/all.json"

const locale = getLocale(Astro.url) ?? DEFAULT_LOCALE;
const translation = translations[locale as keyof typeof translations];

Or from multiple json translation files:

import { getLocale } from "astro-i18n-aut";
import { DEFAULT_LOCALE } from "../consts";

const locale = getLocale(Astro.url) ?? DEFAULT_LOCALE;
const { default: translation } = await import(`../translations/${locale}.json`);

You may be interested in adding a translation framework like https://www.i18next.com/ to handle loading from json files and dealing with things like plurals, interpolation, and formatting if you have a larger website.