The Language class is used for the automated management of application labels and translations. The labels are centralized in JSON files located in the "lang" folder. Using these language-specific JSON files (e.g., en.inc.json, us.inc.json), labels can be utilized throughout the application.
If the $config["language_on"] = TRUE
setting is specified in the env.php configuration file, the system (during the initial Environment init phase) will automatically detect the language (by searching for it in the query, session, or configurations) and automatically load the associated language JSON file.
Additionally, the following configurations work as follows:
$config["language_force_default"]
forces the use of the default language set in $config["language_default"].$config["enabled_languages"]
is an array of enabled languages (e.g., array("en","us","it","fr",....).$config["show_default_language_in_URL"]
automatically inserts the language code into the URLs of the pages.use Boostack\Models\Language; // Get a label $label = Language::getLabel('mainDescription'); echo $label; // Output depends on the current language settings
Language::init() is typically invoked by the Environment controller (automatically called by Environment.php).
You can structure the language JSON file (e.g., en.inc.json) as you like and refer to specific labels using dot notation. For example, if the JSON contains this:
{ "services": { "title": "Custom Hosted & Managed Services", "description": "improves process management and business workflow performance.", "items": [ { "title": "Web Dev", "description": "Multiplatform and multimarket websites and web portals", "image": "services/webdev.png" }, { "title": "Mobile Dev", "description": "Mobile apps for Android and iOS systems with hybrid and native technologies", "image": "services/mobdev.png" }, { "title": "Custom Dev", "description": "Custom software solutions aimed at automating business processes.", "image": "services/cusdev.png" } ] } }
you can print the content like this:
use Boostack\Models\Language; // Get and print a nested label echo Language::getLabel('services.title'); // Output: Custom Hosted & Managed Services $services = (Object)Language::getLabel("services.items"); foreach ($services as $service) { echo $service["title"]."/n"; echo $service["description"]."/n"; }
__construct()
void
: No return value.init()
void
: No return value.getLabel($key)
string
: The translated label.findLanguage()
string
: The language found.setSessionLanguage($lang)
void
: No return value.getLabelsFromLanguage($lang)
array
: The translated labels.