ForgeLanguage

Optional capability module that extends forge kernel to have language capabilities, providing an easy-to-use localization system.

Overview

ForgeLanguage provides a complete localization system for Forge Kernel applications. It offers a convenient way to retrieve language-specific strings, manage available languages, and present a language switcher component to your users.

Key Features

  • Global helper functions for translations
  • Pre-built interactive Language Switcher UI Component
  • Easy file-based language definition

Installation

Install ForgeLanguage using ForgePackageManager.

# Install module
php forge.php package:install-module --module=ForgeLanguage

Available Helpers

The module registers global helpers (located in modules/ForgeLanguage/src/Support/helpers.php) that you can use anywhere in your application, especially in views.

languageTerm()

Fetches a translated string for a given key.

<?= languageTerm('welcome', 'Welcome to Forge Kernel', ['name' => 'John']) ?>

current_language()

Returns the current active language code (e.g., 'en', 'es').

<?php $current = current_language(); ?>

available_languages()

Returns an array of available languages defined in your app.

<?php $languages = available_languages(); ?>

Language Switcher Component

The module includes a ready-to-use Language Switcher component to let users easily swap languages. Use it directly in your views.

<?php
use App\Modules\ForgeLanguage\Definitions\LanguageSwitcherDefinition;
?>

<?= component(
    name: 'ForgeLanguage:language-switcher',
    props: [
        'definition' => new LanguageSwitcherDefinition(
            showFlags: true,
            showLabels: true,
            showCodes: false,
        )
    ]
) ?>

Adding Languages

To add new languages to your application, simply create a PHP file in your defined structure language path, by default app/Languages/ directory. For example, for English create en.php, and for Spanish create es.php.

Each file should return an associative array of keys and their translations.

// app/Languages/en.php
<?php
return [
    'welcome' => 'Welcome to Forge Kernel',
    'description' => 'Welcome to your development playground'
];