MO file caching?

I created German translation files for a theme using Poedit and compiled the .po file to an .mo file, which I placed in the languages directory for my theme. I didn’t make any other edits to the theme. It just worked, which was a nice surprise.

The issue is that after editing and recompiling my translation files, the changes are not reflected on my site – the old translation still appears.

I’m guessing the canonical answer is to restart Apache, but seeing as how my site is in a shared hosting environment, I don’t think that’s an option for me. Are there any workarounds for fixing this or some way I can edit my theme to avoid the issue altogether?

There’s caching that happens with the locale component, which I believe includes the translations also. On a typical system that stuff is cached to /tmp. It could also be caching at some other layer, I suppose. Normally the caching isn’t a problem when updating translations, though.

One thing to try would be flipping the language from German to something else, then back (after loading at a least a page) to see if that “jogs” the system.

Setting

locale.cache = ""

in the application/config/config.ini should disable the locale cache completely, also.

Lastly, I believe the default TTL for caches from Zend Framework 1 is 3600 seconds, so simply waiting may do the trick.

Ok, it looks like what’s actually happening is that the translations are only being loaded when I activate the Translations plugin (I had tested it previously but didn’t realize I still had it installed).

I can use the “Reset translations” option in the plugin config to clear the cache. When I deactivate the plugin, the translations continue to appear (though I’m not sure for how long… it’s been at least 30 minutes so far).

For what it’s worth it seems like the relevant code in the Translations plugin for clearing the cache is this:

$cache = Zend_Registry::get('Zend_Translate');
$cache::clearCache();

Along with adding the translation sources, the plugin doesn’t do anything else, which makes me think it ought to be replicable in the theme.

So I guess the question is whether I need the Translations plugin in order to load/manage translations in my theme, or is there a way to do this without a plugin?

In my theme, I’ve added the following…

/* in custom.php */
add_translation_source(dirname(__FILE__) . '/languages');
<!--in header.php-->
<html lang="<?php echo get_html_lang();?>">  

Is there something I’m overlooking or is this basically why the plugin exists? I had assumed it was just there for users who didn’t want to edit theme files directly.

I think you’re correct that the Translations plugin is pretty much there for people who don’t want to or can’t edit their themes to load translations themselves.

You don’t need it to load translations from a theme though, what you’ve done in code is fine. The cache clear button may add some convenience though if you’re changing the translations a lot. Or as mentioned above you can disable caching or clear it in other ways.

Ok, this is starting to make some sense. I think perhaps the reason I thought it wasn’t working without the plugin was due to its clearing of the cache and not necessarily any of its other functionality. I added the new/modified translations after setting the language in config.ini so I guess if I’d waited an hour (3600 seconds), my changes would have appeared on their own.

I’ve added the code below in custom.php for convenience and will probably add something equivalent as a theme option at some point.

// temporarily uncomment the lines below to clear the translations cache...
// $cache = Zend_Registry::get('Zend_Translate');
// $cache::clearCache();

Thanks John!