Documentation

Language

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.

Auto-discovery of Language

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.

Usage Example

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";
}

Methods

  • __construct()
    Prevents direct instantiation of Language.
    • This method returns void: No return value.
  • init()
    Initialize the language settings.
    • This method returns void: No return value.
  • getLabel($key)
    Get the label for the given key.
    • $key: The key for the label.
    • This method returns string: The translated label.
  • findLanguage()
    Find the language based on configuration and request.
    • This method returns string: The language found.
  • setSessionLanguage($lang)
    Set the session language.
    • $lang: The language to set in session.
    • This method returns void: No return value.
  • getLabelsFromLanguage($lang)
    Get translated labels from language file.
    • $lang: The language for which to get labels.
    • This method returns array: The translated labels.