Translations
Every string Inkbar shows comes from messages. English is the default. French and German ship with the library, and any other language is a set of strings you pass in.
Why strings
Section titled “Why strings”messages is a plain object of strings and small functions. Inkbar asks nothing of your translation setup, so it works with whatever your app uses, or with nothing. The examples here call a generic t(key, params) where your own function goes.
A set is nested by feature area, such as toolbar and find. messages takes a partial set, and a key you leave out stays English.
Override a few keys
Section titled “Override a few keys”Pass only what you want to change.
<script setup lang="ts">import { InkRoot, InkViewport } from '@inkbar/vue'import { assets } from 'virtual:inkbar-assets'
defineProps<{ file: File }>()
const messages = { toolbar: { find: 'Search this file' } }</script>
<template> <InkRoot :src="file" :assets="assets" :messages="messages"> <InkViewport /> </InkRoot></template>import { InkRoot, InkViewport } from '@inkbar/react'import { assets } from 'virtual:inkbar-assets'
const messages = { toolbar: { find: 'Search this file' } }
export function Viewer({ file }: { file: File }) { return ( <InkRoot src={file} assets={assets} messages={messages}> <InkViewport /> </InkRoot> )}<script lang="ts"> import { InkRoot, InkViewport } from '@inkbar/svelte' import { assets } from 'virtual:inkbar-assets'
let { file }: { file: File } = $props()
const messages = { toolbar: { find: 'Search this file' } }</script>
<InkRoot src={file} {assets} {messages}> <InkViewport /></InkRoot>import { createViewer } from '@inkbar/core/dom'import { assets } from 'virtual:inkbar-assets'
const viewer = createViewer(el, { source: file, assets, messages: { toolbar: { find: 'Search this file' } },})Passing messages again replaces the last value, so overrides do not pile up.
French and German
Section titled “French and German”Each language is its own entry point, so your bundler drops the ones you don’t import.
<script setup lang="ts">import { fr } from '@inkbar/core/locales/fr'import { InkRoot, InkViewport } from '@inkbar/vue'import { assets } from 'virtual:inkbar-assets'
defineProps<{ file: File }>()</script>
<template> <InkRoot :src="file" :assets="assets" :messages="fr"> <InkViewport /> </InkRoot></template>import { fr } from '@inkbar/core/locales/fr'import { InkRoot, InkViewport } from '@inkbar/react'import { assets } from 'virtual:inkbar-assets'
export function Viewer({ file }: { file: File }) { return ( <InkRoot src={file} assets={assets} messages={fr}> <InkViewport /> </InkRoot> )}<script lang="ts"> import { fr } from '@inkbar/core/locales/fr' import { InkRoot, InkViewport } from '@inkbar/svelte' import { assets } from 'virtual:inkbar-assets'
let { file }: { file: File } = $props()</script>
<InkRoot src={file} {assets} messages={fr}> <InkViewport /></InkRoot>import { createViewer } from '@inkbar/core/dom'import { fr } from '@inkbar/core/locales/fr'import { assets } from 'virtual:inkbar-assets'
const viewer = createViewer(el, { source: file, assets, messages: fr })de comes from @inkbar/core/locales/de. To change a string in a shipped set, spread it: messages: { ...fr, find: { ...fr.find, field: 'Chercher' } }.
Both sets are drafts. If a string reads wrong to you, open an issue on inkbar/inkbar with the key and the wording you would use.
Messages with values
Section titled “Messages with values”A message that takes values is a function of one params object. Call your own t from it:
import type { InkMessagesInput } from '@inkbar/core'
declare function t(key: string, params?: object): string
const messages: InkMessagesInput = { lang: 'es', find: { field: t('find.field'), count: ({ current, total }) => t('find.count', { current, total }), },}Plurals are your t’s job. It knows your language’s rules, so find.count returns “1 de 1” or “2 de 7” in the form your language needs.
To do the same by hand, use the two helpers from @inkbar/core. template fills {name} placeholders. plural picks a form by the language’s plural rule for one parameter, and other is the fallback:
import { plural, template } from '@inkbar/core'
const messages = { lang: 'en', search: { resultCount: plural('en', 'count', { one: '{count} result', other: '{count} results', }), }, thumbnails: { page: template('en', 'Page {page}'), },}Both format numbers for the language you give them, so 1200 reads 1,200 in English and 1.200 in German. The shipped sets are built with them.
Switching live
Section titled “Switching live”Change messages or locale and the viewer relabels in place. It does not remount, reload the document or move the scroll position, and an open menu, popover, dialog or find bar stays open.
<script setup lang="ts">import { de } from '@inkbar/core/locales/de'import { fr } from '@inkbar/core/locales/fr'import { InkRoot, InkViewport } from '@inkbar/vue'import { ref } from 'vue'import { assets } from 'virtual:inkbar-assets'
defineProps<{ file: File }>()
const messages = ref(fr)</script>
<template> <button @click="messages = fr">Français</button> <button @click="messages = de">Deutsch</button> <InkRoot :src="file" :assets="assets" :messages="messages"> <InkViewport /> </InkRoot></template>import { de } from '@inkbar/core/locales/de'import { fr } from '@inkbar/core/locales/fr'import { InkRoot, InkViewport } from '@inkbar/react'import { useState } from 'react'import { assets } from 'virtual:inkbar-assets'
export function Viewer({ file }: { file: File }) { const [messages, setMessages] = useState(fr) return ( <> <button onClick={() => setMessages(fr)}>Français</button> <button onClick={() => setMessages(de)}>Deutsch</button> <InkRoot src={file} assets={assets} messages={messages}> <InkViewport /> </InkRoot> </> )}<script lang="ts"> import { de } from '@inkbar/core/locales/de' import { fr } from '@inkbar/core/locales/fr' import { InkRoot, InkViewport } from '@inkbar/svelte' import { assets } from 'virtual:inkbar-assets'
let { file }: { file: File } = $props()
let messages = $state(fr)</script>
<button onclick={() => (messages = fr)}>Français</button><button onclick={() => (messages = de)}>Deutsch</button><InkRoot src={file} {assets} {messages}> <InkViewport /></InkRoot>import { de } from '@inkbar/core/locales/de'
viewer.setMessages(de)viewer.setLocale('de-CH') // optionalviewer.messages is a readable of the words and locale in use, as { messages, locale }. subscribe calls you after each setMessages or setLocale. Pieces made without a viewer, such as createToolbar, follow it when you pass messages: viewer.messages.
Locale and lang
Section titled “Locale and lang”Each set carries its lang: fr.lang is 'fr'. Without a locale, the viewer root’s lang attribute and the dates on notes follow it, so messages={fr} alone reads as French to a screen reader. Pass locale, such as 'de-CH', when the page language or the date format should differ from the messages.
Labels you supply
Section titled “Labels you supply”The text you pass in is yours to translate. These each take a string or a function that returns one:
- a tool’s
label, and thelabelof a toolbar or panel - a sidebar’s
handleLabel - an outline’s
emptyText - a plugin command’s label and its
errorStatus - a card’s
title,labeland action labels
Inkbar calls a function label when it renders and again on every setMessages or setLocale, so the label follows the language.
const toolbar = createToolbar({ label: () => t('toolbar'), messages: viewer.messages })
definePlugin('review', (plugin) => { const notes = plugin.records<Note>('notes')
plugin.command('suggest', () => t('suggest'), run, { errorStatus: () => t('suggest.failed'), })
plugin.marks(notes, { rect: (n) => n.rect, label: (n) => t('note.label', { text: n.text }), card: (n) => ({ title: () => t('note.title'), actions: [{ label: () => t('note.dismiss'), run: () => notes.remove(n.id) }], }), })})A card’s actions is an array of { label, run } so each label can be a function. The object form, with the label as the key, works for fixed text. See Writing a plugin.
Key reference
Section titled “Key reference”Every message in the English set, by group. The table leaves out lang, which names the set’s language and isn’t a message. A function message shows its template, or its plural forms, with {name} for the values it receives. Your replacement for a function message takes the same params object.