Skip to content

Instantly share code, notes, and snippets.

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 christophervalles/2575948 to your computer and use it in GitHub Desktop.
Save christophervalles/2575948 to your computer and use it in GitHub Desktop.
Update i18n strings on the source code based on the changes made on a *.po file (Fix wrong sentences, change text, typos, etc). If you developed an app and some sentences on the base language are wrong or must be changed/fixed just create a .po file with
<?php
system('clear');
ini_set('auto_detect_line_endings', TRUE);
$fin = fopen('en.po', 'r');
$change = 0;
while(!feof($fin)){
$line = trim(fgets($fin));
$files = array();
while(!feof($fin) && (substr($line, 0, 2) == '#:' || substr($line, 0, 2) == '#,' || substr($line, 0, 1) == '"' || $line == 'msgid ""' || $line == 'msgstr ""' || empty($line))){
if(substr($line, 0, 2) == '#:'){
preg_match('/#:\s(.+):(\d+)/', $line, $matches);
$files[] = array('file' => $matches[1], 'line' => $matches[2]);
}
$line = trim(fgets($fin));
}
if(!feof($fin)){
preg_match('/"(.*)"/', $line, $matches);
$str1 = trim($matches[1]);
$line = trim(fgets($fin));
preg_match('/"(.*)"/', $line, $matches);
$str2 = trim($matches[1]);
if(md5($str1) != md5($str2) && !empty($str2)){
foreach($files as $f){
$change++;
$lines = file('YOUR PROJECT PATH/' . $f['file']);
echo str_repeat('=', 150) . PHP_EOL;
echo sprintf('Change #%d', $change) . PHP_EOL . PHP_EOL;
echo sprintf('Modifying file: %s - Line %d', $f['file'], $f['line']) . PHP_EOL;
echo sprintf('Replacing "%s" with "%s"', $str1, $str2) . PHP_EOL;
echo sprintf("Original line: %s", str_replace($str1, sprintf("\033[41;97m%s\033[0m", $str1), trim($lines[$f['line'] - 1]))) . PHP_EOL;
$lines[$f['line'] - 1] = str_replace($str1, $str2, $lines[$f['line'] - 1]);
echo sprintf('Modified line: %s', str_replace($str2, sprintf("\033[41;97m%s\033[0m", $str2), trim($lines[$f['line'] - 1]))) . PHP_EOL . PHP_EOL;
echo 'Do you want to perform the change? [Y/n]: ';
$stdin = fopen ('php://stdin', 'r');
$line = fgets($stdin);
if(trim($line) == 'Y' || trim($line) == 'y' || trim($line) == ''){
file_put_contents('/Users/christophervalles/projects/familiafacil-frontend/' . $f['file'], implode('', $lines));
echo sprintf('File %s modified', $f['file']) . PHP_EOL;
}
echo PHP_EOL;
}
}
}
}
fclose($fin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment