Use override_load_textdomain for cache translation and improve performance

The question:

I have a multisite wordpress with multiple localized sites (es_ES, de_DE, it_IT).
I want improve page speed by caching wordpress translation file.

My code works (inspired by Tristan Darricau)! and my page speed is improved of 0.50 seconds.

Now, my question is: My code is right or can break something (core, theme, plug-in), i have tested and it seem to work, but i’m a newbie of wp!

/**
 * Cache translation
 * @param boolean $override
 * @param string  $domain
 * @param string  $mofile
 * @return boolean
 */
function my_override_load_textdomain($override, $domain, $mofile) {

    global $l10n;

    // check if $mofile exisiste and is readable
    if ( !(is_file($mofile) && is_readable($mofile)) ){
        return false;
    }

    // creates a unique key for cache
    $key   = md5($mofile);

    // I try to retrive data from cache
    $data  = wp_cache_get($key, $domain);

    // Retrieve the last modified date of the translation files
    $mtime = filemtime($mofile);

    $mo = new MO();

    // if cache not return data or data it's old
    if ( !$data || !isset($data['mtime']) || ($mtime > $data['mtime']) ) {
        // retrive data from MO file
        if ( $mo->import_from_file( $mofile ) ){

            $data = array(
                'mtime'   => $mtime,
                'entries' => $mo->entries,
                'headers' => $mo->headers
            );

            // save data in cache
            wp_cache_set($key, $data, $domain, 0);

        } else {
            return false;
        }

    } else {
        $mo->entries = $data['entries'];
        $mo->headers = $data['headers'];
    }

    if ( isset( $l10n[$domain] ) ) {
        $mo->merge_with( $l10n[$domain] );
    }

    $l10n[$domain] = &$mo;

    return true;
}
add_filter('override_load_textdomain', 'my_override_load_textdomain',1,3);

Side note: i know this is a question for code review stack’s platform, but all wordpress’guys are here, and this is a question for wordpress’guys.

I apologize for my bad English

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

It seems your code is OK and won’t break anything but for it to work properly across different pages, maybe you should use Transient API functions instead of Object Cache counterparts.

Why?

Object Cache is not persistent by default (i.e. does not work across different pages). It will persist only if install an persistent cache plugin and a persistent cache backend (e.g. redis, memcached).

The Transient API worksa little bit “smarter”: it keeps the cached content on database to be used “persistently” if there’s no persistent object cache set, but will use the object cache API if there is an persistent cache backend and plugin.

Use the Transients API instead of these functions if you need to guarantee that your data will be cached. If persistent caching is configured, then the transients functions will use the wp_cache functions described in this document. However if persistent caching has not been enabled, then the data will instead be cached to the options table.

(source: https://codex.wordpress.org/Class_Reference/WP_Object_Cache )

Be aware that database performance may be affected, so, test and measure carefully.

Method 2

The only problem that seems to be with that code/idea is that you are caching the whole translation instead of caching the translation nodes (the phrases actually being translated). This leads to higher memory demand from the caching system, but probably not in any significant way (base assumption is that you use some object caching plugin as well, otherwise the whole code is pointless)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment