__DIR__ . '/cache/twig', // Uncomment for production 'debug' => true, ]); // --- Translation Setup --- $translationService = new \App\Services\TranslationService( __DIR__ . '/lang', SUPPORTED_LOCALES, DEFAULT_LOCALE ); // Get current locale from session or default $locale = $_SESSION['locale'] ?? DEFAULT_LOCALE; $translator = $translationService->getTranslator($locale); // Add translation function to Twig $twig->addFunction(new \Twig\TwigFunction('trans', function (string $id, array $parameters = [], string $domain = null, string $locale = null) use ($translator) { return $translator->trans($id, $parameters, $domain, $locale); })); // Add current locale to Twig globals $twig->addGlobal('current_locale', $locale); $twig->addGlobal('supported_locales', SUPPORTED_LOCALES); $twig->addGlobal('app_name', APP_NAME); // Add translated confirm messages for JS $twig->addGlobal('delete_part_confirm', $translator->trans('Are you sure you want to delete this part?')); $twig->addGlobal('delete_category_confirm', $translator->trans('Are you sure you want to delete this category?')); // Build category tree for sidebar try { $db = App\Database\Database::getInstance(); $categories = App\Models\Category::getAll($db); $items = App\Models\Item::getAll($db); function buildTree($categories, $items, $parentId = null, &$visited = []) { $tree = []; foreach ($categories as $cat) { if ($cat['parent_id'] == $parentId && !in_array($cat['id'], $visited)) { $visited[] = $cat['id']; $node = [ 'id' => $cat['id'], 'name' => $cat['name'], 'children' => buildTree($categories, $items, $cat['id'], $visited), 'items' => [] ]; foreach ($items as $item) { if ($item['category_id'] == $cat['id']) { $node['items'][] = $item; } } $tree[] = $node; } } return $tree; } $categoryTree = buildTree($categories, $items); } catch (Exception $e) { error_log('Error building category tree: ' . $e->getMessage()); $categoryTree = []; } $twig->addGlobal('category_tree', $categoryTree);