Merge pull request #14380 from civicrm/5.14
[civicrm-core.git] / CRM / Core / Resources / Strings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Manage translatable strings on behalf of resource files.
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 */
34 class CRM_Core_Resources_Strings {
35
36 /**
37 * Cache.
38 *
39 * @var CRM_Utils_Cache_Interface|null
40 */
41 private $cache = NULL;
42
43 /**
44 * @param CRM_Utils_Cache_Interface $cache
45 * Localization cache.
46 */
47 public function __construct($cache) {
48 $this->cache = $cache;
49 }
50
51 /**
52 * Flush the cache of translated strings.
53 */
54 public function flush() {
55 $this->cache->flush();
56 }
57
58 /**
59 * Get the strings from a file, using a cache if available.
60 *
61 * @param string $bucket
62 * The name of a cache-row which includes strings for this file.
63 * @param string $file
64 * File path.
65 * @param string $format
66 * Type of file (e.g. 'text/javascript', 'text/html').
67 *
68 * @return array
69 * List of translatable strings.
70 *
71 * @throws \Exception
72 */
73 public function get($bucket, $file, $format) {
74 // array($file => array(...strings...))
75 $stringsByFile = $this->cache->get($bucket);
76 if (!$stringsByFile) {
77 $stringsByFile = [];
78 }
79 if (!isset($stringsByFile[$file])) {
80 if ($file && is_readable($file)) {
81 $stringsByFile[$file] = $this->extract($file, $format);
82 }
83 else {
84 $stringsByFile[$file] = [];
85 }
86 $this->cache->set($bucket, $stringsByFile);
87 }
88 return $stringsByFile[$file];
89 }
90
91 /**
92 * Extract a list of strings from a file.
93 *
94 * @param string $file
95 * File path.
96 * @param string $format
97 * Type of file (e.g. 'text/javascript', 'text/html').
98 * @return array
99 * List of translatable strings.
100 * @throws Exception
101 */
102 public function extract($file, $format) {
103 switch ($format) {
104 case 'text/javascript':
105 return CRM_Utils_JS::parseStrings(file_get_contents($file));
106
107 case 'text/html':
108 // Magic! The JS parser works with HTML! See CRM_Utils_HTMLTest.
109 return CRM_Utils_JS::parseStrings(file_get_contents($file));
110
111 default:
112 throw new Exception("Cannot extract strings: Unrecognized file type.");
113 }
114 }
115
116 }