diff --git a/language.php b/language.php index a2a07ec..395548e 100644 --- a/language.php +++ b/language.php @@ -19,6 +19,10 @@ class language // The only available codeset is UTF8 // The languages are always in the format fr_FR (without the codeset) + + /** Language cache directory */ + public $cacheDir = "data/locale"; + /** Choose the best language in the browser list and which is available in locale path @param string|null $repLocale Directory where are stored the translations @@ -210,51 +214,39 @@ class language $category = $this->languageCategoryText ($category); $temporaries = glob ("$repLocale/$languageCode.$codeset/$category/*-*.mo"); $moFile = "$repLocale/$languageCode.$codeset/$category/$package.mo"; - $link = "$repLocale/$languageCode.$codeset/$category/$package-". - time().".mo"; + $linkBase = $this->cacheDir."/$languageCode.$codeset/$category/". + "$package-"; + $link = $linkBase.filemtime($moFile).".mo"; if (! file_exists ($moFile)) { return ""; } + // Manage the cache directory + if (! file_exists (dirname ($link))) + { + // Try to create the cache dir. If there is an error, return the official + // moFile. Apache will need to be restarted + @mkdir (dirname ($link), 0777, true); + } + if (is_dir (dirname ($link)) && is_writeable (dirname ($link)) && + is_readable (dirname ($link))) + { + // Manage the cache file + if (! file_exists ($link) || ! is_readable ($link) || + filemtime ($moFile) > filemtime ($link)) + { + $files = glob ("$linkBase*"); + array_map ("unlink", $files); + copy ($moFile, $link); + chmod ($link, 0666); + } + if (filemtime ($moFile) <= filemtime ($link)) + { + return $link; + } + } clearstatcache (false, $moFile); return $moFile; -/* if (! is_writeable (dirname ($moFile))) - { - // Si le répertoire est protégé en écriture, on donne le fichier original. - // On devra relancer Apache pour prendre en compte une modification - return $moFile; - } - if (count ($temporaries) > 1) - { - // Too much links are available : delete all and recreate it - foreach ($temporaries as $temp) - { - unlink ($temp); - } - $temporaries = array (); - } - if (empty ($temporaries)) - { - // Create the cache - copy ($moFile, $link); - } - else - { - // The cache exists. Is it up to date ? - $cache = $temporaries[0]; - if (filemtime ($cache) < filemtime ($moFile)) - { - // Obsolete cache : recreate it - unlink ($cache); - copy ($moFile, $link); - } - else - { - // Up to date cache - $link = $cache; - } - } - return $link;*/ } function languageActivation ($moFile, $languageCode, $category = LC_MESSAGES,