Skip to content

Instantly share code, notes, and snippets.

@sergeylukin
Created November 12, 2018 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergeylukin/79671e8521f131db30c90df06d230cb4 to your computer and use it in GitHub Desktop.
Save sergeylukin/79671e8521f131db30c90df06d230cb4 to your computer and use it in GitHub Desktop.
<?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