Support Log in

37. Caching with Multilingual Sites

Developer Guide
Prefer video? Watch the Lang Forge tutorials 9 short guides covering every feature Watch

Best practices for page and object caching on multilingual WordPress sites.

Page Cache Configuration

Most page caching plugins automatically create separate cache files per URL. Since Lang Forge uses URL-based language differentiation (directory prefixes, parameters, subdomains, or domains), each language version gets its own cache entry automatically.

php
// No special configuration needed for most caching plugins.
// The URL-based language format ensures different language versions
// get different cache keys automatically.

// If using a custom caching solution, ensure the cache key includes the language:
$cache_key = 'page_' . md5($_SERVER['REQUEST_URI']);
// Since /en/about and /de/about are different URLs, they get different cache keys.

Object Cache with Language Awareness

php
/**
 * When caching computed data that varies by language,
 * include the language code in the cache key.
 */
function theme_get_featured_posts() {
    $lang = lf_get_current_language();
    $cache_key = 'featured_posts_' . $lang;
    $cached = wp_cache_get($cache_key, 'theme');

    if ($cached !== false) {
        return $cached;
    }

    $posts = get_posts([
        'posts_per_page' => 5,
        'meta_query'     => [
            ['key' => '_lf_language', 'value' => $lang],
            ['key' => '_featured', 'value' => '1'],
        ],
    ]);

    wp_cache_set($cache_key, $posts, 'theme', 3600);
    return $posts;
}

Transient Cache for Translation Data

php
/**
 * Cache translation status data in a transient.
 * Useful for dashboard widgets that query translation completeness.
 */
function theme_get_translation_stats() {
    $transient_key = 'lf_translation_stats';
    $stats = get_transient($transient_key);

    if ($stats !== false) {
        return $stats;
    }

    $languages = lf_get_languages();
    $stats = [];

    foreach ($languages as $code => $lang) {
        $query = new WP_Query([
            'post_type'  => 'post',
            'meta_key'   => '_lf_language',
            'meta_value' => $code,
            'fields'     => 'ids',
        ]);
        $stats[$code] = [
            'name'  => $lang['name'],
            'count' => $query->found_posts,
        ];
    }

    set_transient($transient_key, $stats, HOUR_IN_SECONDS);
    return $stats;
}

// Invalidate cache when a translation is created
add_action('lf_translation_created', function () {
    delete_transient('lf_translation_stats');
}, 10, 0);

CDN Configuration

php
// When using a CDN, ensure language-specific URLs are treated as different resources.
// Most CDNs cache by full URL, so directory-based language prefixes work automatically.

// For cookie-based or header-based language detection (not recommended),
// you would need to configure the CDN to vary by the relevant header/cookie.
// Lang Forge uses URL-based detection by default, which avoids this complexity.

Forge AI Assistant Online

Hi! I'm the Lang Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs