From 9f36eb6a3e4214020fd1f32614f3f796f242efac Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Mon, 19 Sep 2011 15:10:52 -0400 Subject: [PATCH] Issue #1281932: Convert locale js parser to extended regex and add tests. --- includes/locale.inc | 34 +++++++++++++- modules/locale/locale.test | 107 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 2 deletions(-) diff --git a/includes/locale.inc b/includes/locale.inc index 199edd1..79c9c2c 100644 --- a/includes/locale.inc +++ b/includes/locale.inc @@ -525,10 +525,40 @@ function _locale_parse_js_file($filepath) { // Match all calls to Drupal.t() in an array. // Note: \s also matches newlines with the 's' modifier. - preg_match_all('~[^\w]Drupal\s*\.\s*t\s*\(\s*(' . LOCALE_JS_STRING . ')\s*[,\)]~s', $file, $t_matches); + preg_match_all('~ + [^\w]Drupal\s*\.\s*t\s* # match "Drupal.t" with whitespace + \(\s* # match "(" argument list start + (' . LOCALE_JS_STRING . ')\s* # capture string argument + [,\)] # match ")" or "," to finish + ~sx', $file, $t_matches); // Match all Drupal.formatPlural() calls in another array. - preg_match_all('~[^\w]Drupal\s*\.\s*formatPlural\s*\(\s*.+?\s*,\s*(' . LOCALE_JS_STRING . ')\s*,\s*((?:(?:\'(?:\\\\\'|[^\'])*@count(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*@count(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*[,\)]~s', $file, $plural_matches); + preg_match_all('~ + [^\w]Drupal\s*\.\s*formatPlural\s* # match "Drupal.formatPlural" with whitespace + \( # match "(" argument list start + \s*.+?\s*,\s* # match count argument + (' . LOCALE_JS_STRING . ')\s*,\s* # match singular string argument + ( # capture plural string argument + (?: # non-capturing group to repeat string pieces + (?: + \' # match start of single-quoted string + (?:\\\\\'|[^\'])* # match any character except unescaped single-quote + @count # match "@count" + (?:\\\\\'|[^\'])* # match any character except unescaped single-quote + \' # match end of single-quoted string + | + " # match start of double-quoted string + (?:\\\\"|[^"])* # match any character except unescaped double-quote + @count # match "@count" + (?:\\\\"|[^"])* # match any character except unescaped double-quote + " # match end of double-quoted string + ) + (?:\s*\+\s*)? # match "+" with possible whitespace, for str concat + )+ # match multiple because we supports concatenating strs + )\s* # end capturing of plural string argument + [,\)] + ~sx', $file, $plural_matches); + // Loop through all matches and process them. $all_matches = array_merge($plural_matches[1], $t_matches[1]); diff --git a/modules/locale/locale.test b/modules/locale/locale.test index 90e313d..99a0994 100644 --- a/modules/locale/locale.test +++ b/modules/locale/locale.test @@ -180,6 +180,113 @@ class LocaleConfigurationTest extends DrupalWebTestCase { } /** + * Functional tests for JavaScript parsing for translatable strings. + */ +class LocaleJavascriptTranslationTest extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'Javascript translation', + 'description' => 'Tests parsing js files for translatable strings', + 'group' => 'Locale', + ); + } + + function setUp() { + parent::setUp('locale'); + } + + function testFileParsing() { + + // Create a js file with calls the Drupal.t and Drupal.formatPlural. + $filename = 'public://locale.test.js'; + $contents = <<fields('s', array('source', 'lid')) + ->condition('s.location', $filename) + ->execute() + ->fetchAllKeyed(); + + // List of all strings that should be in the file. + $test_strings = array( + "Standard Call t", + "Whitespace Call t", + + "Single Quote t", + "Single Quote \\'Escaped\\' t", + "Single Quote Concat strings t", + + "Double Quote t", + "Double Quote \\\"Escaped\\\" t", + "Double Quote Concat strings t", + + "Standard Call plural", + "Standard Call @count plural", + "Whitespace Call plural", + "Whitespace Call @count plural", + + "Single Quote plural", + "Single Quote @count plural", + "Single Quote \\'Escaped\\' plural", + "Single Quote \\'Escaped\\' @count plural", + + "Double Quote plural", + "Double Quote @count plural", + "Double Quote \\\"Escaped\\\" plural", + "Double Quote \\\"Escaped\\\" @count plural", + ); + + // Assert that all strings were found properly. + foreach ($test_strings as $str) { + $this->assertTrue(isset($source_strings[$str]), t("Found source string: %source", array('%source' => $str))); + } + } +} +/** * Functional test for string translation and validation. */ class LocaleTranslationFunctionalTest extends DrupalWebTestCase { -- 1.7.0.4