Magic Translate
  • Getting started
  • Clients
    • React
      • The <T> component
      • The useT hook
      • Next.js
    • React SSR
      • The translate function
  • FAQs
    • How fast is the translation
    • How is the usage calculated
    • What are the limitations
    • What happens when the monthly limit is reached?
    • What are the available languages?
Powered by GitBook
On this page
  • Getting Started
  • Changing Languages
  • Example project
  1. Clients

React

How to use Magic Translate in React applications

Getting Started

Install the react client for Magic Translate in your application.

# using yarn
yarn add @magic-translate/react

# using npm
npm i @magic-translate/react

Now it's time to wrap your application in a <MagicTranslateProvider>

import {
  MagicTranslateProvider,
  Language
} from '@magic-translate/react'

const MyApp = () => (
  <MagicTranslateProvider
    apiKey="<your-api-key>"
    language={Language.DE} // any supported language
  >
    // your application
  </MagicTranslateProvider>
)

Now you can use the translate component <T> anywhere in your application

import {
  T
} from '@magic-translate/react'

const SomeComponent = () => <T>Please translate me!</T>

And that's basically it! You can now translate your application effortlessly into over 70 languages 🤯

Changing Languages

To change the current language, all you have to do is change the language prop of the MagicTranslateProvider. All your content will automatically be translated into the desired language.

Alternatively, you can use the useSwitchLanguage() hook:

import { useLanguageSwitcher } from '@magic-translate/react'

const SomeComponent = () => {
    const { setLanguage, currentLanguage } = useLanguageSwitcher()
    
    return (
        <div>Current language is: {currentLanguage}</div>
        <button onClick={() => setLanguage("de")}>German</button>
    )
}

Please note that when using the useLanguageSwitcher() hook, the language prop of the MagicTranslateProvider is treated as the default (initial) language. Changing the language by setting the language prop will no longer have any effect.

Example project

PreviousClientsNextThe <T> component

Last updated 2 months ago

If you don't have an API key yet, please visit to get one.

You can find an example project here:

magictranslate.io
https://github.com/magic-translate/magic-translate-example-react