public function load($locale, $locale_path, $domain, $textdomain = true)
 {
     $file = $locale_path . '/' . $locale . '/LC_MESSAGES/' . $domain . '.po';
     $cache_file = waSystem::getInstance()->getConfig()->getPath('cache') . '/apps/' . $domain . '/locale/' . $locale . '.php';
     if (isset(self::$cache[$locale][$domain])) {
     } elseif (!file_exists($file)) {
         self::$cache[$locale][$domain] = array();
     } elseif (file_exists($cache_file) && filemtime($cache_file) > filemtime($file)) {
         self::$cache[$locale][$domain] = (include $cache_file);
     } else {
         if (file_exists($file)) {
             $gettext = new waGettext($file);
             self::$cache[$locale][$domain] = $gettext->read();
         } else {
             self::$cache[$locale][$domain] = array();
         }
         waFiles::create($cache_file);
         waUtils::varExportToFile(self::$cache[$locale][$domain], $cache_file);
     }
     if (isset(self::$cache[$locale][$domain]['meta']['Plural-Forms']['plural']) && self::$cache[$locale][$domain]['meta']['Plural-Forms']['plural']) {
         self::$cache[$locale][$domain]['meta']['f'] = create_function('$n', self::$cache[$locale][$domain]['meta']['Plural-Forms']['plural']);
     }
     if ($textdomain) {
         self::$domain = $domain;
         self::$locale = $locale;
     }
     if (!self::$locale) {
         self::$locale = $locale;
     }
 }
 public function save()
 {
     $htaccess_path = $this->config['path'] . "/.htaccess";
     if (!file_exists($htaccess_path)) {
         if (!file_exists($this->config['path'])) {
             mkdir($this->config['path'], 0777, true);
         }
         file_put_contents($htaccess_path, "Deny from all\n");
     }
     foreach ($this->config['locales'] as $locale => $domain) {
         $locale_path = $this->config['path'] . "/" . $locale . "/" . "LC_MESSAGES" . "/" . $domain . ".po";
         if ($this->config['verify']) {
             $locale_path_log = $locale_path . '.log';
             if (file_exists($locale_path)) {
                 if ($fh = fopen($locale_path_log, "w")) {
                     $counter = 0;
                     flock($fh, LOCK_EX);
                     $gettext = new waGettext($locale_path, true);
                     $strings = $gettext->read();
                     $strings = $strings['messages'];
                     $words = $this->words;
                     foreach ($strings as $msg_id => $info) {
                         if (!isset($this->words[$msg_id])) {
                             fputs($fh, "msgid \"" . str_replace('"', '\\"', $msg_id) . "\"\n");
                             ++$counter;
                         } else {
                             unset($words[$msg_id]);
                         }
                     }
                     if ($counter) {
                         echo "\r\n{$counter} string(s) is out of date for {$locale} at {$domain}\r\n";
                     }
                     if ($words) {
                         fputs($fh, "\n\n#Missed:\n\n\n");
                         foreach ($words as $msg_id => $info) {
                             if (isset($strings[$msg_id])) {
                                 unset($words[$msg_id]);
                                 continue;
                             }
                             fputs($fh, "msgid \"" . str_replace('"', '\\"', $msg_id) . "\"\n");
                         }
                         $counter = count($words);
                         echo "\r\n{$counter} string(s) is missed or not translated for {$locale} at {$domain}\r\n";
                     }
                     fflush($fh);
                     flock($fh, LOCK_UN);
                     fclose($fh);
                 } else {
                     echo "\r\nError while open {$locale_path} in a+ mode\r\n";
                 }
             } else {
                 echo "\r\\Locale file {$locale_path} is missed\r\n";
             }
         } else {
             if (!file_exists($locale_path)) {
                 $this->create($locale);
                 $strings = array();
             } else {
                 $gettext = new waGettext($locale_path, true);
                 $strings = $gettext->read();
                 $strings = $strings['messages'];
             }
             $counter = 0;
             if ($fh = fopen($locale_path, "a+")) {
                 if ($this->config['debug']) {
                     echo "\r\n" . $locale_path . " - " . count($this->words) . " records\r\n";
                 }
                 flock($fh, LOCK_EX);
                 foreach ($this->words as $msg_id => $info) {
                     // Ищем вхождения текущей фразы
                     if (isset($strings[$msg_id])) {
                         continue;
                     }
                     // Если не нашли - записываем
                     // Если не нашли - записываем
                     foreach ((array) $info['lines'] as $line) {
                         fputs($fh, "\n#: " . $line);
                     }
                     fputs($fh, "\nmsgid \"" . str_replace('"', '\\"', $msg_id) . "\"\n");
                     if (!empty($info['plural'])) {
                         fputs($fh, "msgid_plural \"" . str_replace('"', '\\"', $info['plural']) . "\"\n");
                         $n = $locale == 'ru_RU' ? 3 : 2;
                         for ($i = 0; $i < $n; $i++) {
                             fputs($fh, "msgstr[{$i}] \"\"\n");
                         }
                     } else {
                         fputs($fh, "msgstr \"\"\n");
                     }
                     ++$counter;
                 }
                 fflush($fh);
                 flock($fh, LOCK_UN);
                 fclose($fh);
             } else {
                 echo "\r\nError while open {$locale_path} in a+ mode\r\n";
             }
             if ($counter) {
                 echo "\r\nAdded {$counter} string(s) for locale {$locale} at {$domain}\r\n";
             }
         }
     }
 }
Beispiel #3
0
 public function save()
 {
     foreach ($this->config['locales'] as $locale => $domain) {
         $locale_path = $this->config['path'] . "/" . $locale . "/" . "LC_MESSAGES" . "/" . $domain . ".po";
         if ($this->config['verify']) {
             $locale_path_log = $locale_path . '.log';
             if (file_exists($locale_path)) {
                 if ($fh = fopen($locale_path_log, "w")) {
                     $counter = 0;
                     flock($fh, LOCK_EX);
                     $gettext = new waGettext($locale_path);
                     $strings = $gettext->read();
                     foreach ($strings['messages'] as $msg_id => $info) {
                         if (!isset($this->words[$msg_id])) {
                             fputs($fh, $msg_id . "\n");
                             ++$counter;
                         }
                     }
                     flock($fh, LOCK_UN);
                     fclose($fh);
                     if ($counter) {
                         echo "\r\n{$counter} string(s) is out of date for {$locale} at {$domain}\r\n";
                     }
                 } else {
                     echo "\r\nError while open {$locale_path} in a+ mode\r\n";
                 }
             } else {
                 echo "\r\\Locale file {$locale_path} is missed\r\n";
             }
         } else {
             if (!file_exists($locale_path)) {
                 $this->create($locale);
                 $strings = array();
             } else {
                 $gettext = new waGettext($locale_path, true);
                 $strings = $gettext->read();
                 $strings = $strings['messages'];
             }
             $counter = 0;
             if ($fh = fopen($locale_path, "a+")) {
                 if ($this->config['debug']) {
                     echo "\r\n" . $locale_path . " - " . count($this->words) . " records\r\n";
                 }
                 flock($fh, LOCK_EX);
                 foreach ($this->words as $words => $lines) {
                     // Ищем вхождения текущей фразы
                     if (isset($strings[stripslashes($words)])) {
                         continue;
                     }
                     // Если не нашли - записываем
                     fputs($fh, "\n#: " . $lines . "\n");
                     fputs($fh, "msgid \"" . $words . "\"\n");
                     fputs($fh, "msgstr \"\"\n");
                     ++$counter;
                 }
                 flock($fh, LOCK_UN);
                 fclose($fh);
             } else {
                 echo "\r\nError while open {$locale_path} in a+ mode\r\n";
             }
             if ($counter) {
                 echo "\r\n{$counter} string(s) for locale {$locale} at {$domain}\r\n";
             }
         }
     }
 }