Beispiel #1
0
 /**
  * @return _DevblocksTranslationManager
  */
 static function getTranslationService()
 {
     static $languages = array();
     $locale = DevblocksPlatform::getLocale();
     // Registry
     if (isset($languages[$locale])) {
         return $languages[$locale];
     }
     $cache = self::getCacheService();
     if (null === ($map = $cache->load(self::CACHE_TAG_TRANSLATIONS . '_' . $locale))) {
         /* @var $cache _DevblocksCacheManager */
         $map = array();
         $map_en = DAO_Translation::getMapByLang('en_US');
         if (0 != strcasecmp('en_US', $locale)) {
             $map_loc = DAO_Translation::getMapByLang($locale);
         }
         // Loop through the English string objects
         if (is_array($map_en)) {
             foreach ($map_en as $string_id => $obj_string_en) {
                 $string = '';
                 // If we have a locale to check
                 if (isset($map_loc) && is_array($map_loc)) {
                     @($obj_string_loc = $map_loc[$string_id]);
                     @($string = !empty($obj_string_loc->string_override) ? $obj_string_loc->string_override : $obj_string_loc->string_default);
                 }
                 // If we didn't hit, load the English default
                 if (empty($string)) {
                     @($string = !empty($obj_string_en->string_override) ? $obj_string_en->string_override : $obj_string_en->string_default);
                 }
                 // If we found any match
                 if (!empty($string)) {
                     $map[$string_id] = $string;
                 }
             }
         }
         unset($obj_string_en);
         unset($obj_string_loc);
         unset($map_en);
         unset($map_loc);
         // Cache with tag (tag allows easy clean for multiple langs at once)
         $cache->save($map, self::CACHE_TAG_TRANSLATIONS . '_' . $locale);
     }
     $translate = _DevblocksTranslationManager::getInstance();
     $translate->addLocale($locale, $map);
     $translate->setLocale($locale);
     $languages[$locale] = $translate;
     return $translate;
 }
Beispiel #2
0
 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')));
 }