Skip to main content

i18n

Many startup companies hope to promote their platforms to different countries, so it is essential for platforms to be multilingual. OneDayBuild already supports language switching, allowing you to adapt your platform to the use of different users through simple configuration.

OneDayBuild uses Next-intl as the underlying program.

Configuration

In the config.js file, find the i18n section and set the default language and the provided language types. Our example provides two languages, English and Chinese.

 i18n: {
defaultLocale: "en",
locales: ["en", "cn"],
},

Then, in the libs/i18n/i18n.js file, define the supported languages in the locales section. The platform will automatically switch to the corresponding language when it detects the user's language, and it will be displayed in the URL like this: https://oneday.build/cn/.

The libs/i18n/messages/ folder contains language packs supported by the platform. You can place the translated language packs here, and make sure the names match the configuration items. The structure is as follows:

{
"Index": {
"title": "Hello world!",
"subtitle": "How is it going?"
},
}

Use

After using i18n, the language will be included in all access paths following the domain name. Therefore, if you need to do direct path jumps in the system, you need to know the current language at any time. You can get the current language value using the following method:

import { useLocale } from "next-intl";

export function i18n () {
const locale = useLocale();
<a href={`/${locale}/test`}>Hello</a>
}

In specific pages, you can use the following method to placeholder in the appropriate position.

import { useTranslations } from "next-intl";

export default function Home() {
const t = useTranslations("Index");
<h1 className="text-4xl mt-6 font-bold">{t("One")}</h1>
}

Generating Languages

During the platform construction period, you can use English as the main language and improve the en.json file. Before going online, you can supplement other language packs.

For other language packs, you can use tools like ChatGPT to help you generate them, which can greatly save your time and cost.