# Localization recipes

> Read the reader's language from your auth provider, i18n library, or server, use locale and translations in React and Vue, and feed labels from your own translation files.



Each recipe produces a language tag, or a list of them, to pass to `locale`. For what `locale` and `translations` do, see
[Localize the Editor UI](/editor/localization).

## The browser [#the-browser]

`navigator.languages` lists the browser's languages in order of preference. Pass it directly:

```ts
new SuperDoc({ selector: '#editor', document: '/sample.docx', locale: navigator.languages });
```

SuperDoc uses the first tag in the list that it has a language pack for.

## Clerk [#clerk]

Clerk does not store a UI language on its `User` object. Keep the language your application saves for the user in
[user metadata](https://clerk.com/docs/guides/users/extending), for example `publicMetadata.language`, and fall back to
the browser.

Read the value on the server, where it is current, and pass it to the page:

```tsx
import { currentUser } from '@clerk/nextjs/server';

const user = await currentUser();
const saved = user?.publicMetadata.language;
const locale = typeof saved === 'string' ? saved : undefined;
```

`language` is a key your application chooses. Set it from your backend with
[`updateUserMetadata()`](https://clerk.com/docs/reference/backend/user/update-user-metadata). In the browser,
`useUser()` keeps the metadata from when the session loaded, so call `await user.reload()` after you change the value.

If you also translate Clerk's own components with `@clerk/localizations`, pass `enUS` explicitly when the reader
switches back to English.

## next-intl [#next-intl]

```tsx
import { useLocale } from 'next-intl';

const locale = useLocale();
```

## i18next and react-i18next [#i18next-and-react-i18next]

```tsx
import { useTranslation } from 'react-i18next';

const { i18n } = useTranslation();
const locale = i18n.language;
```

Without React, read `i18next.language`.

## vue-i18n [#vue-i18n]

```ts
import { useI18n } from 'vue-i18n';

const { locale } = useI18n();
```

Pass `locale.value` when you create the Editor.

## The server [#the-server]

On the server, read the request's `Accept-Language` header and send the tags to the page:

```ts
const locale = (request.headers.get('accept-language') ?? '')
  .split(',')
  .map((entry) => entry.split(';')[0].trim())
  .filter(Boolean);
```

This keeps the header's order and ignores its `q` weights. Browsers list languages from most to least preferred, so the
order is usually enough. SuperDoc skips entries that are not language tags, such as `*`.

## React and Vue [#react-and-vue]

The React and Vue wrappers accept `locale` and `translations` as props. They compare both by content, so an inline
object with the same entries neither restarts the Editor nor warns; a change to its entries applies on the next mount.

The React example switches languages by exporting the document and remounting with a new `key`; see
[Change the language](/editor/localization#change-the-language).

**Vanilla — `src/main.ts`**

```ts
import { SuperDoc, type SuperDocTranslations } from 'superdoc';
import 'superdoc/style.css';

const translations = {
  en: { 'toolbar.search': 'Find in contract' },
  'pt-BR': { 'toolbar.search': 'Buscar no contrato' },
} satisfies SuperDocTranslations;

const superdoc = new SuperDoc({
  selector: '#editor',
  document: '/sample.docx',
  locale: navigator.languages,
  translations,
});

window.addEventListener('beforeunload', () => superdoc.destroy());

```

**React — `src/App.tsx`**

```tsx
import { useRef, useState } from 'react';
import { SuperDocEditor, type SuperDocRef, type SuperDocTranslations } from '@superdoc/react';
import '@superdoc/react/style.css';

const translations = {
  en: { 'toolbar.search': 'Find in contract' },
  'pt-BR': { 'toolbar.search': 'Buscar no contrato' },
} satisfies SuperDocTranslations;

type Session = { locale: string; document: string | Blob; mount: number };

export default function App() {
  const editor = useRef<SuperDocRef>(null);
  const [ready, setReady] = useState(false);
  const [session, setSession] = useState<Session>({ locale: 'en', document: '/sample.docx', mount: 0 });

  async function switchLanguage(locale: string) {
    setReady(false);
    try {
      const current = await editor.current?.getInstance()?.export({ exportType: ['docx'], triggerDownload: false });
      if (!(current instanceof Blob)) throw new Error('SuperDoc did not return a DOCX file.');
      setSession((previous) => ({ locale, document: current, mount: previous.mount + 1 }));
    } catch (error) {
      console.error('Could not switch the language.', error);
      setReady(true);
    }
  }

  return (
    <>
      <select
        aria-label='Language'
        value={session.locale}
        disabled={!ready}
        onChange={(event) => void switchLanguage(event.target.value)}
      >
        <option value='en'>English</option>
        <option value='pt-BR'>Português (Brasil)</option>
      </select>
      <SuperDocEditor
        key={session.mount}
        ref={editor}
        document={session.document}
        locale={session.locale}
        translations={translations}
        onReady={() => setReady(true)}
      />
    </>
  );
}

```


In Vue:

```vue
<script setup lang="ts">
import { SuperDocEditor } from '@superdoc/vue';
import '@superdoc/vue/style.css';
import type { SuperDocTranslations } from 'superdoc';

defineProps<{ locale: string }>();

const translations = {
  en: { 'toolbar.search': 'Find in contract' },
  'pt-BR': { 'toolbar.search': 'Buscar no contrato' },
} satisfies SuperDocTranslations;
</script>

<template>
  <SuperDocEditor document="/sample.docx" :locale="locale" :translations="translations" />
</template>
```

## Your own translation files [#your-own-translation-files]

If your application already translates its UI, feed SuperDoc's labels from the same files. Build each language's labels
with `satisfies`, so TypeScript checks every key:

```ts
import type { SuperDocTranslations } from 'superdoc';

type Language = 'en' | 'pt-BR';
type Translate = (language: Language, key: string) => string;

export function superdocTranslations(translate: Translate): SuperDocTranslations {
  const labels = (language: Language) =>
    ({
      'comments.inputPlaceholder': translate(language, 'editor.addComment'),
      'toolbar.search': translate(language, 'editor.find'),
    }) satisfies SuperDocTranslations[Language];
  return { en: labels('en'), 'pt-BR': labels('pt-BR') };
}

```

With i18next, pass `(language, key) => i18next.getFixedT(language)(key)`.

TypeScript does not check the keys of a JSON import against `SuperDocTranslations`, so a misspelled key in a JSON file
is silently ignored. If your translations come from a translation tool as JSON, compare their keys with
`superdoc/locales/en.json`; see [Use a translation tool](/editor/localization/interface-strings#use-a-translation-tool).
