static function importTmxFile($filename) { $db = DevblocksPlatform::getDatabaseService(); if (!file_exists($filename)) { return; } /* * [JAS] [TODO] This could be inefficient when reading a lot * of TMX sources, but it could also be inefficient always * keeping it in memory after using it once. I'm going to err * on the side of a little extra DB work for the few times it's * called. */ $hash = array(); foreach (DAO_Translation::getWhere() as $s) { /* @var $s Model_TranslationDefault */ $hash[$s->lang_code . '_' . $s->string_id] = $s; } if (false == @($xml = simplexml_load_file($filename))) { /* @var $xml SimpleXMLElement */ return; } $namespaces = $xml->getNamespaces(true); foreach ($xml->body->tu as $tu) { /* @var $tu SimpleXMLElement */ $msgid = strtolower((string) $tu['tuid']); foreach ($tu->tuv as $tuv) { /* @var $tuv SimpleXMLElement */ $attribs = $tuv->attributes($namespaces['xml']); $lang = (string) $attribs['lang']; $string = (string) $tuv->seg[0]; // [TODO] Handle multiple segs? @($hash_obj = $hash[$lang . '_' . $msgid]); /* @var $hash_obj Model_Translation */ // If not found in the DB if (empty($hash_obj)) { $fields = array(DAO_Translation::STRING_ID => $msgid, DAO_Translation::LANG_CODE => $lang, DAO_Translation::STRING_DEFAULT => $string); $id = DAO_Translation::create($fields); // Add to our hash to prevent dupes $new = new Model_Translation(); $new->id = $id; $new->string_id = $msgid; $new->lang_code = $lang; $new->string_default = $string; $new->string_override = ''; $hash[$lang . '_' . $msgid] = $new; // If exists in DB and the string has changed } elseif (!empty($hash_obj) && 0 != strcasecmp($string, $hash_obj->string_default)) { $fields = array(DAO_Translation::STRING_DEFAULT => $string); DAO_Translation::update($hash_obj->id, $fields); } } } unset($xml); }
function saveAddLanguagePanelAction() { $active_worker = PortSensorApplication::getActiveWorker(); // Make sure we're an active worker if (empty($active_worker) || empty($active_worker->id)) { return; } $codes = DAO_Translation::getDefinedLangCodes(); @($add_lang_code = DevblocksPlatform::importGPC($_REQUEST['add_lang_code'], 'string', '')); @($copy_lang_code = DevblocksPlatform::importGPC($_REQUEST['copy_lang_code'], 'string', '')); @($del_lang_ids = DevblocksPlatform::importGPC($_REQUEST['del_lang_ids'], 'array', array())); if (!empty($del_lang_ids)) { if (is_array($del_lang_ids)) { foreach ($del_lang_ids as $lang_id) { DAO_Translation::deleteByLangCodes($lang_id); } } } // Don't add blanks or the same language twice. if (!empty($add_lang_code) && !isset($codes[$add_lang_code])) { // English reference strings (to know our scope) $english_strings = DAO_Translation::getMapByLang('en_US'); $copy_strings = array(); // If we have a desired source language for defaults, load it. if (!empty($copy_lang_code)) { if (0 == strcasecmp('en_US', $copy_lang_code)) { $copy_strings = $english_strings; } else { $copy_strings = DAO_Translation::getMapByLang($copy_lang_code); } } // Loop through English strings for new language if (is_array($english_strings)) { foreach ($english_strings as $string_id => $src_en) { /* @var $src_en Model_Translation */ $override = ''; // If we have a valid source, copy its override or its default (in that order) @($copy_string = $copy_strings[$string_id]); if (is_a($copy_string, 'Model_Translation')) { $override = !empty($copy_string->string_override) ? $copy_string->string_override : $copy_string->string_default; } // Insert the new string as an override. Only official translations are defaults $fields = array(DAO_Translation::STRING_ID => $string_id, DAO_Translation::LANG_CODE => $add_lang_code, DAO_Translation::STRING_DEFAULT => '', DAO_Translation::STRING_OVERRIDE => $override); DAO_Translation::create($fields); } } } // If we added a new language then change the view to display it if (!empty($add_lang_code)) { $defaults = new Ps_AbstractViewModel(); $defaults->class_name = 'Ps_TranslationView'; $defaults->id = Ps_TranslationView::DEFAULT_ID; // Clear the existing view $view = Ps_AbstractViewLoader::getView(Ps_TranslationView::DEFAULT_ID, $defaults); $view->doResetCriteria(); // Set search to untranslated strings that aren't English $view->renderSortBy = SearchFields_Translation::STRING_ID; $view->renderSortAsc = true; $view->params = array(SearchFields_Translation::LANG_CODE => new DevblocksSearchCriteria(SearchFields_Translation::LANG_CODE, DevblocksSearchCriteria::OPER_EQ, $add_lang_code)); /* * If we didn't copy from another language, only show empty strings * which makes it easier to translate in the GUI. */ if (empty($copy_lang_code)) { $view->params[SearchFields_Translation::STRING_OVERRIDE] = new DevblocksSearchCriteria(SearchFields_Translation::STRING_OVERRIDE, DevblocksSearchCriteria::OPER_EQ, ''); } Ps_AbstractViewLoader::setView($view->id, $view); } self::_clearCache(); DevblocksPlatform::redirect(new DevblocksHttpResponse(array('setup', 'translations'))); }