2017 © Pedro Peláez
 

wordpress-plugin translation-cache

Improves site performance by caching translation files using WordPress object cache.

image

inpsyde/translation-cache

Improves site performance by caching translation files using WordPress object cache.

  • Tuesday, October 10, 2017
  • by gmazzap
  • Repository
  • 9 Watchers
  • 29 Stars
  • 131 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 4 Versions
  • 5 % Grown

The README.md

Inpsyde Translation Cache

Improves site performance by caching translation files using WordPress object cache., (*1)


Description

'Inpsyde Translation Cache' provides caching of the translation .mo files using WP Object Cache mechanism., (*2)

Performance benefit of the plugin can be seen when using persistent cache plugin because, by default, WordPress object cache does not 'survive' the single request and translation files are already cached on a per-request basis in WP., (*3)

For this reason the plugin, by default, does nothing when there's no persistent caching plugin installed., (*4)


MU plugin self-installation

Considering that many plugins have their language files, to be allowed to cache plugins translations, 'Inpsyde Translation Cache' has to run as MU plugin., (*5)

For this reason, 'Inpsyde Translation Cache' copies its main file to the MU plugin folder on installation, so it can work as MU plugin., (*6)

The 'regular' plugin stays active though, but doing nothing, or better, it listed for deactivation to delete the MU plugin copy., (*7)

In fact, when 'regular' plugin is deactivated, the MU plugin copy is deleted (unless it was modified)., (*8)

Please note that on some configurations this 'self-installation' routine may not work, e.g. if MU plugins folder is not writable., (*9)

In those cases 'Inpsyde Translation Cache' will continue to work as a regular plugin, but it will not be able to cache translation files loaded before 'plugins_loaded' hook is fired. Moreover, an admin notice is also shown to suggest users to manually copy main plugin file to MU plugins folder to improve performance., (*10)


Cache invalidation

'Inpsyde Translation Cache' stores all the text domains:, (*11)

  • core
  • plugins
  • themes

it means that when a new version of those is installed, the cache need to cleared., (*12)

Automatic invalidation

For invalidation of WordPress core translations the plugin relies on WordPress version: if that change all cached core translations are invalidated., (*13)

For plugin and themes, things are less straightforward., (*14)

'Inpsyde Translation Cache' uses 'switch_theme' hook to invalidate cache of both the 'old' and the 'new' theme., (*15)

It also uses 'activated_plugin' and 'deactivated_plugin' to invalidate cache for plugins., (*16)

However, to invalidate cache of themes and plugins 'Inpsyde Translation Cache' needs to know the text domain they use., (*17)

For that scope, 'Inpsyde Translation Cache' uses 'TextDomain' file header of plugin and themes., (*18)

Considering that 'TextDomain' file header is not mandatory, it is possible that some plugins or themes that don't provide that header (even if they should)., (*19)

Manual invalidation

In those cases it is possible to invalidate the cache 'manually' by calling Inpsyde\MoCache::flush_cache() method., (*20)

For example, let's assume there's a plugin that loads a text domain like this:, (*21)

 load_plugin_textdomain( 'some-plugin-txt-domain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 

and let's assume this plugin has not in plugin headers something like:, (*22)

Text Domain: some-plugin-txt-domain

'Inpsyde Translation Cache' will not be able to flush cache for that plugin automatically when activated and deactivated., (*23)

However, 'manually' invalidating cache for that plugin would be just a matter of:, (*24)

function invalidate_some_plugin_translation_cache() {
    if ( class_exists( 'Inpsyde\TranslationCache\MoCache' ) ) {
        Inpsyde\TranslationCache\MoCache::flush_cache( 'some-plugin-txt-domain' );
    }
}

register_activation_hook(
    WP_PLUGIN_DIR . '/some-plugin-dir/some-plugin-filename.php',
    'invalidate_some_plugin_translation_cache'
);

register_deactivation_hook(
    WP_PLUGIN_DIR . '/some-plugin-dir/some-plugin-filename.php',
    'invalidate_some_plugin_translation_cache'
);

Of course, when it is possible to edit the plugin to include the Text Domain plugin header, it is surely better, because 'Inpsyde Translation Cache' will be able to invalidate the cache for plugin translations automatically., (*25)

For themes the workflow is very similar, just use 'switch_theme' hook to intercept theme change., (*26)

Invalidation by version

There's another way to invalidate cached translations., (*27)

'Inpsyde Translation Cache' builds an unique key for the combination of text domain and .mo file path., (*28)

This unique key is built using an 'hash' of:, (*29)

  • text domain
  • .mo file path
  • 'Inpsyde Translation Cache' own version
  • an arbitrary 'version' string that can be set via mocache_cache_version filter

It means that using mocache_cache_version filter it is possible to invalidate all the stored keys for a given text domain., (*30)

For example, assuming same plugin as above we could do:, (*31)

add_filter( 'mocache_cache_version', function( $version, $domain ) {
    $plugin_path = WP_PLUGIN_DIR . '/some-plugin-dir/some-plugin-filename.php';
    $headers     = get_plugin_data( $plugin_file_path, FALSE, FALSE );

    return $headers['Version'];
}, 10, 2 );

So we use the 'Version' plugin header to invalidate the cache whenever the version change., (*32)

Compared to the 'manual' approach above, this approach has the benefit to do not explicitly call 'Inpsyde Translation Cache' objects, so there's no need to check for class existence, in fact, if 'Inpsyde Translation Cache' is not installed or not activated, then the code does nothing with no effect., (*33)

But it also has two flaws:, (*34)

  • Cache entries for 'old' versions are not deleted, they are just ignored because there's a new version. However, they will be discarded when 'natural' expiration for keys is reached and considering that, by default, the 'natural' expiration is 12 hours, this issue should not be that critical.
  • A code like the one above requires the plugin to define the version in headers and to be effective it relies on the version be updated every time any translation file is changed. Even if these are common practices, theme / plugins authors may just ignore or forget them.

So, again, the suggestion is to use Text Domain header whenever possible, maybe suggesting to maintainers of third party plugins and themes to add it when not present., (*35)


Available hooks

'Inpsyde Translation Cache' provides some hooks for customization. Most of them will only work properly from a MU plugin, especially if 'Inpsyde Translation Cache' is running as MU plugin., (*36)

Available action hooks:, (*37)

  • inpsyde_translation_cache, Fires just after the plugin class has been loaded. Useful to wrap calls to Inpsyde\TranslationCache\MoCache::flush_cache()

Available filter hooks:, (*38)

  • mocache_cache_enabled Can be used to disable plugin programmatically returning false via hooked callback., (*39)

  • mocache_cache_version Can be used to invalidate the cache for given text domain (passed as argument) If returned version change, cache is invalidated. See ' Invalidation by version' above., (*40)

  • mocache_cache_expire Can be used to customize the duration in seconds of cached values. Returning a value less or equal to zero prevent cache to be done at all., (*41)


Installation

'Inpsyde Translation Cache' is available via Composer with package name inpsyde/mo-cache, but it does not require Composer to be installed or used., (*42)

The 'classical' installation method (_download_ -> put in plugins folder -> activate) works as well., (*43)


Requirements

  • PHP 5.5 or higher.
  • WordPress, tested currently in version 5.1

License

This repository is a free software, and is released under the terms of the GNU General Public License version 2 or (at your option) any later version. See LICENSE for complete license., (*44)

'Inpsyde Translation Cache' incorporates work covered by the following copyright and permission notices:, (*45)

  ___                           _      
 |_ _|_ __  _ __  ___ _   _  __| | ___ 
  | || '_ \| '_ \/ __| | | |/ _` |/ _ \
  | || | | | |_) \__ \ |_| | (_| |  __/
 |___|_| |_| .__/|___/\__, |\__,_|\___|
           |_|        |___/            

The team at Inpsyde is engineering the Web since 2006., (*46)

The Versions

10/10 2017

dev-master

9999999-dev

Improves site performance by caching translation files using WordPress object cache.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

by Masaki Takeuchi

wordpress translations language cache caching translation i18n internationalization localization languages translate performance l10n mo file

20/09 2017

1.0.2

1.0.2.0

Improves site performance by caching translation files using WordPress object cache.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

by Masaki Takeuchi

wordpress translations language cache caching translation i18n internationalization localization languages translate performance l10n mo file

31/10 2016

1.0.1

1.0.1.0

Improves site performance by caching translation files using WordPress object cache.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

by Masaki Takeuchi

wordpress translations language cache caching translation i18n internationalization localization languages translate performance l10n mo file

30/10 2016

1.0.0

1.0.0.0

Improves site performance by caching translation files using WordPress object cache.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

by Masaki Takeuchi

wordpress translations language cache caching translation i18n internationalization localization languages translate performance l10n mo file