CodShot
PHP Public

Language Detection and Translation Loader

Manages front-end language detection, session and cookie handling, and loads language translation scopes from PHP files with fallback support.

PHP
<?php

declare(strict_types=1);

// ------------------------------------------------------------
// Global language code list
// ------------------------------------------------------------
if (!defined('ALLOWED_LANG_CODES')) {
    define('ALLOWED_LANG_CODES', [
        'bg','cs','da','de','el','en','es','fi','fr','hr','hu','it','lt','mk',
        'nl','no','pl','pt','ro','ru','sk','sl','sq','sr','sv','tr','cnr'
    ]);
}

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

// ------------------------------------------------------------
// Front nyelvek
// ------------------------------------------------------------
$frontLangs = function_exists('frontend_active_languages')
    ? frontend_active_languages()
    : (
        function_exists('availableFrontLangs')
            ? availableFrontLangs()
            : [
                'en' => 'English',
                'hu' => 'Magyar',
                'es' => 'Español',
                'de' => 'Deutsch',
                'fr' => 'Français',
                'it' => 'Italiano',
            ]
    );

$allowedFrontLangs = array_keys($frontLangs);

function detect_current_front_lang(array $allowedFrontLangs): string
{
    $default = 'en';

    if (function_exists('current_lang')) {
        $routerLang = current_lang();
        if (is_string($routerLang) && in_array($routerLang, $allowedFrontLangs, true)) {
            return $routerLang;
        }
    }

    if (!empty($_SESSION['lang']) && in_array($_SESSION['lang'], $allowedFrontLangs, true)) {
        return $_SESSION['lang'];
    }

    if (!empty($_COOKIE['lang']) && in_array($_COOKIE['lang'], $allowedFrontLangs, true)) {
        return $_COOKIE['lang'];
    }

    if (in_array($default, $allowedFrontLangs, true)) {
        return $default;
    }

    return !empty($allowedFrontLangs) ? $allowedFrontLangs[0] : 'en';
}

function lang_normalize_scope_name(string $scope): string
{
    return strtolower(trim($scope));
}

function lang_allowed_scopes(): array
{
    return ['public', 'auth', 'account', 'emails', 'newsletter'];
}

function lang_scope_file_path(string $langCode, string $scope): ?string
{
    $langCode = strtolower(trim($langCode));
    $scope = lang_normalize_scope_name($scope);

    if (!in_array($scope, lang_allowed_scopes(), true)) {
        return null;
    }

    $baseDir = __DIR__ . '/' . $langCode;
    $file = $baseDir . '/' . $scope . '.php';

    if (is_file($file)) {
        return $file;
    }

    $fallbackFile = __DIR__ . '/en/' . $scope . '.php';

    if (is_file($fallbackFile)) {
        return $fallbackFile;
    }

    return null;
}

function lang_load_scopes(array|string $scopes): void
{
    global $translations, $loadedLangScopes, $currentLangCode;

    $scopeList = is_array($scopes) ? $scopes : [$scopes];

    foreach ($scopeList as $scope) {
        $scope = lang_normalize_scope_name((string) $scope);

        if ($scope === '') {
            continue;
        }

        if (in_array($scope, $loadedLangScopes, true)) {
            continue;
        }

        $file = lang_scope_file_path($currentLangCode, $scope);

        if ($file === null) {
            $loadedLangScopes[] = $scope;
            continue;
        }

        $data = require $file;

        if (is_array($data)) {
            $translations = array_merge($translations, $data);
        }

        $loadedLangScopes[] = $scope;
    }
}

function lang_is_scope_loaded(string $scope): bool
{
    global $loadedLangScopes;

    $scope = lang_normalize_scope_name($scope);

    return in_array($scope, $loadedLangScopes, true);
}

function lang_get_loaded_scopes(): array
{
    global $loadedLangScopes;

    return $loadedLangScopes;
}

$currentLangCode = detect_current_front_lang($allowedFrontLangs);

$_SESSION['lang'] = $currentLangCode;

setcookie('lang', $currentLangCode, [
    'expires'  => time() + (30 * 24 * 60 * 60),
    'path'     => '/',
    'secure'   => (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'),
    'httponly' => false,
    'samesite' => 'Lax',
]);

$translations = [];
$loadedLangScopes = [];

function __(string $str): string
{
    global $translations;

    return $translations[$str] ?? $str;
}

if (!defined('CF_LANG')) {
    define('CF_LANG', $currentLangCode);
}

if (!defined('F_LANGS')) {
    define('F_LANGS', $frontLangs);
}

$lng = $currentLangCode;

Snippet actions

Pluto
Pluto
Updated: 2026-09-01 16:05
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support