Description
Do you think that this is something worth having on the starter kit?
If you think so, let me know and I can open the pull request!
In an app we are building, we needed to localize a few pages. In order to do that we made use of i18next and "react-i18next.
Implementation
I decided for a direct approach, initializing i18next on the frontend, using it's own translation files.
Brief description of changes:
- Defined translation files on
app/frontend/lib/translations/[LANG].ts
- Initialize
i18next on app/frontend/lib/i18next.ts
- Include it on the
inertia.tsx entrypoint.
- Pass locale with
inertia_shared on InertiaController.
- Add
useLocale to react to locale changes.
- Add
around_action :switch_locale on ApplicationController to let Rails manage the locale.
Usage
import { useTranslation } from "react-i18next"
export default function Dashboard() {
const { t } = useTranslation()
return ( <p>{t("dashboard.title")}</p>)
}
Description
Do you think that this is something worth having on the starter kit?
If you think so, let me know and I can open the pull request!
In an app we are building, we needed to localize a few pages. In order to do that we made use of
i18nextand"react-i18next.Implementation
I decided for a direct approach, initializing
i18nexton the frontend, using it's own translation files.Brief description of changes:
app/frontend/lib/translations/[LANG].tsi18nextonapp/frontend/lib/i18next.tsinertia.tsxentrypoint.inertia_sharedonInertiaController.useLocaleto react to locale changes.around_action :switch_localeonApplicationControllerto let Rails manage the locale.Usage