PHP autodetect translatables / detect piece of code by regex -
i'm having multilanguage site stores translatables within default.php filled array contains keys.
i prefer make automatic. have (singleton) class able detect files based type. (controller, action, view, model, etc...)
i detect piece of code of format this:
$this->translate('[a-za-z]'); $view->translate('[a-za-z]'); getview()->translate('[a-za-z]'); throw new exception('[a-za-z]'); addmessage(array('message' => '[a-za-z]');
however must filtered when starts with/contains:
$this->translate('((0-9)+_)[a-za-z]'); $this->translate('[a-za-z]' . $* . '[a-za-z]'); // variable in middle must filtered, begin or end still allowed
ofcourse [a-za-z] regex example.
like sais have class detect files. class make use of reflection (or in case zend reflection, i'm using zend) not see way reflect function using regex.
the action placed within cronjob , manual called action not big issue when used memory bit 'too' large.
description
[$]this->translate[(]'((?:[^'\\]|\\.|'')*)'[)];
** see image better, right click image , select view in new window
this regular expression following:
- code blocks starting
$this-translate('
through it's closing');
- places value inside
'
quotes capture group 1 - avoids messy edge cases in substring may contain looks end
');
string when in reality characters escaped.
example
live demo
https://regex101.com/r/ec5xq6/
sample text
$this->translate('(?:droids\');{2}'); $nottranalate('fdasad'); $this->translate('[a-za-z]');
sample matches
match 1 1. [17-33] `(?:droids\');{2}` match 2 1. [79-87] `[a-za-z]`
explanation
node explanation ---------------------------------------------------------------------- (?-imsx: group, not capture (case-sensitive) (with ^ , $ matching normally) (with . not matching \n) (matching whitespace , # normally): ---------------------------------------------------------------------- [$] character of: '$' ---------------------------------------------------------------------- this->translate 'this->translate' ---------------------------------------------------------------------- [(] character of: '(' ---------------------------------------------------------------------- ' '\'' ---------------------------------------------------------------------- ( group , capture \1: ---------------------------------------------------------------------- (?: group, not capture (0 or more times (matching amount possible)): ---------------------------------------------------------------------- [^'\\] character except: ''', '\\' ---------------------------------------------------------------------- | or ---------------------------------------------------------------------- \\ '\' ---------------------------------------------------------------------- . character except \n ---------------------------------------------------------------------- | or ---------------------------------------------------------------------- '' '\'\'' ---------------------------------------------------------------------- )* end of grouping ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ' '\'' ---------------------------------------------------------------------- [)] character of: ')' ---------------------------------------------------------------------- ; ';' ----------------------------------------------------------------------
Comments
Post a Comment