diff --git a/potx.inc b/potx.inc index 94af04a..5d98b11 100644 --- a/potx.inc +++ b/potx.inc @@ -212,9 +212,9 @@ function _potx_process_file($file_path, $strip_prefix = 0, $save_callback = '_po _potx_parse_twig_file($code, $file_name, $save_callback); } - $constraint_extract = false; + $constraint_extract = FALSE; if (substr($name_parts['filename'], -10) == 'Constraint' && $api_version > POTX_API_7) { - $constraint_extract = true; + $constraint_extract = TRUE; } // Extract raw PHP language tokens. @@ -238,7 +238,7 @@ function _potx_process_file($file_path, $strip_prefix = 0, $save_callback = '_po if (in_array($token[0], array(T_STRING, T_DOC_COMMENT)) || ($token[0] == T_VARIABLE && $token[1] == '$t') || $constraint_match) { // Give doc comments a specific key because their content varies. - $key = ($token[0] == T_DOC_COMMENT) ? 'T_DOC_COMMENT' : ($constraint_match ? 'T_CONSTRAINT' : $token[1]); + $key = ($token[0] == T_DOC_COMMENT) ? 'T_DOC_COMMENT' : ($constraint_match ? 'T_POTX_CONSTRAINT' : $token[1]); if (!isset($_potx_lookup[$key])) { $_potx_lookup[$key] = array(); } @@ -1764,10 +1764,26 @@ function _potx_find_routing_yml_file_strings($code, $file_name, $save_callback, } } + +/** + * Detect validation constraint messages. Drupal 8+ + * + * This sequences is searched for: + * T_POTX_CONSTRAINT = T_CONSTANT_ENCAPSED_STRING + * + * note: T_POTX_CONSTRAINT is marked for T_VARIABLE tokens inside .php files + * with the "Constraint" suffix, where the token is "$message", or ends + * with "Message" + * + * @param $file_name + * Name of file parsed. + * @param $save_callback + * Callback function used to save strings. + */ function _potx_find_constraint_messages($file_name, $save_callback) { global $_potx_tokens, $_potx_lookup; - foreach ($_potx_lookup['T_CONSTRAINT'] as $key => $ti) { + foreach ($_potx_lookup['T_POTX_CONSTRAINT'] as $key => $ti) { if ($_potx_tokens[$ti + 1] == '=' && $_potx_tokens[$ti + 2][0] == T_CONSTANT_ENCAPSED_STRING) { $str = $_potx_tokens[$ti + 2][1]; diff --git a/tests/TestConstraint.php b/tests/TestConstraint.php new file mode 100644 index 0000000..8b93b74 --- /dev/null +++ b/tests/TestConstraint.php @@ -0,0 +1,8 @@ +assertNoMsgID('mapping'); $this->assertNoMsgID('sequence'); } + + /** + * Test parsing Drupal 8 validation constraint messages. + */ + public function testDrupal8ConstraintMessages() { + $filename = drupal_get_path('module', 'potx') .'/tests/TestConstraint.php'; + $this->parseFile($filename, POTX_API_8); + + $this->assertMsgID('Test message'); + $this->assertMsgID('Test message 2'); + $this->assertPluralID('1 test message', '@count test message'); + $this->assertNoMsgID('Not a message for translation'); + } /** * Test parsing of Drupal 6 info file. Drupal 5 and 7 have no other rules.