Skip to content

Internationalisation

vitepress-plugin-quiz ships with five built-in locales and a simple API to use or override them.

Built-in locales

CodeLanguage
enEnglish (default)
frFrench
deGerman
itItalian
esSpanish

Setting a locale

Pass the locale code as the second argument to enhanceAppWithQuiz:

ts
// .vitepress/theme/index.ts
import { enhanceAppWithQuiz } from "vitepress-plugin-quiz"

export default {
  enhanceApp({ app }) {
    enhanceAppWithQuiz(app, "fr") // French
  },
}

Using locale strings programmatically

You can import getLocale and locales directly if you need the strings outside of VitePress (e.g. in tests or SSR wrappers):

ts
import { getLocale, locales } from "vitepress-plugin-quiz"

const en = getLocale("en")
console.log(en.validate) // "Validate"

const all = locales
console.log(Object.keys(all)) // ["en", "fr", "de", "it", "es"]

Custom locale

Provide your own locale object. It must satisfy the QuizLocale type:

ts
import { enhanceAppWithQuiz } from "vitepress-plugin-quiz"
import type { QuizLocale } from "vitepress-plugin-quiz"

const ptLocale: QuizLocale = {
  answered: "respondida(s)",
  validate: "Validar",
  reset: "Reiniciar",
  correctAnswers: (n) => `resposta${n !== 1 ? "s corretas" : " correta"}`,
  multipleHint: "Várias respostas possíveis",
  explanation: "Explicação: ",
  inputHint: "Digite sua resposta",
  inputPlaceholder: "Sua resposta…",
  expectedAnswer: "Resposta esperada:",
}

export default {
  enhanceApp({ app }) {
    // Provide the custom locale directly
    app.provide("quiz-locale", ptLocale)
    enhanceAppWithQuiz(app) // still registers the components
  },
}

Tip: When calling enhanceAppWithQuiz(app) without a locale argument after manually providing quiz-locale, the manually provided value takes precedence because provide was called first.

QuizLocale type reference

ts
interface QuizLocale {
  /** Label shown in the progress bar: "3 / 5 answered" */
  answered: string
  /** Validate button label */
  validate: string
  /** Reset button label */
  reset: string
  /** Score label suffix — receives the correct count */
  correctAnswers: (n: number) => string
  /** Hint shown above multiple-choice answers */
  multipleHint: string
  /** Prefix shown before explanation text */
  explanation: string
  /** Hint shown above fill-in-the-blank input */
  inputHint: string
  /** Input field placeholder */
  inputPlaceholder: string
  /** Label shown before the expected answer on wrong input */
  expectedAnswer: string
}

Released under the GPL-3.0 License.