set_language()
public function set_language(string $lang): void
Description
Sets the current application language. Stores the language code in the session and sets a cookie for persistence across browser sessions (30-day lifetime).
Language codes are typically two-letter ISO codes (e.g., 'en', 'fr', 'es', 'de'). The language setting affects which translation files are loaded by the load() method.
Parameters
| Parameter | Type | Description |
|---|---|---|
| $lang | string | The language code to set as active (e.g., 'en', 'fr', 'es'). |
Return Value
| Type | Description |
|---|---|
| void | This method does not return a value. |
Example Usage
PHP
// Set language to French
$this->language->set_language('fr');
// Set language to Spanish
$this->language->set_language('es');
// Language switcher in a controller
public function switch_language(string $lang) {
$this->language->set_language($lang);
redirect('welcome');
}Notes
- The language is stored in
$_SESSION['app_lang']and a cookie namedapp_lang. - Cookie lifetime is 30 days (2,592,000 seconds).
- Use
get_language()to retrieve the current language. - Use
reset_language()to clear the language setting.