Translations


Monti has a small, custom translation system. Strings are wrapped in the __() helper and translated at print-time using files loaded for the active language.

__("Welcome!");
__("Welcome :name!", ["name" => "John"]); // placeholders, replaced by :key

If no translation is found for a string, __() simply returns the original string unchanged.

Available and default language

Both are set in config/app.php:

/config/app.php
"languages" => [
    "default" => "en",
    "available" => ["en", "it"],
],
\Project::get()->getLanguages();       // ["en", "it"]
\Project::get()->getDefaultLanguage(); // "en"
\Project::get()->hasLanguage("fr");    // false

Adding a translation file

Translation files are loaded from the locales folder at the project root, and from a locales folder inside each active module. Each file must be named <anything>.<language_code>.php (or .json) and return (or contain, for JSON) a map from the original string to its translation:

locales/messages.it.php
<?php
return [
    "Welcome!" => "Benvenuto!",
    "Welcome :name!" => "Benvenuto :name!",
];

The same file, as JSON:

locales/messages.it.json
{
    "Welcome!": "Benvenuto!",
    "Welcome :name!": "Benvenuto :name!"
}

Getting and setting the active language

\Language::get();     // the current language code
\Language::set("it"); // switches language

If nothing was explicitly set, Monti determines the active language, in order: the active_language cookie, then the browser's Accept-Language header, then the configured default.

\Language::set() silently does nothing if the target language is not listed in app.languages.available.