Internationalization
LightNet comes with full support for internationalization (i18n). This allows you to create a multilingual site with ease. This guide will show you how to set up a new language, translate LightNet into a new language, and translate your own UI strings.
Add a new UI language
Follow this steps to add a new UI language to your LightNet site:
- Add translations file: Create a new file in the
src/translations
folder. The file must be named with the language code (e.g.de.yml
for German). The content of the file should look like this:The left side of the colon is the key and the right side is the translation value. You may add a description by starting the line withsrc/translations/de.yml # Your description of the translation.x.hello-world: Hallo Welt#
. - Translate LightNet strings: Some languages are built-in to LightNet. This languages are maintained by the community. So it might be there is missing translation. If your language is not built-in add LightNet translations to the new file. All required keys can be found here. Translate the keys to your new language. If you choose a built-in language your translations will override the internal translations. Please consider sharing your LightNet translations with the community.
- Translate your own UI strings: Add your own UI strings to the file. More details on custom UI strings can be found below.
- Add to config: Add the new language to the
languages
array in yourastro.config.mjs
file.astro.config.mjs export default defineConfig({integrations: [lightnet({languages: [{// Language code must match the translation// file name. For example "de.yml" has the code "de"code: "de",// Display name for the language selector.label: "Deutsch",// Use this as an user interface languageisUILanguage: true},],}),],}) - Set default language (Optional): Inside
astro.config.mjs
set the new language as the default language by settingisDefaultUILanguage: true
in the language config object. - Validate: Run the development server with
npm run dev
and check if the new language is available in the language selector. Select the new language and check if the translations are working as expected.
Use custom UI strings
LightNet allows you to add custom UI strings to your site. This is useful if you want to add custom text to your site that is not part of LightNet. Custom strings can be used for example to add custom text to the navigation or to add a text to the homepage. Follow this steps to add custom UI strings to your LightNet site:
- Use your translations file from the previous chapter to add the custom UI string as a key-value pair. For example
this is how you would add a custom string to the English translations file:
Choose a unique key (left side of the colon) for your custom string. By convention all your keys should be prefixed with
src/translations/en.yml x.hello-world: Hello Worldx.my-custom-string: My custom textx.
. LightNet’s internal keys are prefixed withln.
. This helps to avoid naming collisions and will improve the validation of your keys. The key will be used to referenced your custom string in your site. We recommend to use English keys for your custom strings. - Add the translation for your custom string to the other translations files. This is important if you want to support multiple languages.
src/translations/de.yml x.hello-world: Hallo Weltx.my-custom-string: Mein eigener Text - Use the custom string in your site. We provide a translation function with
Astro.locals.i18n.t
. You can use this function to get the translation of your custom string.The translation function will return the translation of the keysrc/pages/[locale]/index.astro ---export { getLocalePaths as getStaticPaths } from "lightnet/i18n"import { Page } from "lightnet/components"---<Page><h1>{Astro.locals.i18n.t("x.my-custom-string")}</h1></Page>my-custom-string
in the current language. If the key is not found in the translations file, the function will return the value of the default translations file. If the key is not found in the default translations file, the function will throw an Error. Some config options require you to pass the translation key instead of the translated string. For example thetitle
property in theastro.config.mjs
file. Check the config reference to see where a translation key is required.
How translations are resolved
- User translations are merged with built-in translations. User translations will override built-in translations.
- Lookup translation key in exactly matching translation file
- Lookup translation key in base language. E.g. for
en_US
this will useen
to resolve the key. - Lookup translation key in fallbackLanguages defined for this language. Starting from first item. Stopping on first match.
- Lookup translation key in default user-interface language.
- Lookup translation key in english translations
- Return key if nothing is found. If key starts with
ln.
orx.
throw an error.
Share your translations
explain how to share translations.