3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
34 class CRM_Core_Resources_Strings
{
39 * @var CRM_Utils_Cache_Interface|null
41 private $cache = NULL;
44 * @param CRM_Utils_Cache_Interface $cache
47 public function __construct($cache) {
48 $this->cache
= $cache;
52 * Flush the cache of translated strings.
54 public function flush() {
55 $this->cache
->flush();
59 * Get the strings from a file, using a cache if available.
61 * @param string $bucket
62 * The name of a cache-row which includes strings for this file.
65 * @param string $format
66 * Type of file (e.g. 'text/javascript', 'text/html').
69 * List of translatable strings.
73 public function get($bucket, $file, $format) {
74 // array($file => array(...strings...))
75 $stringsByFile = $this->cache
->get($bucket);
76 if (!$stringsByFile) {
79 if (!isset($stringsByFile[$file])) {
80 if ($file && is_readable($file)) {
81 $stringsByFile[$file] = $this->extract($file, $format);
84 $stringsByFile[$file] = [];
86 $this->cache
->set($bucket, $stringsByFile);
88 return $stringsByFile[$file];
92 * Extract a list of strings from a file.
96 * @param string $format
97 * Type of file (e.g. 'text/javascript', 'text/html').
99 * List of translatable strings.
102 public function extract($file, $format) {
104 case 'text/javascript':
105 return CRM_Utils_JS
::parseStrings(file_get_contents($file));
108 // Magic! The JS parser works with HTML! See CRM_Utils_HTMLTest.
109 return CRM_Utils_JS
::parseStrings(file_get_contents($file));
112 throw new Exception("Cannot extract strings: Unrecognized file type.");