Loading Language Files
Trongate allows you to maintain a clean, professional application by separating your logic from your static text. The framework does not impose a single global file; instead, it encourages a structured directory system based on the principle of one subdirectory per language.
The Directory Protocol
To support multiple languages, you must create a dedicated subdirectory for every language your application supports. These directories are named using standard language codes (e.g., en, fr, es).
A typical structure within a module looks like this:
modules/contact/language/en/phrases.phpmodules/contact/language/fr/phrases.phpmodules/contact/language/es/phrases.php
The load() Method
The load() method is used to fetch the translation array from the correct subdirectory. To ensure your code remains dynamic, use the {lang} placeholder:
$path = 'modules/contact/language/{lang}/phrases.php';
$phrases = $this->language->load($path);Multi-Language Example: Thank You Page
In this scenario, we define the text for English, French, and Spanish. Notice that the language file must define an array named $phrases.
1. Language Files
English (en/phrases.php):
<?php
$phrases = [
'headline' => 'Thank You!',
'success_message' => 'Your contact form was successfully submitted.'
];French (fr/phrases.php):
<?php
$phrases = [
'headline' => 'Merci !',
'success_message' => 'Votre formulaire de contact a été soumis avec succès.'
];Spanish (es/phrases.php):
<?php
$phrases = [
'headline' => '¡Gracias!',
'success_message' => 'Su formulario de contacto ha sido enviado con éxito.'
];Controller and View (Separation of Concerns)
The controller prepares clean, simple variables for the view. This ensures the view is "dumb" and strictly concerned with output.
The Controller
<?php
class Contact extends Trongate {
function thank_you() {
$data['lang'] = $this->language->get_language();
// Load phrases from the language-coded subdirectory
$path = 'modules/contact/language/{lang}/phrases.php';
$phrases = $this->language->load($path);
// Map phrases to simple variables
$data['headline'] = $phrases['headline'];
$data['success_message'] = $phrases['success_message'];
$data['view_module'] = 'contact';
$this->view('thank_you_view', $data);
}
}The View
<!DOCTYPE html>
<html lang="<?= $lang ?>">
<head>
<meta charset="UTF-8">
<title><?= out($headline) ?></title>
</head>
<body>
<h1><?= out($headline) ?></h1>
<p><?= out($success_message) ?></p>
</body>
</html>By mapping language strings to specific variables in the controller, you create a clear contract between your logic and your display. This makes your code more readable, safer, and easier to maintain.
We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.