---
title: "php-gettext"
description: "The preset for gettext in PHP. It reads .po catalogs and gettext, _ and __ calls. It applies only when you list it."
canonical_url: "https://x18n.dev/presets/php-gettext"
---
# php-gettext

> The preset for gettext in PHP. It reads .po catalogs and gettext, \_ and \_\_ calls. It applies only when you list it.

The `php-gettext` preset supports [gettext](https://www.php.net/manual/en/book.gettext.php) in PHP projects.

## Detection

The preset has no detection. It applies only when you list it in `presets`. The preset is exclusive: x18n applies no listed preset after it.

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

export default defineConfig({
  presets: ['php-gettext'],
})
```

```yaml [x18n.config.yml]
presets: [php-gettext]
```
::

## Locale files

If your config has no `resources`, the preset uses `locale/{locale}/LC_MESSAGES/*.po` and `locales/{locale}.po`. The `po` format reads and writes these catalogs. Each `msgid` is a key, and its `msgstr` is the value. The preset sets `keySeparator: false`, so a `.` or `:` in a `msgid` stays part of the key:

```po [locale/de/LC_MESSAGES/messages.po]
msgid "Hello"
msgstr "Hallo"
```

## Supported syntax

In PHP files, the preset reads `gettext`, `_` and `__`. The key is the source message:

```php [index.php]
<?php echo gettext("Hello"); echo _("Missing");
```

These usages are `Hello` and `Missing`.

## Keys for extracted strings

The preset sets `extract.keygen` to `source`, so `x18n extract` uses the text itself as the key. Set `extract.keygen` in the config to change it.

## Placeholders

printf: `%s`, `%d`, `%1$s`.

## Extraction templates

| Kinds | Template           |
| ----- | ------------------ |
| any   | `gettext('{key}')` |


## Sitemap

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