3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Manage translatable strings on behalf of resource files.
32 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Core_Resources_Strings
{
38 * @var CRM_Utils_Cache_Interface|NULL
40 private $cache = NULL;
43 * @param CRM_Utils_Cache_Interface $cache
46 public function __construct($cache) {
47 $this->cache
= $cache;
51 * Flush the cache of translated strings.
53 public function flush() {
54 $this->cache
->flush();
58 * Get the strings from a file, using a cache if available.
60 * @param string $bucket
61 * The name of a cache-row which includes strings for this file.
64 * @param string $format
65 * Type of file (e.g. 'text/javascript', 'text/html').
67 * List of translatable strings.
69 public function get($bucket, $file, $format) {
70 $stringsByFile = $this->cache
->get($bucket); // array($file => array(...strings...))
71 if (!$stringsByFile) {
72 $stringsByFile = array();
74 if (!isset($stringsByFile[$file])) {
75 if ($file && is_readable($file)) {
76 $stringsByFile[$file] = $this->extract($file, $format);
79 $stringsByFile[$file] = array();
81 $this->cache
->set($bucket, $stringsByFile);
83 return $stringsByFile[$file];
87 * Extract a list of strings from a file.
91 * @param string $format
92 * Type of file (e.g. 'text/javascript', 'text/html').
94 * List of translatable strings.
97 public function extract($file, $format) {
99 case 'text/javascript':
100 return CRM_Utils_JS
::parseStrings(file_get_contents($file));
103 // Magic! The JS parser works with HTML! See CRM_Utils_HTMLTest.
104 return CRM_Utils_JS
::parseStrings(file_get_contents($file));
107 throw new Exception("Cannot extract strings: Unrecognized file type.");