Skip to content

Version v1.6.5 Older versions: docs on GitHub

Localization

On this page

The package ships with two interface languages: Ukrainian (uk, the default) and English (en).

Panel language

The language applies to the panel routes only; the language of the rest of the application does not change. It is resolved in this order (the first non-empty value wins):

  1. the closure from Larsadmin::resolveLocaleUsing();
  2. the user's choice stored in the session (larsadmin.locale); it is accepted only if the language is listed in config('larsadmin.locales');
  3. config('larsadmin.locale') (LARSADMIN_LOCALE, uk by default);
  4. the application language (app()->getLocale()).

If larsadmin.locale is null, the panel uses the application language.

Configuration:

'locale' => env('LARSADMIN_LOCALE', 'uk'),

'locales' => [
    'uk' => 'Українська',
    'en' => 'English',
],

Language switcher

The switcher in the header lists the languages from locales (code => label) and stores the choice in the session (POST {path}/locale, 30 requests per minute). Guests can use the endpoint too, so the language can be chosen before signing in.

Language from the user profile

use Maxcxam\Larsadmin\Larsadmin;

// in boot() of the application service provider
Larsadmin::resolveLocaleUsing(fn (Request $request) => $request->user()?->locale);

The returned value is used as the panel language (it is not checked against locales, so return supported languages only). Returning null or an empty string passes the decision on to the next option.

Interface translations

The package strings live in the larsadmin:: namespace: the files ui.php, messages.php, actions.php and metrics.php (metric range labels, "Other", "None", "Yes"/"No") for uk and en.

To change or add strings, publish them:

php artisan vendor:publish --tag=larsadmin-lang

The files are copied to lang/vendor/larsadmin/{uk,en}. Translate or change the keys you need; you can delete the ones you don't. Application keys take precedence over the package keys. For a new language, add a lang/vendor/larsadmin/{code} directory with the same files and an entry in config('larsadmin.locales').

The frontend receives ui.php from the server for the current language. If a key is missing, the bundled Ukrainian copy is used, and if it is missing there too, the key itself is shown.

Validation messages

php artisan larsadmin:install copies lang/uk/validation.php (Ukrainian messages with the same structure as the framework's English file) only if the application has no such file. An existing file is not overwritten; the command reports that it leaves the file untouched. From then on the file is yours to edit freely.

Laravel itself provides the English messages (lang/en/validation.php, if you published it).

Entity and field labels

The labels you write in code need no separate translation files if the panel uses one language:

public static ?string $label = 'Категорії';       // plural
public static ?string $singularLabel = 'Категорія';

Text::make('Назва', 'name')
  • $label and $singularLabel go through __(), so you can translate them with JSON strings (lang/en.json: "Категорії": "Categories") or with keys.
  • Without $label, the plural is derived from the class name (Post gives Posts).
  • The field name (the first argument of make()) is shown as is. For a non-ASCII name, always pass the attribute as the second argument.
  • Filter and action labels (the default name() derived from the class name, Filter::make($label), options) go through __(). An overridden name() is shown as is.

Date formats

Date and DateTime fields on index and detail are formatted with the formats.date and formats.datetime keys of larsadmin::ui (for uk: d.m.Y and d.m.Y H:i:s; for en: Y-m-d and Y-m-d H:i:s). displayFormat('d/m/Y') sets the format of a single field and takes precedence.

Next: Themes.

Type to search all guides. Use ↑ ↓ to move and Enter to open.