Localization

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.

The browser

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

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 does not store a UI language on its User object. Keep the language your application saves for the user in user metadata, 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:

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(). 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

import { useLocale } from 'next-intl';

const locale = useLocale();

i18next and react-i18next

import { useTranslation } from 'react-i18next';

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

Without React, read i18next.language.

vue-i18n

import { useI18n } from 'vue-i18n';

const { locale } = useI18n();

Pass locale.value when you create the Editor.

The server

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

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

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.

src/main.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());

In 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

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:

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.

On this page