react internationalization i18n

background

support multiple languages, need a language switcher in application.
language changes not associate with routes.

requirements

i18next react-i18next i18next-browser-languagedetector i18next-http-backend

code…


This content originally appeared on DEV Community and was authored by qian

background

  • support multiple languages, need a language switcher in application.
  • language changes not associate with routes.

requirements

i18next react-i18next i18next-browser-languagedetector i18next-http-backend

code

i18.ts

i18n
  // load translation using http -> see /public/locales
  // learn more: https://github.com/i18next/i18next-http-backend
  .use(Backend)
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    ns: [
      "translation",
      "header",
      "note",
      "settings",
      "zod",
      "custom",
      "introduce",
      "message",
    ],
    defaultNS: "translation",
    fallbackLng: "en",
    debug: false,
    detection: {
      caches: ["localStorage"],
    },
  });
export default i18n;

main.ts

import "./i18";

locale files

Put json file in the /public/locales/, if you add one json to each language, you need to add its filename to 'ns' in config, like above i18.ts.
[images]

using example

basic useTranslation
import { useTranslation } from "react-i18next";

const { t } = useTranslation();

<button className="btn border truncate" onClick={handleSignIn}>
  {t("sign-in")}
</button>;

translation.json

{
  "sign-in": "sign in"
}

plurals

t("item", { count: info.count });
{
  "item_one": "item",
  "item_other": "items"
}
multiple namespaces useTranslation

if pass an array to useTranslation, the first namespace as the default, we can use it without pass namespace. The other namespaces, when getting data from these namespace, need to pass the namespace.

const { t } = useTranslation(["note", "message"]);

// default namespace
t("empty-description");
// other namesapces
t("save title successfully", { ns: "message" });

note.json

{
  "empty-description": "No notes exists, go write your first note!"
}

message.json

{
  "save title successfully": "save title successfully"
}
Trans Component
<Trans i18nKey={"success.description"} seconds={seconds}>
  we will get you in after {{ seconds }}s, or click here dive in right now
</Trans>
{
  "success": {
    "description": "we will get you in after {{seconds}}s, or click button go home right now"
  }
}

get/set current language

get current language depends on the cache config in i18.ts
I use localStorage in this article

// get current language
const currentLng = localStorage.getItem("i18nextLng");

// set current language
const { i18n } = useTranslation();
i18n.changeLanguage(item);

reference


This content originally appeared on DEV Community and was authored by qian


Print Share Comment Cite Upload Translate Updates
APA

qian | Sciencx (2024-08-02T00:08:07+00:00) react internationalization i18n. Retrieved from https://www.scien.cx/2024/08/02/react-internationalization-i18n/

MLA
" » react internationalization i18n." qian | Sciencx - Friday August 2, 2024, https://www.scien.cx/2024/08/02/react-internationalization-i18n/
HARVARD
qian | Sciencx Friday August 2, 2024 » react internationalization i18n., viewed ,<https://www.scien.cx/2024/08/02/react-internationalization-i18n/>
VANCOUVER
qian | Sciencx - » react internationalization i18n. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/02/react-internationalization-i18n/
CHICAGO
" » react internationalization i18n." qian | Sciencx - Accessed . https://www.scien.cx/2024/08/02/react-internationalization-i18n/
IEEE
" » react internationalization i18n." qian | Sciencx [Online]. Available: https://www.scien.cx/2024/08/02/react-internationalization-i18n/. [Accessed: ]
rf:citation
» react internationalization i18n | qian | Sciencx | https://www.scien.cx/2024/08/02/react-internationalization-i18n/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.