I18n::loadPo PHP Method

loadPo() public static method

Loads the text .po file and returns array of translations
public static loadPo ( string $filename ) : mixed
$filename string Text .po file to load
return mixed Array of translations on success or false on failure
    public static function loadPo($filename)
    {
        if (!($file = fopen($filename, 'r'))) {
            return false;
        }
        $type = 0;
        $translations = array();
        $translationKey = '';
        $translationContext = null;
        $plural = 0;
        $header = '';
        do {
            $line = trim(fgets($file));
            if ($line === '' || $line[0] === '#') {
                $translationContext = null;
                continue;
            }
            if (preg_match("/msgid[[:space:]]+\"(.+)\"\$/i", $line, $regs)) {
                $type = 1;
                $translationKey = stripcslashes($regs[1]);
            } elseif (preg_match("/msgid[[:space:]]+\"\"\$/i", $line, $regs)) {
                $type = 2;
                $translationKey = '';
            } elseif (preg_match("/msgctxt[[:space:]]+\"(.+)\"\$/i", $line, $regs)) {
                $translationContext = $regs[1];
            } elseif (preg_match("/^\"(.*)\"\$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
                $type = 3;
                $translationKey .= stripcslashes($regs[1]);
            } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"\$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
                $translations[$translationKey][$translationContext] = stripcslashes($regs[1]);
                $type = 4;
            } elseif (preg_match("/msgstr[[:space:]]+\"\"\$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
                $type = 4;
                $translations[$translationKey][$translationContext] = '';
            } elseif (preg_match("/^\"(.*)\"\$/i", $line, $regs) && $type == 4 && $translationKey) {
                $translations[$translationKey][$translationContext] .= stripcslashes($regs[1]);
            } elseif (preg_match("/msgid_plural[[:space:]]+\".*\"\$/i", $line, $regs)) {
                $type = 6;
            } elseif (preg_match("/^\"(.*)\"\$/i", $line, $regs) && $type == 6 && $translationKey) {
                $type = 6;
            } elseif (preg_match("/msgstr\\[(\\d+)\\][[:space:]]+\"(.+)\"\$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
                $plural = $regs[1];
                $translations[$translationKey][$translationContext][$plural] = stripcslashes($regs[2]);
                $type = 7;
            } elseif (preg_match("/msgstr\\[(\\d+)\\][[:space:]]+\"\"\$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
                $plural = $regs[1];
                $translations[$translationKey][$translationContext][$plural] = '';
                $type = 7;
            } elseif (preg_match("/^\"(.*)\"\$/i", $line, $regs) && $type == 7 && $translationKey) {
                $translations[$translationKey][$translationContext][$plural] .= stripcslashes($regs[1]);
            } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"\$/i", $line, $regs) && $type == 2 && !$translationKey) {
                $header .= stripcslashes($regs[1]);
                $type = 5;
            } elseif (preg_match("/msgstr[[:space:]]+\"\"\$/i", $line, $regs) && !$translationKey) {
                $header = '';
                $type = 5;
            } elseif (preg_match("/^\"(.*)\"\$/i", $line, $regs) && $type == 5) {
                $header .= stripcslashes($regs[1]);
            } else {
                unset($translations[$translationKey][$translationContext]);
                $type = 0;
                $translationKey = '';
                $translationContext = null;
                $plural = 0;
            }
        } while (!feof($file));
        fclose($file);
        $merge[''] = $header;
        return array_merge($merge, $translations);
    }

Usage Example

 public function importPOFile($lang = 'ara')
 {
     $this->autoRender = false;
     $localDir = App::path('locales');
     $localDir = $localDir[0];
     $localeImportFile = $localDir . $this->convertLangCodeFromCoreToSchool($lang) . DS . 'LC_MESSAGES' . DS . 'update.po';
     if (is_file($localeImportFile)) {
         //echo '<meta content="text/html; charset=utf-8" http-equiv="Content-Type">';
         $translations = I18n::loadPo($localeImportFile);
         $saveData = array();
         $counter = 0;
         foreach ($translations as $tKey => $tValue) {
             if (!empty($tKey)) {
                 $saveData[$counter]['eng'] = $tKey;
                 $saveData[$counter][$lang] = $tValue;
                 $counter++;
             }
         }
         $data = $this->Translation->find('all');
         if (!empty($data)) {
             foreach ($saveData as $tKey => $tValue) {
                 $conditions = array('Translation.eng' => $tValue['eng']);
                 if ($this->Translation->hasAny($conditions)) {
                     $this->Translation->recursive = -1;
                     $transData = $this->Translation->findByEng($tValue['eng'], array('id'));
                     $saveData[$tKey]['id'] = $transData['Translation']['id'];
                 }
             }
         }
         if ($this->Translation->saveAll($saveData)) {
             return $this->redirect(array('action' => 'index', $lang));
         }
     }
 }
All Usage Examples Of I18n::loadPo