server caching of gettext translation files

I just started translating my theme with the `gettext_th()` function and when testing the translations they didn't show up!!

That reminded me of something... the .mo files are cached. Restarting my local MAMP server and all was ok. Only problem my (shared) host does not allow me to restart their server...

I did some digging in an old project where I implemented a solution to this. Basically you a timestamp is added to the .mo filename
I found the info here http://blog.ghost3k.net/articles/php/11/gettext-caching-in-php

So now I am wondering did you ZP guys think about this already and am I missing something?
If not I guess I have to rewrite the `gettext_th()` function than right? But how can I do this without touching the core files?
Could I just create `my_gettext_th()` and put it in the themes functions.php file
Than put the my_gettext_th 'keyword' into the .po files and compile the .mo's?

Comments

  • acrylian Administrator, Developer
    No, we don't have anything for that. If I see the example correctly from a quick look it just tells get text to use a new file to workaround the cache. You could of course create a custom function theme or plugin based for this like you suggested.

    But how long is the file kept in the cache? If it is just a day or so you could just wait on the live server until they are updated automatically. How often do you change translations?

    I am not used to or even tried but maybe it is possible to disable the cache via htaccess or php.ini for these files as well or at least temporarilyl:
    http://httpd.apache.org/docs/2.0/mod/mod_cache.html

    Update: Maybe the first comment here is worth to try as well:
    http://php.net/manual/en/book.gettext.php
  • Hi acrylian, I just did what I said and it works great.
    The only thing is that off course the locale folder needs to be writable.
    One could also implement the deletion of old translation files with unlink()

    I actually have no idea how long the files are kept in the cache or when they are updated..
    Thanks for the links I'll check them out now.

    Anyway, here are the 2 function mods I put into function.php in my theme folder:

    `
    function my_gettext_th($string, $theme='') {
    global $_zp_gallery;
    if (empty($theme)) {
    $theme = $_zp_gallery->getCurrentTheme();
    }
    my_setupDomain($theme, 'theme');
    $translation = gettext($string);
    my_setupDomain();
    return $translation;
    }

    function my_setupDomain($domain=NULL,$type=NULL) {
    // locale directory // !!! needs write permissions in the locale folder !!!
    switch ($type) {
    case "plugin":
    $domainpath = getPlugin($domain . "/locale/");
    break;
    case "theme":
    $domainpath = SERVERPATH . "/" . THEMEFOLDER . "/" . $domain . "/locale";
    break;
    default:
    $domain = 'zenphoto';
    $domainpath = SERVERPATH . "/" . ZENFOLDER . "/locale/";
    break;
    }

    // current locale - fr_FR.UTF-8 -> fr_FR
    $locale = substr(ZENPHOTO_LOCALE, 0, 5);
    // path to the .MO file that we should monitor
    $filename = "$domainpath/$locale/LC_MESSAGES/$domain.mo";
    // check the .MO's modification time
    $mtime = date("Y-m-d_His", filemtime($filename));
    // our new unique .MO file
    $filename_new = "$domainpath/$locale/LC_MESSAGES/{$domain}_{$mtime}.mo";
    // check if we have created it before
    if (!file_exists($filename_new)) {
    // if not, create it now, by copying the original
    copy($filename, $filename_new);
    }
    // compute the new domain name
    $domain_new = "{$domain}_{$mtime}";

    bindtextdomain($domain_new, $domainpath);
    if (function_exists('bind_textdomain_codeset')) {
    bind_textdomain_codeset($domain, 'UTF-8');
    }
    textdomain($domain_new);
    }
    `
  • acrylian Administrator, Developer
    Good you got it working. We need to investigate regarding security with the writable folder I guess. I will have to leave that to my colleage. Maybe that is something for the theme/plugin translations.

    Maybe you could just ask your host about the server cache period? Would be interesting, I somehow never thought about that.
  • OK, just something to add.
    ZP comes with no files in the zp-core/locale/en_US/ folder.
    When using the above you need to add a LC_MESSAGES folder with zenphoto.po and zenphoto.mo files.
    These translations are not necessary, but apparently my function goes also sniffing in ZP's core files, looking for the filemtime, I don't know why yet...
    All works okay. My theme's locale folder are set to 755

    I used this mainly for working on my translations, once they are all set it's very easy to do a search and replace in my themes folder and replace all the `my_gettext_th()` functions back to the normal `gettext_th()`
  • These translations are not necessary, but apparently my function goes also sniffing in ZP's core files, looking for the filemtime, I don't know why yet...

    Good question, but you could find out by putting a debugging statement in he "default" switch case. You should not get there from a theme call on `my_setupDomain()`

    Never mind. It is the second `my_setupDomain()` call in `my_gettext_th()` that does it. You could change the code to set a flag in the switch whether to make the file copy or not.
  • Sweet, cheers sbillard!
Sign In or Register to comment.