Skip to content

Adding localization support in the frontend #123

@nerlichman

Description

@nerlichman

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>)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions