function gtTranslateGlade($dialog) { global $config; $langFile = "i18n/glade-source/{$config['language']}.{$dialog}.po"; $gladeFile = "resources/{$dialog}.glade"; $outFile = "resources/{$config['language']}.{$dialog}.glade"; if (!file_exists($langFile)) { return $gladeFile; } $poFile = new File_Gettext_PO(); $poFile->load($langFile); $poStrings = $poFile->strings; $find = []; $replace = []; foreach ($poStrings as $in => $out) { if ($out) { $find[] = '/<property(.*)translatable="yes"(.*)>' . preg_quote($in, '/') . '(<\\/property>)/'; $replace[] = '<property\\1\\2>' . preg_quote($out, '/') . '\\3'; } } $result = preg_replace($find, $replace, file_get_contents($gladeFile)); file_put_contents($outFile, $result); return $outFile; }
/** * gets the translation as array form a language * @param string $language * @return array */ public function get_translation($language) { $mo_translation = i18n::get_language_path($language); if (!file_exists($mo_translation)) { Alert::set(Alert::ERROR, $language); $this->redirect(Route::url('oc-panel', array('controller' => 'translations'))); } $base_translation = i18n::get_language_path(); //pear gettext scripts require_once Kohana::find_file('vendor', 'GT/Gettext', 'php'); require_once Kohana::find_file('vendor', 'GT/Gettext/PO', 'php'); require_once Kohana::find_file('vendor', 'GT/Gettext/MO', 'php'); //load the .po files //original en translation $pocreator_en = new File_Gettext_PO(); $pocreator_en->load($base_translation); //the translation file $pocreator_translated = new File_Gettext_PO(); $pocreator_translated->load($mo_translation); //get an array with all the strings $en_array_order = $pocreator_en->strings; //sort alphabetical using locale ksort($en_array_order, SORT_LOCALE_STRING); //array with translated language may contain missing from EN $origin_translation = $pocreator_translated->strings; //lets get the array with translated values and sorted, will include everything even if was not previously saved $translation_array = array(); $untranslated_array = array(); //keep track of words not translated stores ID $i = 0; foreach ($en_array_order as $origin => $value) { //do we have the translation? if (isset($origin_translation[$origin]) and !empty($origin_translation[$origin]) > 0) { $translated = $origin_translation[$origin]; } else { $untranslated_array[] = $i; $translated = ''; } $translation_array[] = array('id' => $i, 'original' => $origin, 'translated' => $translated); $i++; } return array($translation_array, $untranslated_array); }
/** * poFile2moFile * * That's a simple fake of the 'msgfmt' console command. It reads the * contents of a GNU PO file and saves them to a GNU MO file. * * @param string $pofile path to GNU PO file * @param string $mofile path to GNU MO file * * @static * @access public * @return mixed Returns true on success or PEAR_Error on failure. */ function poFile2moFile($pofile, $mofile) { if (!is_file($pofile)) { return File_Gettext::raiseError("File {$pofile} doesn't exist."); } include_once 'File/Gettext/PO.php'; $PO = new File_Gettext_PO($pofile); if (true !== ($e = $PO->load())) { return $e; } $MO =& $PO->toMO(); if (true !== ($e = $MO->save($mofile))) { return $e; } unset($PO, $MO); return true; }
mkdir(dirname($destDir . $filepath), 0777, true); } file_put_contents($destDir . $filepath, $in); } $sources = scandir($sourcesDir, 1); $sources2 = scandir($sourcesDir . '/classes/', 1); foreach ($sources2 as $classdir) { $sources[] = '/classes/' . $classdir; } foreach ($sources as $id => $filename) { if (preg_match('/^.*\\.(php)$/i', $filename)) { echo $id . ' z ' . count($sources) . ' souborů ' . "\n"; exchLocStrings($sourcesDir . '/' . $filename, $gt, $destDir); } } //exchLocStrings('/home/vitex/Projects/VitexSoftware/iciedit/src/index.php'); $gt->strings = array_flip($gt->strings); if (!file_exists($destDir . 'locales/cs_CZ/LC_MESSAGES/')) { mkdir($destDir . 'locales/cs_CZ/LC_MESSAGES/', 0777, true); } $gt->save($destDir . 'locales/cs_CZ/LC_MESSAGES/messages.po2'); $gt2 = new File_Gettext_PO(); $gt2->load($localesDir . '/en_US/LC_MESSAGES/messages.po'); $gt2->strings = array(); foreach ($gt->strings as $strId => $string) { $gt2->strings[$strId] = $strId; } if (!file_exists($destDir . 'locales/en_US/LC_MESSAGES/')) { mkdir($destDir . 'locales/en_US/LC_MESSAGES/', 0777, true); } $gt->save($destDir . 'locales/en_US/LC_MESSAGES/messages.po');
protected function runLanguage($lang) { // Find all source files (english) $files = array('core', 'dataTypes'); // Create an empty array for all texts $stringKeys = array(); // Go through all files foreach ($files as $file) { // Load XML $xml = new SimpleXMLElement(file_get_contents('messages/en/' . $file . '.xml')); foreach ($xml->entry as $entry) { $stringKeys[] = (string) $entry; } } // // This is a quick dirty hack because Gettext_PDO does can't handle escaped field delimiters. // So i replace all espaced delimiters with -!!- and reverse it afterwards. // $old = file_get_contents('../../translation/protected/messages/_translation/' . $lang . '.po'); $replaced = str_replace('\\"', '-!!-', $old); file_put_contents('../../translation/protected/messages/_translation/' . $lang . '.po', $replaced); // Now try to load translations require_once 'File/Gettext/PO.php'; $test = new File_Gettext_PO('../../translation/protected/messages/_translation/' . $lang . '.po'); $test->load(); $data = $test->toArray(); $strings = array(); file_put_contents('../../translation/protected/messages/_translation/' . $lang . '.po', $old); foreach ($data['strings'] as $key => $value) { $key = str_replace('-!!-', '"', $key); $value = str_replace('-!!-', '"', $value); $pos = array_search($key, $stringKeys); if ($pos > -1) { array_splice($stringKeys, $pos, 1); $strings[$key] = $value; } } if (count($stringKeys) > 0) { echo $lang . ' skipped, missing ' . count($stringKeys) . ' terms' . "\n"; foreach ($stringKeys as $key) { echo " " . $key . "\n"; } } else { echo $lang . ' imported' . "\n"; } if (strlen($lang) == 2) { $targetLang = $lang . '_' . $lang; } else { $targetLang = $lang; } $finishedTranslated = array("pt_br", "en_gb", "fr_fr", "it_it", "ru_ru", "uk_uk", "pl_pl", "es_es", "hu_hu", "ro_ro", "nl_nl", "sw_sw"); if (!in_array($targetLang, $finishedTranslated)) { return; } @mkdir('messages/' . strtolower($targetLang)); // Translate foreach ($files as $file) { $xml = simplexml_load_file('messages/en/' . $file . '.xml'); $xmlNew = new SimpleXMLElement('<category id="' . $xml['id'] . '" />'); $dom = new DOMDocument("1.0", "utf-8"); $dom->formatOutput = true; $category = $dom->createElement("category", null); $category->setAttribute("id", $xml['id']); $dom->appendChild($category); foreach ($xml->entry as $entry) { if (isset($strings[(string) $entry])) { $child = $dom->createElement("entry", $strings[(string) $entry]); $child->setAttribute("id", $entry['id']); $category->appendChild($child); } } /** * Reload into new DOM because otherwise formatOutput does not work (whyever ? ..) */ $content = $dom->saveXml(); $dom = new DOMDocument("1.0", "utf-8"); $dom->loadXML($content); $dom->formatOutput = true; var_dump(file_put_contents('messages/' . strtolower($targetLang) . '/' . $file . '.xml', $dom->saveXML())); } }
function macro_MsgTrans($formatter, $value, $param = array()) { global $DBInfo; $user =& $DBInfo->user; if (empty($param['init']) and (!is_array($DBInfo->owners) or !in_array($user->id, $DBInfo->owners))) { return sprintf(_("You are not allowed to \"%s\" !"), "msgtrans"); } if (!$pagename) { $pagename = $DBInfo->default_translation ? $DBInfo->default_translation : 'LocalTranslationKo'; } $page = $DBInfo->getPage($pagename); $strs = array(); $charset = strtoupper($DBInfo->charset); $lang = !empty($value) ? $value : ($DBInfo->lang ? $DBInfo->lang : 'en_US.' . $charset); if ($page->exists()) { $raw = $page->get_raw_body(); $raw = rtrim($raw); $lines = explode("\n", $raw); foreach ($lines as $l) { $l = trim($l); if ($l[0] == '#') { if (preg_match('/^#lang(?' . '>uage)? (ko_KR|en_US|fr_FR)$/', $l, $m)) { $lang = $m[1]; if ($DBInfo->charset) { $lang .= '.' . $charset; } } continue; } if ($l[0] == '"') { if (preg_match('/^(("(([^"]|\\\\")*?)"\\s*)+)\\s*(.*)$/', $l, $m)) { $smap = array('/"\\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\\"/'); $rmap = array('', "\n", "\r", "\t", '"'); $w = preg_replace($smap, $rmap, $m[3]); $t = preg_replace($smap, $rmap, $m[5]); } } else { list($w, $t) = explode(" ", $l, 2); } $strs[$w] = $t; } } if (getenv("OS") == "Windows_NT") { $lang = substr($lang, 0, 2); } //print_r($strs); if (!empty($strs) or !empty($param['init'])) { $myMO = null; $ldir = 'locale/' . $lang . '/LC_MESSAGES'; $mofile = $ldir . '/moniwiki.mo'; if (!file_exists($mofile)) { # load *.po file $e = false; $mylang = substr($lang, 0, 2); $pofile = 'locale/po/' . $mylang . '.po'; if (!file_exists($pofile)) { $pofile = dirname(__FILE__) . '/../locale/po/' . $mylang . '.po'; } if (file_exists($pofile)) { include_once 'lib/Gettext/PO.php'; $myPO = new File_Gettext_PO(); if (($e = $myPO->load($pofile)) == true) { $myMO = $myPO->toMO(); preg_match('/charset=(.*)$/', $myMO->meta['Content-Type'], $cs); if (strtoupper($cs[1]) != $charset) { if (function_exists("iconv")) { $myMO->meta['Content-Type'] = 'text/plain; charset=' . $charset; foreach ($myMO->strings as $k => $v) { $nv = iconv($cs[1], $charset, $v); if (isset($nv)) { $myMO->strings[$k] = $nv; } } } else { $e = false; } } } } } else { # load *.mo file include_once 'lib/Gettext/MO.php'; $myMO = new File_Gettext_MO(); $e = $myMO->load($mofile); } if (!is_object($myMO)) { return; } if ($e == true) { $myMO->strings = array_merge($myMO->strings, $strs); #$myMO->meta['PO-Revision-Date']= date('Y-m-d H:iO'); ksort($myMO->strings); // important! #print_r($myMO->strings); } else { $meta = array('Content-Type' => 'text/plain; charset=' . $charset, 'Last-Translator' => 'MoniWiki Translator', 'PO-Revision-Date' => date('Y-m-d H:iO'), 'MIME-Version' => '1.0', 'Language-Team' => 'MoniWiki Translator'); if (true !== ($e = $myMO->fromArray(array('meta' => $meta, 'strings' => $strs)))) { return "Fail to make a mo file.\n"; } } $vartmp_dir = $DBInfo->vartmp_dir; $tmp = tempnam($vartmp_dir, "GETTEXT"); #$tmp=$vartmp_dir."/GETTEXT.mo"; if (true !== ($e = $myMO->save($tmp))) { return "Fail to save mo file.\n"; } if (!function_exists('bindtextdomain')) { // FIXME use cache class $tmp2 = tempnam($vartmp_dir, "GETTEXT"); $fp = fopen($tmp2, 'w'); if (is_resource($fp)) { fwrite($fp, _msgs_to_php($myMO->strings)); fclose($fp); $ldir2 = $DBInfo->cache_dir . '/' . $ldir; if (!rename($tmp2, $ldir2 . '/moniwiki.php')) { unlink($tmp2); // fail to copy ? } } } # gettext cache workaround # http://kr2.php.net/manual/en/function.gettext.php#58310 # use md5sum instead $md5 = md5_file($tmp); $md5file = $DBInfo->cache_dir . '/' . $ldir . '/md5sum'; $ldir = $DBInfo->cache_dir . '/' . $ldir; _mkdir_p($ldir, 0777); $f = fopen($md5file, 'w'); if (is_resource($f)) { fwrite($f, $md5); fclose($f); } if (!rename($tmp, $ldir . '/moniwiki-' . $md5 . '.mo')) { unlink($md5file); // fail to copy ? return "Fail to save mo file.\n"; } return _("Local translation files are successfully translated !\n"); } return "Empty !\n"; }
public function action_edit() { if ($this->request->param('id')) { $language = $this->request->param('id'); $default = DOCROOT . 'languages/' . $language . '/LC_MESSAGES/messages.po'; $default_mo = DOCROOT . 'languages/' . $language . '/LC_MESSAGES/messages.mo'; } else { Request::current()->redirect(Route::url('oc-panel', array('controller' => 'translations'))); } Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Edit Translation'))); $this->template->title = __('Edit Translation'); $this->template->bind('content', $content); $content = View::factory('oc-panel/pages/translations/edit'); $this->template->scripts['footer'][] = 'js/oc-panel/translations.js'; $base_translation = i18n::get_language_path(); //pear gettext scripts require_once Kohana::find_file('vendor', 'GT/Gettext', 'php'); require_once Kohana::find_file('vendor', 'GT/Gettext/PO', 'php'); require_once Kohana::find_file('vendor', 'GT/Gettext/MO', 'php'); //.po to .mo script require_once Kohana::find_file('vendor', 'php-mo/php-mo', 'php'); //load the .po files $pocreator_en = new File_Gettext_PO(); $pocreator_en->load($base_translation); $pocreator_default = new File_Gettext_PO(); $pocreator_default->load($default); $en_array_order = $pocreator_en->strings; // ksort($en_array_order,SORT_NATURAL); // since PHP 5.4 natcasesort($en_array_order); // better than natsort() ?? //watch out there's a limit of 1000 posts....we have 540... if ($this->request->post()) { foreach ($en_array_order as $key => $string) { $keys[] = $key; } $translations = $this->request->post('translations'); $strings = array(); $out = ''; foreach ($translations as $key => $value) { if ($value != "") { $strings[$keys[$key]] = $value; $out .= '#: String ' . $key . PHP_EOL; $out .= 'msgid "' . $keys[$key] . '"' . PHP_EOL; $out .= 'msgstr "' . $value . '"' . PHP_EOL; $out .= PHP_EOL; } } //write the generated .po to file $fp = fopen($default, 'w+'); $read = fwrite($fp, $out); fclose($fp); $pocreator_default->strings = $strings; //generate the .mo from the .po file phpmo_convert($default); Alert::set(Alert::SUCCESS, $this->request->param('id') . ' ' . __('Language saved')); Request::current()->redirect(Route::url('oc-panel', array('controller' => 'translations', 'action' => 'edit', 'id' => $this->request->param('id')))); } $content->edit_language = $this->request->param('id'); $content->strings_en = $en_array_order; $content->strings_default = $pocreator_default->strings; }