Auto-sync enabled

FlatlyPage

Version 1.0.1 • 59 files • 798.19 KB
admin/lang.php
<?php
// inc/lang.php

function get_page_content($page_id, $lang = 'pl') {
    $basePath = __DIR__ . "/../data/{$page_id}.php";
    
    if (!file_exists($basePath)) return [];

    $sourceData = include $basePath;

    // Jeśli język domyślny, zwracamy oryginał
    if ($lang === 'pl') {
        return $sourceData;
    }

    $langPath = __DIR__ . "/../data/{$page_id}_{$lang}.php";

    // Jeśli plik języka nie istnieje, tworzymy go na bazie oryginału (1:1)
    if (!file_exists($langPath)) {
        save_page_content($page_id, $sourceData, $lang);
        return $sourceData;
    }

    $langData = include $langPath;

    // Łączymy strukturę oryginału z treścią tłumaczenia (Struktura 1:1)
    return merge_recursive_sync($sourceData, $langData);
}

function merge_recursive_sync($source, $translation) {
    foreach ($source as $key => $value) {
        if (is_array($value)) {
            // Rekurencyjnie wchodzimy głębiej (np. w bloki)
            $source[$key] = merge_recursive_sync($value, $translation[$key] ?? []);
        } else {
            // Jeśli w tłumaczeniu istnieje wartość i nie jest pusta, nadpisujemy oryginał
            if (isset($translation[$key]) && !empty($translation[$key]) && $key !== 'id' && $key !== 'type') {
                $source[$key] = $translation[$key];
            }
        }
    }
    return $source;
}

function save_page_content($page_id, $data, $lang = 'pl') {
    $suffix = ($lang === 'pl') ? '' : '_' . $lang;
    $path = __DIR__ . "/../data/{$page_id}{$suffix}.php";
    
    $content = "<?php\nreturn " . var_export($data, true) . ";";
    return file_put_contents($path, $content);
}