Language
Internationalization (i18n) lets one application serve users in several languages. The code asks for a key such as user.welcome, and the framework returns the text for the current locale, such as Welcome for en-US or 歡迎 for zh-TW. Translations live in files, one directory per locale, so translators can change text without touching code.
INFO
The standalone package has its own detailed guide: Windwalker Language. This page focuses on using it inside the Windwalker framework.
The language system has two layers:
Windwalker\Language\Language: the standalone translator. It has no framework dependency.Windwalker\Core\Language\LangService: the Core service. It implementsLanguageInterfaceand wraps aLanguageobject.
Enabling the Language Service
LanguagePackage registers LangService as a shared service. Obtain it from the container, or inject it into a constructor:
use Windwalker\Core\Language\LangService;
// Resolve from the container
$lang = $container->get(LangService::class);
// Or inject it into a constructor
class ReportService
{
public function __construct(
protected LangService $lang,
) {
}
}Setup
Configuration
The project config is etc/packages/language.config.php. The host application uses these keys:
// etc/packages/language.config.php
return [
'debug' => (bool) (env('LANG_DEBUG') ?? false),
'locale' => 'zh-TW',
'fallback' => 'en-US',
'paths' => [
'@languages',
],
];debug: marks untranslated strings and stores them as orphans. Debug output needs bothapp.debugandlanguage.debugenabled.locale: the locale used when the application starts.fallback: the locale used when a key is missing in the current locale.paths: directories scanned for translation files.@languagesresolves toresources/languages.
Translation Files
Place each locale in its own directory, and use the same file name in every locale:
resources/languages/
├── en-US/
│ └── main.ini
└── zh-TW/
└── main.ini; resources/languages/en-US/main.ini
user.welcome = "Welcome, :name"
user.logout = "Log out"Placeholders use the :name form. Language::replace() replaces each :key with the matching value from the arguments.
The file extension must always be written explicitly, for example main.ini or main.php. This is a requirement left over from legacy behavior, so do not rely on the format argument alone to find the file.
Complete Example
The example below loads the translation files in a middleware, translates inside a service, and returns the text in the current locale:
<?php
// app/Module/Front/FrontMiddleware.php
namespace App\Module\Front;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Windwalker\Core\Language\TranslatorTrait;
use Windwalker\Core\Middleware\AbstractLifecycleMiddleware;
class FrontMiddleware extends AbstractLifecycleMiddleware
{
use TranslatorTrait;
protected function preprocess(ServerRequestInterface $request): void
{
// Load every ini file for the current locale
$this->lang->loadAll('ini');
}
protected function postProcess(ResponseInterface $response): void
{
}
}Pass placeholder values as named arguments, such as name: $name. The same form works in a Blade template. An array form is also available as an optional alternative.
<?php
// app/Service/WelcomeService.php
namespace App\Service;
use Windwalker\Core\Language\LangService;
class WelcomeService
{
public function __construct(
protected LangService $lang,
) {
}
public function greeting(string $name): string
{
return $this->lang->trans(
'user.welcome',
name: $name,
);
}
}The array form works the same way and is useful when the keys are built at runtime:
$params = ['name' => $name];
echo $this->lang->trans('user.welcome', ...$params);
// OR
echo $this->lang->trans('user.welcome', $params);echo $welcome->greeting('Alice');
// ZH-TW: 歡迎, Alice
// EN-US: Welcome, AliceLoading Translation Files
Each middleware loads the files its area needs. A front-end request should not load admin strings, and the reverse holds too.
Loading in Middleware
Load files in preprocess() of the middleware for each area. The host application's FrontMiddleware loads its vendor packages first, then its own files:
<?php
namespace App\Module\Front;
use Lyrasoft\Luna\LunaPackage;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Unicorn\UnicornPackage;
use Windwalker\Core\Language\TranslatorTrait;
use Windwalker\Core\Middleware\AbstractLifecycleMiddleware;
class FrontMiddleware extends AbstractLifecycleMiddleware
{
use TranslatorTrait;
protected function preprocess(ServerRequestInterface $request): void
{
// Vendor files, loaded for the current locale
$this->lang->loadAllFromVendor(UnicornPackage::class, 'ini');
$this->lang->loadAllFromVendor(LunaPackage::class, 'ini');
// Project files in resources/languages
$this->lang->loadAll('ini');
// Or load one file for the current locale
$this->lang->loadFile('main.ini', 'ini');
}
protected function postProcess(ResponseInterface $response): void
{
}
}The admin middleware follows the same pattern. It loads the same vendor packages and the project files that the admin area uses:
<?php
namespace App\Module\Admin;
use Lyrasoft\Luna\LunaPackage;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Unicorn\UnicornPackage;
use Windwalker\Core\Language\TranslatorTrait;
use Windwalker\Core\Middleware\AbstractLifecycleMiddleware;
class AdminMiddleware extends AbstractLifecycleMiddleware
{
use TranslatorTrait;
protected function preprocess(ServerRequestInterface $request): void
{
$this->lang->loadAllFromVendor(UnicornPackage::class, 'ini');
$this->lang->loadAllFromVendor(LunaPackage::class, 'ini');
$this->lang->loadAll('ini');
}
protected function postProcess(ResponseInterface $response): void
{
}
}Loading One File
loadFile() loads a single file for the current locale. The file name must include its extension, and the format argument must match it:
// Load one file for the current locale
$lang->loadFile('main.ini', 'ini');
// Load one file for another locale
$lang->loadFile('main.ini', 'ini', 'en-US');Loading All Files
loadAll() loads every file of the given format from the locale directories in language.paths. The loadAllFromPath() method does the same for one directory, and loadAllFromVendor() reads resources/languages inside a vendor package:
// Every ini file in the configured paths
$lang->loadAll('ini');
// Every ini file in one directory
$lang->loadAllFromPath('@languages', 'ini');
// Every ini file shipped with a package class
$lang->loadAllFromVendor(UnicornPackage::class, 'ini');loadAllFromVendor() accepts a package class or a vendor name.
Switching the Locale Dynamically
The multi-language mechanism described above is static. The locale is read from the configuration, and the files are loaded for it. Windwalker does not decide the locale for each request by itself. Dynamic switching, routing by locale, automatic detection of the user's language, and remembering the choice in the session are features your project must implement. The rest of this section builds a small example that does these things with one middleware.
The example works like this:
- The URL carries the locale as a query parameter, such as
?locale=en-US. - A
LocaleMiddlewaresets the locale onLangService. - The chosen locale is saved in the session, so later requests without a
localeparameter keep the same language.
A Minimal LocaleMiddleware
The middleware reads locale from the query string. If it is missing or not supported, it uses the session value. It then saves the locale and sets it on LangService:
<?php
namespace App\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Windwalker\Core\Language\LangService;
use Windwalker\Session\Session;
class LocaleMiddleware implements MiddlewareInterface
{
public const SESSION_KEY = 'user.locale';
public const SUPPORTED = ['zh-TW', 'en-US'];
public const DEFAULT = 'zh-TW';
public function __construct(
protected LangService $lang,
protected Session $session,
) {
}
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$locale = $request->getQueryParams()['locale'] ?? null;
if (!in_array($locale, self::SUPPORTED, true)) {
$locale = $this->session->def(
self::SESSION_KEY,
self::DEFAULT
);
}
$this->session->set(self::SESSION_KEY, $locale);
$this->lang->setLocale($locale);
return $handler->handle($request);
}
}WARNING
Always check the query value against a whitelist before you store it. Without the check, any value in the URL is saved to the session and passed to the language loader.
Register the middleware before the middleware that loads the language files, so the files are loaded for the new locale.
Linking to Another Locale
Switch links only need the locale parameter:
<a href="?locale=en-US">English</a>
<a href="?locale=zh-TW">繁體中文</a>Translating in Your Own Service
Inject LangService into any service class, and translate inside its methods. The service does not need to know which locale is active; the shared service already holds it:
use Windwalker\Core\Language\LangService;
class InvoiceService
{
public function __construct(
protected LangService $lang,
) {
}
public function statusLabel(string $status): string
{
return $this->lang->trans("invoice.status.$status");
}
public function itemCount(int $count): string
{
// Pick the plural form for the count
return $this->lang->choice('invoice.items', $count);
}
}LangService also implements Windwalker\Utilities\Contract\LanguageInterface. Type-hint that interface when the code only needs translation, and does not need the Core features.
Translating in Blade Templates
Blade views receive $lang as a global variable. The @lang and @translate directives both output $lang(...), and @choice outputs $lang->choice(...):
<h1>@lang('user.welcome', name: $user->name)</h1>
<a href="/logout">@translate('user.logout')</a>
<span>@choice('cart.items', $count)</span>
<span>@choice('cart.items', $count, extra: '...')</span>In Windwalker layouts, it also has a $lang variable which is LangService object. You can use it to translate text in the layout:
<?php
// Translate in PHP block
echo $lang->trans('user.welcome', name: $user->name);
?>
<h1>{{ $lang->trans('user.welcome', name: $user->name) }}</h1>
<a href="/logout">{{ $lang->trans('user.logout') }}</a>
<span>{{ $lang->choice('cart.items', $count) }}</span>
<span>{{ $lang->choice('cart.items', $count, extra: '...') }}</span>WARNING
Never pass user-provided text as the translation key.
Using the TranslatorTrait
TranslatorTrait injects LangService into the $lang property with #[Inject]. The class must be created by the container, such as a view, a middleware, or a service registered in DI. The trait adds these methods:
use Windwalker\Core\Language\TranslatorTrait;
class DashboardView
{
use TranslatorTrait;
public function prepareMetadata(): void
{
// Translate a key
$title = $this->trans('unicorn.title.dashboard');
// Translate with a plural form
$items = $this->choice('cart.items', 3);
// Check whether a key exists in the current locale
$exists = $this->hasLang('user.welcome');
// Access the underlying LangService object
$lang = $this->getLang();
}
}useLangNamespace() replaces $this->lang with a namespaced copy created by LangService::extract(). The copy returns the same translations, and it keeps a link to the parent service:
// Translate with a namespace, then restore the default
$this->useLangNamespace('invoice');
echo $this->trans('status.paid');LangService and LanguageInterface
LangService implements LanguageInterface. The interface defines get(), trans(), choice(), replace(), and has(). Code that depends only on the interface works with either implementation.
LangService adds these capabilities, compared with the standalone Language object:
- It is built from the container config, and reads
language.locale,language.fallback, andlanguage.paths. - It resolves path aliases such as
@languagesand@vendor/.... loadFile(),loadFileFromPath(), andloadFileFromVendor()load one file from a path, a vendor package, or the locale directory.loadAll(),loadAllFromPath(), andloadAllFromVendor()load every file of one format.cloneForLocale()returns a copy with another locale.extract()returns a namespaced copy with a parent link.__invoke()allows the service to be called like a function.- Unknown methods, such as
setLocale()andgetLocale(), are forwarded to the innerLanguageobject.
Maintaining Translation Files
Translation files drift apart over time. New keys are added in one locale and missing in another. Two tools help you keep them in sync: the lang:merge command fills in missing keys, and debug mode records keys that have no translation.
Merging Language Files
lang:merge copies missing keys from one locale into another. Values that already exist in the target locale are kept. Run it from the host app root:
# Fill zh-TW with missing keys from en-US
php windwalker lang:merge main.ini zh-TW en-USThe locale arguments accept three forms. All three mean the same thing:
# {to} {from}
php windwalker lang:merge main.ini zh-TW en-US
# {from} '>' {to}
php windwalker lang:merge main.ini en-US '>' zh-TW
# {to} '<' {from}
php windwalker lang:merge main.ini zh-TW '<' en-USWithout -r, the command writes the result to a temporary directory and prints its path, so you can review it first. Add -r to replace the translation file in place:
php windwalker lang:merge main.ini zh-TW en-US -rCommon options:
-r,--replace: Overwrite the target file instead of writing to a temp file.-s,--sort: Sort the keys alphabetically.-c,--clear: Remove target keys that do not exist in the source locale.-d,--dir=DIR: Set the base language directory.-p,--pkg=PKG: Set the package name.
A common workflow is to merge, sort, and replace in one run:
php windwalker lang:merge main.ini zh-TW en-US -rsTIP
Run lang:merge after adding keys to your source locale. Then translate the new keys in the target file, instead of searching for them by hand.
Logging Untranslated Keys
When debug output is enabled, the Language object records each key it cannot find. Debug output needs both app.debug and language.debug. Set LANG_DEBUG=true in your .env file to enable language.debug.
In debug mode, the translated text is wrapped in marks. **text** means the string was found. ??text?? means it was not found in any locale, so the key is shown instead:
echo $lang->trans('app.user.welcome');
// ??app.user.welcome??The key is recorded in @logs/language-orphans.ini at the end of each request. The file accumulates keys across requests. Each key becomes a readable default value, so you can copy the entries into your translation file:
; @logs/language-orphans.ini
[user]
welcome = "User Welcome"
logout = "User Logout"INFO
Orphan logging only runs in debug mode. Turn it off in production, where the log file is not needed and should not grow.
Other Locales
Cloning for Another Locale
cloneForLocale() returns a copy with another locale. The original keeps its locale, so you can translate other languages without changing the request:
// The original stays in its current locale
$zh = $lang->cloneForLocale('zh-TW');
$zh->loadFile('main.ini', 'ini');
echo $zh->trans('user.logout'); // 登出
echo $lang->trans('user.logout'); // Log outBackground Email in the User's Locale
A background job often runs outside the request that created it. It should use the locale stored with the user, not the locale of the worker process:
<?php
use Windwalker\Core\Language\LangService;
class SendWelcomeMail
{
public function __construct(
protected LangService $lang,
) {
}
public function handle(object $user): void
{
// Use the user's preferred locale, stored in your user table
$userLang = $this->lang->cloneForLocale($user->locale);
$userLang->loadFile('main.ini', 'ini');
$subject = $userLang->trans(
'user.welcome',
name: $user->name,
);
$body = $userLang->trans('user.logout');
// Replace with your project's mailer
// $this->mailer->send($user->email, $subject, $body);
}
}TIP
Store each user's preferred locale in the database, and pass it to the job. Do not rely on the locale of the request that queued the job.