From a195533ef8d4fea1984266dec418e8cff9124208 Mon Sep 17 00:00:00 2001 From: Markus Beh <markus.beh@ub.uni-freiburg.de> Date: Fri, 17 Jul 2015 08:57:09 -0400 Subject: [PATCH] Add ability to use theme-specific language files. Wording definitions from parent themes will be inherited or overwritten respecting the theme inheritance hierarchy. --- module/VuFind/src/VuFind/Cache/Manager.php | 18 +++++++ .../I18n/Translator/Loader/ExtendedIni.php | 12 +++++ .../src/VuFindTheme/Initializer.php | 51 +++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/module/VuFind/src/VuFind/Cache/Manager.php b/module/VuFind/src/VuFind/Cache/Manager.php index bcbccde404a..837d78aa120 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 e2253244e00..83a6454b89c 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 90683f04e59..61d505ed2e3 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() + ); + } + } } } -- GitLab