laravel

The preset for Laravel. It reads PHP translation arrays and JSON strings, __ and trans calls, and :name placeholders.

The laravel preset supports the localization of Laravel.

Detection

The preset applies when composer.json requires laravel/framework. The preset is exclusive: when it applies, x18n tests no preset after it.

x18n.config.ts
import { laravel } from '@x18n/cli/presets'
import { defineConfig } from '@x18n/core'

export default defineConfig({
  presets: [laravel()],
})

Locale files

If your config has no resources, the preset uses these patterns:

  • lang/{locale}/{namespaces}.php
  • resources/lang/{locale}/{namespaces}.php
  • lang/{locale}.json
  • resources/lang/{locale}.json

The php format reads a file that returns an array literal, and writes edits back into the array. The path of a PHP file is the first part of its keys:

lang/en/auth.php
<?php

return [
    'failed' => 'These credentials do not match.',
];

This file has the key auth.failed. A file in a subdirectory, such as lang/en/admin/users.php, gives keys such as admin.users.title. In a JSON file, such as lang/en.json, each key is a whole sentence: Welcome back!.

Supported syntax

In PHP and Blade files, the preset reads __, trans, trans_choice, @lang, Lang::get and Lang::choice. Laravel writes the keys of a nested file with a slash. The preset turns each slash into a dot:

resources/views/a.blade.php
{{ __('auth.failed') }}
@lang('admin/users.title')
{{ __('Welcome back!') }}

These usages are auth.failed, admin.users.title and Welcome back!.

Placeholders

Colon: :name, as in 'title' => 'Users :count'.

Extraction templates

KindsTemplate
any__('{key}')