---
title: "LSP protocol"
description: "The x18n language server: standard features, settings and the custom x18n/* protocol."
canonical_url: "https://x18n.dev/references/lsp-protocol"
---
# LSP protocol

> The x18n language server: standard features, settings and the custom x18n/\* protocol.

<!-- Generated by docs/scripts/generate.ts from the source code. Do not edit. -->

`@x18n/lsp` is the language server that the VS Code extension uses. Any editor with an LSP client can run it:

```sh
pnpx x18n-lsp --stdio
```

The server also accepts `--node-ipc` and `--socket=<port>`. With `--socket`, the client listens on the port and the server connects to it. The server loads the projects of each workspace folder, the same way the CLI does.

## Editor setup

Install the server once so that `x18n-lsp` is on your `PATH`:

```sh
npm i -g @x18n/lsp
```

The server runs next to your language's own server. Point it at the file types that use or define messages. Each editor below starts it over stdio and can pass [initialization options](#initialization-options).

### Neovim

Neovim 0.11 and later:

```lua
vim.lsp.config('x18n', {
  cmd = { 'x18n-lsp', '--stdio' },
  filetypes = { 'javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'vue', 'svelte', 'json', 'yaml' },
  root_markers = { 'x18n.config.ts', 'x18n.config.json', 'package.json', '.git' },
  init_options = { showFlags = true },
})
vim.lsp.enable('x18n')
```

### Helix

In `languages.toml`, add the server, then add it to each language next to the server it already uses:

```toml
[language-server.x18n]
command = "x18n-lsp"
args = [ "--stdio" ]
config = { showFlags = true }

[[language]]
name = "typescript"
language-servers = [
  "typescript-language-server",
  "x18n"
]
```

### Sublime Text

With the [LSP](https://packagecontrol.io/packages/LSP) package, in `LSP.sublime-settings`:

```json
{
  "clients": {
    "x18n": {
      "enabled": true,
      "command": ["x18n-lsp", "--stdio"],
      "selector": "source.js | source.ts | source.jsx | source.tsx | text.html.vue | source.json | source.yaml",
      "initializationOptions": { "showFlags": true }
    }
  }
}
```

### Emacs

With `lsp-mode`, register x18n as an add-on server so that it runs next to the main server:

```elisp
(with-eval-after-load 'lsp-mode
  (lsp-register-client
   (make-lsp-client
    :new-connection (lsp-stdio-connection '("x18n-lsp" "--stdio"))
    :activation-fn (lsp-activate-on "javascript" "typescript" "typescriptreact" "vue" "json" "yaml")
    :add-on? t
    :server-id 'x18n)))
```

## Standard features

The server treats keys as symbols. It supports go to definition, go to implementation, references, rename (with prepare), hover, inlay hints, completion of keys, code actions (quick fixes and extraction), document symbols, workspace symbols, code lenses and diagnostics. Diagnostics are the same as the ones from `x18n check`.

## Initialization options

The client passes these fields in `initializationOptions`. Each one is optional.

```ts
interface Settings {
  annotationMaxLength: number
  displayLocale: string | undefined
  codeLens: boolean
  /** Flag emoji next to locales in hovers. */
  showFlags: boolean
  /** Client commands a hover may link to with `command:` URLs. Empty means no links. */
  hoverCommands: ReadonlySet<string>
}
```

A hover shows the value per locale and the number of usages. It links to `x18n.openKey`, `x18n.copyTranslatePrompt` and `x18n.translateWithAgent` only when the client lists them in `hoverCommands`, because a client that cannot run a `command:` URL opens it as a link and fails. The default is no links.

## Custom methods

Shapes follow the [JSON contract](https://x18n.dev/references/json-contracts) version. Positions and ranges use LSP conventions: zero-based lines and UTF-16 characters.

| Method                  | Kind                            | Params                   | Result                 | Description                                                                                                                                                                                  |
| ----------------------- | ------------------------------- | ------------------------ | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `x18n/projects`         | request                         |                          | `ProjectInfo[]`        | The projects of the workspace.                                                                                                                                                               |
| `x18n/keyAt`            | request                         | `PositionParams`         | `KeyAtResult \| null`  | The key or key prefix at a position, in code or in a locale file.                                                                                                                            |
| `x18n/tree`             | request                         | `TreeParams`             | `TreeNode[]`           | One level of the key tree, with translation counts.                                                                                                                                          |
| `x18n/progress`         | request                         |                          | `ProgressResult`       | Translation progress per project and locale.                                                                                                                                                 |
| `x18n/fileKeys`         | request                         | `DocumentParams`         | `FileKeysResult`       | The keys and hard strings of one file, with the usage count of each key.                                                                                                                     |
| `x18n/usageReport`      | request                         |                          | `UsageReportResult`    | Unused keys, and used keys that are not defined, sorted by project and key.                                                                                                                  |
| `x18n/unusedDeletion`   | request                         | `UnusedDeletionParams`   | `UnusedDeletionResult` | Dry run of `x18n.deleteUnused`: per project, the unused keys it deletes with a location per message, the unused keys it keeps because computed keys in a scope may reach them, and warnings. |
| `x18n/search`           | request                         | `SearchParams`           | `SearchMatch[]`        | Keys found by their value in any locale, best first: fuzzy unless `exact`, ignoring case. Each match has the matched value, its locale entry and the usages. At most 100.                    |
| `x18n/decorations`      | request                         | `DocumentParams`         | `DecorationsResult`    | The ranges and values for in-place annotations in a source file.                                                                                                                             |
| `x18n/setDisplayLocale` | notification (client to server) | `SetDisplayLocaleParams` |                        | Changes the display locale. The server refreshes the inlay hints.                                                                                                                            |
| `x18n/openUi`           | request                         | `OpenUiParams`           | `OpenUiResult`         | Starts the UI server for a project on loopback and returns its URL and a one-time auth token.                                                                                                |
| `x18n/translatePrompt`  | request                         | `TranslatePromptParams`  | `string`               | The translate prompt for a locale, as markdown.                                                                                                                                              |
| `x18n/explain`          | request                         |                          | `string`               | What `x18n doctor` prints for every workspace folder: load errors, presets, locale files and config. The server also logs it when it finds no locale files.                                  |
| `x18n/changed`          | notification (server to client) | `ChangedParams`          |                        | A project changed. `keys` lists the keys that changed.                                                                                                                                       |

## Commands

The server runs these through `workspace/executeCommand`. The argument is one object. `project` defaults to the first project.

| Command                | Argument                            | Description                                                                                                                |
| ---------------------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `x18n.set`             | `{ project?, key, locale?, value }` | Sets one value. `locale` defaults to the source locale.                                                                    |
| `x18n.delete`          | `{ project?, key, locale? }`        | Deletes a key in one locale, or in all locales.                                                                            |
| `x18n.deleteUnused`    | `{ project? }`                      | Deletes the keys that `x18n/unusedDeletion` lists, in every locale, through `workspace/applyEdit`. Default: every project. |
| `x18n.renamePrefix`    | `{ project?, key, to }`             | Renames every key under a prefix, in locale files and code.                                                                |
| `x18n.extract`         | `{ uri }`                           | Extracts every hard string of a file.                                                                                      |
| `x18n.migrateI18nAlly` | `{ uri }`                           | Returns the files that the migration from i18n-ally writes, as `MigrationResult`. Nothing is written.                      |

## Types

```ts
export interface Position {
  readonly line: number
  readonly character: number
}

export interface Range {
  readonly start: Position
  readonly end: Position
}

export interface Location {
  readonly uri: string
  readonly range: Range
}

export interface DocumentParams {
  readonly uri: string
}

export interface PositionParams extends DocumentParams {
  readonly position: Position
}

export interface ProjectInfo {
  readonly name: string
  readonly root: string
  readonly sourceLocale: string
  readonly locales: readonly { readonly code: string, readonly displayName: string | undefined, readonly flag: string | undefined }[]
  readonly displayLocale: string
  readonly keys: number
}

export interface KeyAtResult {
  readonly project: string
  readonly key: string
  readonly type: 'key' | 'subtree'
  readonly range: Range
}

export interface KeyInfoLocale {
  readonly code: string
  readonly flag: string | undefined
  readonly value: string | undefined
  readonly state?: string | undefined
  readonly changed: string | undefined
  readonly outdated: boolean
  readonly source: boolean
  readonly present: boolean
}

export interface KeyInfoResult {
  readonly project: string
  readonly key: string
  readonly type: 'key' | 'subtree'
  readonly label?: string | undefined
  readonly range: Range
  readonly locales: readonly KeyInfoLocale[]
  readonly threads: readonly { readonly locale: string, readonly state: string }[]
  readonly usages: number
}

export interface TreeParams {
  readonly project?: string | undefined
  /** Parent key; omitted for the roots. */
  readonly parent?: string | undefined
}

export interface TreeNode {
  readonly key: string
  readonly label: string
  readonly leaf: boolean
  readonly translated: number
  readonly total: number
  /** Display-locale value for leaves. */
  readonly value: string | undefined
}

export interface LocaleProgress {
  readonly locale: string
  readonly translated: number
  readonly total: number
  readonly missing: readonly string[]
  readonly empty: readonly string[]
}

export interface ProgressResult {
  readonly projects: readonly { readonly name: string, readonly locales: readonly LocaleProgress[] }[]
}

export interface FileKey {
  readonly key: string
  readonly range: Range
  readonly value: string | undefined
  readonly missing: boolean
  readonly dynamic: boolean
  /** Usages of the key in the project; prefix usages that may reach it count too. Omitted for dynamic entries. */
  readonly usages?: number | undefined
}

export interface FileKeysResult {
  readonly keys: readonly FileKey[]
  readonly hardStrings: readonly { readonly text: string, readonly range: Range }[]
}

export interface UsageReportResult {
  readonly unused: readonly { readonly project: string, readonly key: string, readonly location: Location | undefined }[]
  readonly missing: readonly { readonly project: string, readonly key: string, readonly locations: readonly Location[] }[]
}

export interface SearchParams {
  /** Text of a value in any locale, ignoring case. */
  readonly query: string
  /** Only values containing the query; by default a fuzzy match also counts. */
  readonly exact?: boolean | undefined
}

export interface SearchMatch {
  readonly project: string
  readonly key: string
  /** The best-matching locale, source first on ties. */
  readonly locale: string
  readonly value: string
  /** The entry holding the matched value. */
  readonly definition: Location | undefined
  readonly usages: readonly Location[]
}

export interface Decoration {
  readonly range: Range
  /** The whole call holding the key, when the scanner knows it. */
  readonly expression?: Range | undefined
  readonly key: string
  /** The display value: the display-locale message with static call params filled. */
  readonly value: string | undefined
  readonly state: 'ok' | 'missing' | 'dynamic'
}

export interface DecorationsResult {
  readonly locale: string
  readonly decorations: readonly Decoration[]
}

export interface SetDisplayLocaleParams {
  readonly locale: string | undefined
}

export interface OpenUiParams {
  readonly project?: string | undefined
}

export interface OpenUiResult {
  readonly url: string
  readonly token: string
}

export interface TranslatePromptParams {
  readonly project?: string | undefined
  readonly locale: string
  readonly keys?: readonly string[] | undefined
}

export interface ReviewsParams {
  readonly project?: string | undefined
  readonly key?: string | undefined
  readonly locale?: string | undefined
  /** Only unresolved comments, and only threads that have some. */
  readonly open?: boolean | undefined
}

export interface ReviewCommentInfo {
  readonly id: string
  readonly type: 'comment' | 'approve' | 'request-change' | 'suggestion'
  readonly author?: { readonly name?: string | undefined, readonly email?: string | undefined } | undefined
  /** ISO 8601. */
  readonly time: string
  readonly text?: string | undefined
  readonly suggestion?: string | undefined
  /** The value an approval approved. */
  readonly value?: string | undefined
  readonly resolved?: boolean | undefined
  /** An approval of a value the message no longer has. */
  readonly stale: boolean
}

export interface ReviewThreadInfo {
  readonly project: string
  readonly key: string
  readonly locale: string
  readonly state: 'changes-requested' | 'approved' | 'stale-approval' | 'suggested' | 'commented' | undefined
  readonly comments: readonly ReviewCommentInfo[]
  /** The key in the locale's resource; undefined while the locale lacks the message. */
  readonly location: Location | undefined
}

export interface ReviewsResult {
  readonly threads: readonly ReviewThreadInfo[]
}

export interface UnusedDeletionParams {
  /** Default: every project. */
  readonly project?: string | undefined
}

/** Dry run of the bulk delete of unused keys; `x18n.deleteUnused` applies it. */
export interface UnusedDeletionResult {
  readonly projects: readonly {
    readonly project: string
    /** Keys to delete, sorted, with one location per message. */
    readonly keys: readonly { readonly key: string, readonly locations: readonly Location[] }[]
    /** Unused keys left alone because fully dynamic usages in a scope may reach them, sorted. */
    readonly kept: readonly { readonly key: string, readonly usages: readonly Location[] }[]
    readonly warnings: readonly string[]
  }[]
}

export interface ChangedParams {
  readonly project: string
  readonly revision: number
  readonly keys: readonly string[]
}

export interface MigrationResult {
  readonly files: readonly MigrationFile[]
  /** What was dropped or needs attention, for the report. */
  readonly notes: readonly string[]
  /** i18n-ally settings that can be removed after the migration. */
  readonly removable: readonly string[]
}

export interface MigrationFile {
  /** Path relative to the project root. */
  readonly path: string
  readonly content: string
}
```


## Sitemap

See the full [sitemap](https://x18n.dev/sitemap.md) for all pages.
