Language Module Reference
The Language module is a core framework service designed to manage the application's linguistic state. It provides a reliable way to persist user preferences across sessions and retrieve localized content.
Core Methods
These methods are available within the Language module and are accessed via the $this->language property available throughout your application.
set_language(string $lang)
Sets the current language for the application. This preference is persisted in both the Session and a Cookie (valid for 30 days).
- $lang: A string representing the language code (e.g., 'en', 'fr', 'es').
get_language()
Returns the current active language code as a string. The method determines the language based on the following priority:
- Active Session (
$_SESSION['app_lang']) - Persistent Cookie (
app_lang) - The
APP_LANGconstant (if defined in config) - The module's default fallback (usually 'en')
reset_language()
Clears the language preference from both the Session and the Cookie, effectively reverting the application to its default language state.
Accessing the Module
The Language module is available throughout your application (Controllers, Models, or Views) via the $this->language property. No manual instantiation is required.
| Method | Description |
|---|---|
$this->language->set_language($lang) |
Sets the global language state. |
$this->language->get_language() |
Returns the current language code (e.g., "en"). |
$this->language->load($path) |
Loads a language file and returns the $phrases array. |
The load() Pathing
When using load(), the {lang} placeholder is mandatory for dynamic multi-lingual support. The method performs an internal str_replace() before attempting to require the file.
Note on Missing Files:
If the file specified in the path does not exist, load() will return an empty array. It will not trigger a PHP error or halt execution.
Summary of Constants and Settings
While the Language module works out-of-the-box, you can influence its behavior via your application's configuration.
- APP_LANG: Define this constant in your
config.phpto set a site-wide default language that overrides the module's internal default. - Cookie Lifetime: The default persistence for the language cookie is 2,592,000 seconds (30 days).
Always use $this->language->get_language() in your controllers to pass the $lang variable to your views. This ensures your <html lang=""> attribute always remains in sync with the framework's state.