daily pastebin goal
6%
SHARE
TWEET

search and replace php for gettext strings

a guest Jan 17th, 2011 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $po = file_get_contents("locale/en_GB/LC_MESSAGES/messages.po");
  4.  
  5. $translations = array(); // german => english
  6. preg_match_all('/msgid "(.+)"\nmsgstr "(.+)"/', $po, $matches, PREG_SET_ORDER);
  7. foreach ($matches as $match) {
  8.         $match[1] = str_replace('\"','"',$match[1]);
  9.         $match[2] = str_replace('\"','"',$match[2]);
  10.        
  11.     $translations['_(\''. $match[1] . '\''] = '_(\'' . $match[2] . '\'';
  12.         $translations['_("'. $match[1] . '"'] = '_("' . $match[2] . '"';
  13.     $translations['__(\''. $match[1] . '\''] = '__(\'' . $match[2] . '\'';
  14.     $translations['__("'. $match[1] . '"'] = '__("' . $match[2] . '"';
  15. }
  16.  
  17. $msgidhits = array();
  18.  
  19. foreach (glob("*.php") as $file) {
  20.     $code = file_get_contents($file);
  21.         foreach($translations AS $msgid => $msgstr) {
  22.                 $hits = 0;
  23.                 $code = str_replace($msgid,$msgstr,$code,$hits);
  24.                
  25.                 $msgid = str_replace('"',"'",$msgid);
  26.                 $msgid = str_replace('__(',"_(",$msgid);
  27.                 if(!isset($msgidhits[$msgid])) $msgidhits[$msgid] = $hits;
  28.                 else $msgidhits[$msgid] += $hits;
  29.         }
  30.     $code = str_replace(array_keys($translations), array_values($translations), $code);
  31.     //file_put_contents($file, $code);
  32.     echo $code; // be careful to test this first before doing the actual replace (and do use a version control system!)
  33. }
  34.  
  35. print_r($msgidhits);
  36.  
  37. echo "nulldrin?";
  38. print_r(array_keys($msgidhits, 0, true));
  39.  
  40. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top