Created
November 12, 2018 08:15
-
-
Save sergeylukin/79671e8521f131db30c90df06d230cb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('MY_APP_DIR', __DIR__ . '/src'); | |
function rsearch($folder, $pattern) { | |
$dir = new RecursiveDirectoryIterator($folder); | |
$ite = new RecursiveIteratorIterator($dir); | |
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH); | |
$fileList = array(); | |
foreach($files as $file) { | |
$fileList = array_merge($fileList, $file); | |
} | |
return $fileList; | |
} | |
$pot = ""; | |
$msgids = []; | |
$cnt = 0; | |
foreach (rsearch(MY_APP_DIR, '/^.*\.phtml$/') as $filePath) { | |
if (strpos($filePath, 'admin')) continue; | |
$contents = file_get_contents($filePath); | |
$matches = null; | |
if (preg_match_all("/>(?=.*[a-zA-Z])(['.,\d\w\s\n\r]+)</im", $contents, $matches)) { | |
foreach ($matches[0] as $msgid) { | |
$msgid = str_replace('<', '', $msgid); | |
$msgid = str_replace('>', '', $msgid); | |
$contents = str_replace( $msgid, "<?= _(\"{$msgid}\"); ?>", $contents ); | |
file_put_contents($filePath, $contents); | |
if (isset($msgids[$msgid])) continue; | |
$cnt++; | |
$msgids[$msgid] = 1; | |
$pot .= "msgid \"{$msgid}\"\n"; | |
$pot .= "msgstr \"\"\n\n"; | |
} | |
} | |
} | |
file_put_contents(__DIR__ . '/short_messages.po', $pot); | |
echo "In total {$cnt} messages"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment