This package provide a simple way to use @adonisjs/i18n
in your AdonisJS application using InertiaJS.
Install and configure the package using the following command :
node ace add @adaloop/adonis-inertia-i18n
Then add the following lines to your detect_user_locale_middleware.ts
file :
Note
This file is created by @adonisjs/i18n
package.
export default class DetectUserLocaleMiddleware {
// This method already exists but you must return a string instead of string | null, then return the default locale if the user doesn't have a locale
protected getRequestLocale(ctx: HttpContext) {
const userLanguages = ctx.request.languages()
return i18nManager.getSupportedLocaleFor(userLanguages) ?? i18nManager.defaultLocale
}
async handle(ctx: HttpContext, next: NextFn) {
// rest of the method...
/**
* Sharing translations to inertia view
*/
if ('inertia' in ctx) {
ctx.inertia.share({
locale: language,
translations: i18nManager.getTranslationsFor(language),
})
}
return next()
}
}
import { useI18n } from '@adaloop/adonis-inertia-i18n/hooks/react'
function MyComponent() {
const { t } = useI18n()
return (
<>
<h1>{t('my.i18n.key')}</h1>
<p>{t('my.i18n.key', 'Fallback')}</p>
</>
)
}
<script setup>
import { useI18n } from '@adaloop/adonis-inertia-i18n/hooks/vue'
const { t } = useI18n()
</script>
<template>
<h1>{{ t('my.i18n.key') }}</h1>
<p>{{ t('my.i18n.key', 'Fallback') }}</p>
</template>
Thank you for being interested in making this package better. We encourage everyone to help improve this project with new features, bug fixes, or performance improvements. Please take a little bit of your time to read our guide to make this process faster and easier.
To understand how to submit an issue, commit and create pull requests, check our Contribution Guidelines.
We expect you to follow our Code of Conduct. You can read it to understand what kind of behavior will and will not be tolerated.