Framework Docs Language

Language

The Language class loads translation phrases from JSON files in lang/ and exposes them via a simple static API. A PHP opcode cache is written on the first load so later requests skip JSON parsing entirely.

Namespace: Wojo\Core\Language

API

Method Signature Description
run() static run(?string $urlSegment = null): void Bootstrap the language system; call once in bootstrap. Priority: URL segment → cookie → config default.
get() static get(string $key, array $replace = [], bool $emphasize = false): string Retrieve a phrase by key; supports :placeholder replacement
isValid() static isValid(string $lang): bool Check whether a language code has a corresponding .lang.json file
getAvailableLanguages() static getAvailableLanguages(): array Return ['en' => 'English', 'fr' => 'French', ...] for all installed language files
getSections() static getSections(?string $lang = null): array Return the top-level section keys in the language JSON
getSectionContent() static getSectionContent(string $section, ?string $lang = null): array Return all phrase pairs for a specific section
getPhrasePairs() static getPhrasePairs(?string $lang = null): array Return the flattened key→phrase array for a language

Language File Format

Language files live in lang/{code}.lang.json and use section-grouped JSON:

PHP
{
  "general": {
    "SITE_TITLE": "My App",
    "WELCOME_MSG": "Welcome, :name!"
  },
  "errors": {
    "INVALID_ID": "The requested item was not found.",
    "ACCESS_DENIED": "You do not have permission."
  }
}

Examples

PHP
// Basic lookup (global t() helper wraps Language::get())
echo t('SITE_TITLE');            // → 'My App'

// With placeholder replacement
echo Language::get('WELCOME_MSG', ['name' => 'Alex']);
// → 'Welcome, Alex!'

// With bold emphasis
echo Language::get('WELCOME_MSG', ['name' => 'Alex'], emphasize: true);
// → 'Welcome, Alex!'

// List all installed languages
$langs = Language::getAvailableLanguages();
// → ['de' => 'German', 'en' => 'English', 'fr' => 'French']

// Check if a language code is installed
if (Language::isValid('fr')) { ... }