In-context editing
A translator who sees a text where it is used translates it better than one who sees a key name in a list. With in-context editing they open your app, click a text, and edit it in a small editor window beside the page.
Turn it on
Section titled “Turn it on”Add the in-context plugin next to the backend:
import i18next from 'i18next';import LocalizeBackend from '@localize-work/i18next-backend';import { LocalizeInContext } from '@localize-work/i18next-backend/in-context';
await i18next .use(LocalizeBackend) .use(LocalizeInContext) .init({ // ... backend: { projectSlug: 'your-project', cdnBaseUrl: 'https://cdn.localize.work' }, });Nothing changes for your visitors. The plugin stays idle until someone opens
the app with ?localize=edit in the address:
https://staging.your-app.example/?localize=editAllow your site
Section titled “Allow your site”An app running on your own computer (localhost) works straight away. For any
other address, an admin or developer of the project adds it once under
Project settings → In-context editing, for example
https://staging.your-app.example.
Each site is listed on its own: the scheme, the host and the port must match, and wildcards are not supported. Until a site is listed, the editor window still opens and saves, but the page does not show the saved text.
What a translator does
Section titled “What a translator does”- Opens the app with
?localize=edit. A small toolbar appears at the bottom. - Moves the pointer over a text. It is outlined and its key is shown.
- Clicks it. The key opens in a localize.work window, where they sign in as usual and edit, comment or review as in the editor.
- Saves. The page shows the new text straight away.
Clicking a text edits it, so a button or a link does not do its usual job
while editing is on. Pause in the toolbar hands the page back for a moment,
to reach another screen. Exit turns editing off; so does ?localize=off.
Editing stays on while that browser tab navigates and ends when it is closed.
Options
Section titled “Options”Set them under localizeInContext in i18next.init.
| Option | Default | Meaning |
|---|---|---|
enabled |
on with ?localize=edit |
false switches the feature off entirely. true turns it on without the parameter; Exit and ?localize=off still turn it off for the tab. |
projectSlug |
backend.projectSlug |
The project to open in the editor. |
editorUrl |
https://app.localize.work |
Where the editor runs. |
queryParameter |
localize |
The address parameter that turns editing on. |
Anyone who knows the parameter can turn the toolbar on, but editing still needs a localize.work account with access to the project. To keep it out of production altogether:
import i18next, { type InitOptions } from 'i18next';import type { LocalizeInContextOptions } from '@localize-work/i18next-backend/in-context';
// i18next's option type does not know the plugin's options, so add them here.const options: InitOptions & { localizeInContext: LocalizeInContextOptions } = { // ... // `enabled: false` switches it off in production builds. Elsewhere the // default applies, so editing starts only with ?localize=edit; `true` // would turn it on for every page load. localizeInContext: import.meta.env.PROD ? { enabled: false } : {},};
i18next.init(options);How it works
Section titled “How it works”While editing is on, every translated string ends with a few invisible characters that stand for its key. The plugin reads them back when a text is clicked. They take no space and are removed again when editing is turned off.
The editor opens in its own window, not inside your page. It signs people in with a cookie, and browsers do not send that cookie to a frame embedded in another site. Your page and the editor window exchange messages, and each checks that the other is who it should be: your page accepts saved text only from the editor’s address, and the editor talks only to the page that opened it, and only when the project lists that page’s site.
Limits
Section titled “Limits”-
Markers are part of the string while editing is on. Code that compares a translated string, measures it or sends it to a server should clean it first:
import { stripMarkers } from '@localize-work/i18next-backend/in-context';const plain = stripMarkers(t('status.active')); -
What can be clicked. Text in an element, and in its
placeholder,title,aria-labelandaltattributes. Text drawn on a canvas or set as the document title cannot. -
Nested translations. A text that includes another with
$t(…)opens the outer key. -
One project per page. If a page starts editing for a second i18next instance that names another project, editing stays off for that instance.
-
Server rendering. The server’s HTML has no markers, so React reports a hydration mismatch while editing is on. It does not affect normal visits.
-
Pop-ups. If the browser blocks the editor window, allow pop-ups for your app.
-
The page does not show what was saved. The site is not on the project’s list yet; see Allow your site.
-
Keys that are not in the project yet open with a note saying so. Turn on saveMissing in development so new keys arrive on their own.