Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Utils / JS.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Parse Javascript content and extract translatable strings.
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2014
34 * $Id$
35 *
36 */
37 class CRM_Utils_JS {
38 /**
39 * Parse a javascript file for translatable strings
40 *
41 * @param string $jsCode
42 * Raw Javascript code.
43 * @return array
44 * of translatable strings
45 */
46 public static function parseStrings($jsCode) {
47 $strings = array();
48 // Match all calls to ts() in an array.
49 // Note: \s also matches newlines with the 's' modifier.
50 preg_match_all('~
51 [^\w]ts\s* # match "ts" with whitespace
52 \(\s* # match "(" argument list start
53 ((?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*
54 [,\)] # match ")" or "," to finish
55 ~sx', $jsCode, $matches);
56 foreach ($matches[1] as $text) {
57 $quote = $text[0];
58 // Remove newlines
59 $text = str_replace("\\\n", '', $text);
60 // Unescape escaped quotes
61 $text = str_replace('\\' . $quote, $quote, $text);
62 // Remove end quotes
63 $text = substr(ltrim($text, $quote), 0, -1);
64 $strings[$text] = $text;
65 }
66 return array_values($strings);
67 }
68 }