Commit | Line | Data |
---|---|---|
fd7dc3f3 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
fd7dc3f3 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
fd7dc3f3 TO |
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 | |
e7112fa7 | 32 | * @copyright CiviCRM LLC (c) 2004-2015 |
fd7dc3f3 TO |
33 | * $Id$ |
34 | */ | |
35 | class CRM_Core_Resources_Strings { | |
36 | ||
37 | /** | |
38 | * @var CRM_Utils_Cache_Interface|NULL | |
39 | */ | |
40 | private $cache = NULL; | |
41 | ||
42 | /** | |
43 | * @param CRM_Utils_Cache_Interface $cache | |
44 | * Localization cache. | |
45 | */ | |
46 | public function __construct($cache) { | |
47 | $this->cache = $cache; | |
48 | } | |
49 | ||
50 | /** | |
51 | * Flush the cache of translated strings. | |
52 | */ | |
53 | public function flush() { | |
54 | $this->cache->flush(); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Get the strings from a file, using a cache if available. | |
59 | * | |
60 | * @param string $bucket | |
61 | * The name of a cache-row which includes strings for this file. | |
62 | * @param string $file | |
63 | * File path. | |
64 | * @param string $format | |
65 | * Type of file (e.g. 'text/javascript', 'text/html'). | |
66 | * @return array | |
67 | * List of translatable strings. | |
68 | */ | |
69 | public function get($bucket, $file, $format) { | |
70 | $stringsByFile = $this->cache->get($bucket); // array($file => array(...strings...)) | |
71 | if (!$stringsByFile) { | |
72 | $stringsByFile = array(); | |
73 | } | |
74 | if (!isset($stringsByFile[$file])) { | |
75 | if ($file && is_readable($file)) { | |
76 | $stringsByFile[$file] = $this->extract($file, $format); | |
77 | } | |
78 | else { | |
79 | $stringsByFile[$file] = array(); | |
80 | } | |
81 | $this->cache->set($bucket, $stringsByFile); | |
82 | } | |
83 | return $stringsByFile[$file]; | |
84 | } | |
85 | ||
86 | /** | |
87 | * Extract a list of strings from a file. | |
88 | * | |
89 | * @param string $file | |
90 | * File path. | |
91 | * @param string $format | |
92 | * Type of file (e.g. 'text/javascript', 'text/html'). | |
93 | * @return array | |
94 | * List of translatable strings. | |
95 | * @throws Exception | |
96 | */ | |
97 | public function extract($file, $format) { | |
98 | switch ($format) { | |
99 | case 'text/javascript': | |
100 | return CRM_Utils_JS::parseStrings(file_get_contents($file)); | |
101 | ||
102 | case 'text/html': | |
103 | // Magic! The JS parser works with HTML! See CRM_Utils_HTMLTest. | |
104 | return CRM_Utils_JS::parseStrings(file_get_contents($file)); | |
105 | ||
106 | default: | |
107 | throw new Exception("Cannot extract strings: Unrecognized file type."); | |
108 | } | |
109 | } | |
110 | } |