---
title: "ngx-translate"
description: "The preset for ngx-translate. It reads the translate pipe and directive in Angular templates, and TranslateService calls."
canonical_url: "https://x18n.dev/presets/ngx-translate"
---
# ngx-translate

> The preset for ngx-translate. It reads the translate pipe and directive in Angular templates, and TranslateService calls.

The `ngx-translate` preset supports [ngx-translate](https://github.com/ngx-translate/core) in Angular projects.

## Detection

The preset applies when the project depends on `@ngx-translate/core`.

```ts [x18n.config.ts]
import { defineConfig } from '@x18n/core'

export default defineConfig({
  presets: ['ngx-translate'],
})
```

## Locale files

If your config has no `resources`, the preset uses `src/assets/i18n/{locale}.json` and `src/assets/i18n/{locale}/{namespaces}.json`.

## Supported syntax

In HTML templates, the preset reads the `translate` pipe and the `translate` directive:

<!-- eslint-skip -->

```html [src/app/app.component.html]
<h1>{{ 'home.title' | translate }}</h1>
<p [translate]="'home.sub'"></p>
<p translate="home.intro"></p>
<span translate>home.footer</span>
```

In TS and JS files, these names count as translate calls: `translate`, `translate.instant`, `translate.get`, `translate.stream`, `translateService.instant`, `translateService.get`, `translateService.stream` and `_`. A call on `this` counts too:

```ts [src/app/app.component.ts]
this.translate.instant('home.save')
```

The object passed after the key holds the interpolation params, which the [`param-mismatch`](https://x18n.dev/checks/param-mismatch) check compares with the placeholders of the source message. A params function (`translate('home.greet', () => ({ name }))`) is left alone, and so is the `_` marker.

## Placeholders

Double braces: `{{name}}`.

## Hard strings in templates

The preset adds the `html` scanner, so extraction finds hard-coded text and text attributes (`title`, `placeholder`, `aria-label`, ...) in `.html` templates. It skips:

- interpolations (`{{ user.name }}`) and ICU messages (`{count, plural, ...}`),
- bindings, events, references and structural directives: `[alt]`, `(click)`, `#ref`, `*ngIf`, `let-item`,
- the parameters of control-flow blocks such as `@if (...)`, `@for (...)`, `@switch`, `@case` and `@defer`, and `@let` declarations; the content of the blocks is scanned,
- elements whose content is translated already: `translate` and `[translate]`, Angular's `i18n` (and the attributes named in `i18n-*`), and `translate="no"`,
- `<script>` and `<style>`.

`<!-- x18n-ignore -->`, `<!-- x18n-ignore-start -->` ... `<!-- x18n-ignore-end -->` and `<!-- x18n-ignore-file -->` comments skip text as in other files.

## Extraction templates

| Kinds                      | Template                                |
| -------------------------- | --------------------------------------- |
| `html-text`                | `{{ '{key}' \| translate }}`            |
| `html-attribute`           | `[{attr}]="'{key}' \| translate"`       |
| `js-string`, `js-template` | `this.translate.instant('{key}'{args})` |


## Sitemap

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