I use a function __()
to translate string, and I added an interface to automatically find all theses translation in all files. This is (supposed to be) done with the following regex:
<?php
$pattern = <<<'LOD'
`
__\(
(?<quote> # GET THE QUOTE
(?<simplequote>') # catch the opening simple quote
|
(?<doublequote>") # catch the opening double quote
)
(?<param1> # the string will be saved in param1
(?(?=\k{simplequote}) # if condition "simplequote" is ok
(\\'|"|[^'"])+ # allow escaped simple quotes or anything else
| #
(\\"|'|[^'"])+ # allow escaped double quotes or anything else
)
)
\k{quote} # find the closing quote
(?:,.*){0,1} # catch any type of 2nd parameter
\)
# modifiers:
# x to allow comments :)
# m for multiline,
# s for dotall
# U for ungreedy
`smUx
LOD;
$files = array('/path/to/file1',);
foreach($files as $filepath)
{
$content = file_get_contents($filepath);
if (preg_match_all($pattern, $content, $matches))
{
foreach($matches['param1'] as $found)
{
// do things
}
}
}
that regex does not works for some string double quoted containing an escaped simple quote (\'
). It seems in fact, whatever the string is simple or double quoted, the condition is considered as false and so the "else" is always used.
<?php
// content of '/path/to/file1'
echo __('simple quoted: I don\'t "see" what is wrong'); // do not work.
echo __("double quoted: I don't \"see\" what is wrong");// works.
for file1, I expect to have both strings found, but only the double quoted works
Edit added more php code to make it easier to test