cache = $cache; } /** * Flush the cache of translated strings. */ public function flush() { $this->cache->flush(); } /** * Get the strings from a file, using a cache if available. * * @param string $bucket * The name of a cache-row which includes strings for this file. * @param string $file * File path. * @param string $format * Type of file (e.g. 'text/javascript', 'text/html'). * @return array * List of translatable strings. */ public function get($bucket, $file, $format) { $stringsByFile = $this->cache->get($bucket); // array($file => array(...strings...)) if (!$stringsByFile) { $stringsByFile = array(); } if (!isset($stringsByFile[$file])) { if ($file && is_readable($file)) { $stringsByFile[$file] = $this->extract($file, $format); } else { $stringsByFile[$file] = array(); } $this->cache->set($bucket, $stringsByFile); } return $stringsByFile[$file]; } /** * Extract a list of strings from a file. * * @param string $file * File path. * @param string $format * Type of file (e.g. 'text/javascript', 'text/html'). * @return array * List of translatable strings. * @throws Exception */ public function extract($file, $format) { switch ($format) { case 'text/javascript': return CRM_Utils_JS::parseStrings(file_get_contents($file)); case 'text/html': // Magic! The JS parser works with HTML! See CRM_Utils_HTMLTest. return CRM_Utils_JS::parseStrings(file_get_contents($file)); default: throw new Exception("Cannot extract strings: Unrecognized file type."); } } }