Merge pull request #2988 from totten/4.4-rmorgtools
[civicrm-core.git] / CRM / Utils / JS.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4+--------------------------------------------------------------------+
232624b1 5| CiviCRM version 4.4 |
6a488035
TO
6+--------------------------------------------------------------------+
7| Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
34 * $Id$
35 *
36 */
37class CRM_Utils_JS {
38 /**
39 * Parse a javascript file for translatable strings
40 *
41 * @param string $jsCode raw Javascript code
42 * @return array of translatable strings
43 */
44 public static function parseStrings($jsCode) {
45 $strings = array();
46 // Match all calls to ts() in an array.
47 // Note: \s also matches newlines with the 's' modifier.
48 preg_match_all('~
49 [^\w]ts\s* # match "ts" with whitespace
50 \(\s* # match "(" argument list start
51 ((?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*
52 [,\)] # match ")" or "," to finish
53 ~sx', $jsCode, $matches);
54 foreach ($matches[1] as $text) {
55 $quote = $text[0];
56 // Remove newlines
57 $text = str_replace("\\\n", '', $text);
58 // Unescape escaped quotes
59 $text = str_replace('\\' . $quote, $quote, $text);
60 // Remove end quotes
61 $text = substr(ltrim($text, $quote), 0, -1);
62 $strings[$text] = $text;
63 }
64 return array_values($strings);
65 }
232624b1 66}