term_id ?? ( $cat->id ?? 0 ); $fallback = $cat->name; } elseif ( is_numeric( $cat ) ) { $term_id = (int) $cat; $term = get_term( $term_id, 'category' ); $fallback = $term && ! is_wp_error( $term ) ? $term->name : (string) $cat; } else { return (string) $cat; } if ( $lang !== 'en' ) return $fallback; $en = get_term_meta( $term_id, 'titre_anglais', true ); return ( $en !== '' && $en !== false ) ? $en : $fallback; } // Register bilingual and en_url as Twig filters add_filter( 'timber/twig', function ( $twig ) { $twig->addFilter( new \Twig\TwigFilter( 'bilingual', 'thalim_bilingual' ) ); $twig->addFilter( new \Twig\TwigFilter( 'en_url', 'thalim_en_url' ) ); $twig->addFilter( new \Twig\TwigFilter( 'cat_name', 'thalim_cat_name' ) ); return $twig; } ); // Language switcher data (replaces pll_the_languages) // Output matches the structure header.twig expects: slug, url, current_lang function thalim_language_switcher(): array { $uri = THALIM_ORIGINAL_URI; $path = parse_url( $uri, PHP_URL_PATH ) ?? '/'; $query = ( $q = parse_url( $uri, PHP_URL_QUERY ) ) ? '?' . $q : ''; $is_en = thalim_current_language() === 'en'; $fr_path = $is_en ? ( substr( $path, 3 ) ?: '/' ) : $path; $en_path = $is_en ? $path : '/en' . $path; return [ 'fr' => [ 'slug' => 'fr', 'url' => home_url( $fr_path ) . $query, 'current_lang' => ! $is_en ], 'en' => [ 'slug' => 'en', 'url' => home_url( $en_path ) . $query, 'current_lang' => $is_en ], ]; } // Apply bilingual split to the browser tab title + translate category names add_filter('document_title_parts', function ($title_parts) { if (!empty($title_parts['title'])) { $title_parts['title'] = thalim_bilingual($title_parts['title']); // On category archives, replace the title with the translated category name if ( is_category() ) { $cat = get_queried_object(); if ( $cat ) { $title_parts['title'] = thalim_cat_name( $cat ); } } } return $title_parts; });