diff --git a/module/VuFind/src/VuFind/Cache/Manager.php b/module/VuFind/src/VuFind/Cache/Manager.php index bcbccde404ad7f57f47e391c2f163bd66e5ecc58..837d78aa1200acabc4aaee279c2f55e5d74620ea 100644 --- a/module/VuFind/src/VuFind/Cache/Manager.php +++ b/module/VuFind/src/VuFind/Cache/Manager.php @@ -187,6 +187,24 @@ class Manager return $this->directoryCreationError; } + /** + * Create a new file cache for the given theme name if neccessary. Return + * the name of the cache. + * + * @param string $themeName Name of the theme + * + * @return string + */ + public function addLanguageCacheForTheme($themeName) + { + $cacheName = 'languages-' . $themeName; + $this->createFileCache( + $cacheName, + $this->getCacheDir() . 'languages/' . $themeName + ); + return $cacheName; + } + /** * Create a "no-cache" setting. * diff --git a/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php b/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php index e2253244e00efe0df67a6d7a3e6be372e241a309..83a6454b89c11c8f1cc7a19b9be4845b7ae7d79c 100644 --- a/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php +++ b/module/VuFind/src/VuFind/I18n/Translator/Loader/ExtendedIni.php @@ -91,6 +91,18 @@ class ExtendedIni implements FileLoaderInterface $this->reader = ($reader === null) ? new ExtendedIniReader() : $reader; } + /** + * Add additional directories to the path stack. + * + * @param array|string $pathStack Path stack addition(s). + * + * @return void + */ + public function addToPathStack($pathStack) + { + $this->pathStack = array_merge($this->pathStack, (array)$pathStack); + } + /** * Load method defined by FileLoaderInterface. * diff --git a/module/VuFindTheme/src/VuFindTheme/Initializer.php b/module/VuFindTheme/src/VuFindTheme/Initializer.php index 90683f04e598d471bebfb2786ee0068ea0182640..61d505ed2e3378b8cd34ded78f9614780d3008ea 100644 --- a/module/VuFindTheme/src/VuFindTheme/Initializer.php +++ b/module/VuFindTheme/src/VuFindTheme/Initializer.php @@ -385,5 +385,56 @@ class Initializer $current->setPaths($templatePathStack); } } + + // Add theme specific language files for translation + $this->updateTranslator($themes); + } + + /** + * Support method for setUpThemes() - add theme specific language files for + * translation. + * + * @param array $themes Theme configuration information. + * + * @return void + */ + protected function updateTranslator($themes) + { + $pathStack = []; + foreach (array_keys($themes) as $theme) { + $dir = APPLICATION_PATH . '/themes/' . $theme . '/languages'; + if (is_dir($dir)) { + $pathStack[] = $dir; + } + } + + if (!empty($pathStack)) { + try { + $translator = $this->serviceManager->get('VuFind\Translator'); + + $pm = $translator->getPluginManager(); + $pm->get('extendedini')->addToPathStack($pathStack); + } catch (\Zend\Mvc\Exception\BadMethodCallException $e) { + // This exception likely indicates that translation is disabled, + // so we can't proceed. + return; + } + + // Override the default cache with a theme-specific cache to avoid + // key collisions in a multi-theme environment. + try { + $cacheManager = $this->serviceManager->get('VuFind\CacheManager'); + $cacheName = $cacheManager->addLanguageCacheForTheme($theme); + $translator->setCache($cacheManager->getCache($cacheName)); + } catch (\Exception $e) { + // Don't let a cache failure kill the whole application, but make + // note of it: + $logger = $this->serviceManager->get('VuFind\Logger'); + $logger->debug( + 'Problem loading cache: ' . get_class($e) . ' exception: ' + . $e->getMessage() + ); + } + } } }