addACL( 'com_langinimaker', 'manage', 'users', 'super administrator' ); if (!$user->authorize( 'com_langinimaker', 'manage' )) { $mainframe->redirect( 'index.php', JText::_('ALERTNOTAUTH') ); } class list_dir { var $x = 0; var $files; function scan_dir($dir) { if(!$temp = @scandir($dir)) { echo "".JText::_('Error! Files not found!')."
"; return; } for($i=0; $i < count($temp); $i++) { if($temp[$i] != "." && $temp[$i] != "..") { if(is_file("$dir/$temp[$i]")) $this->files[] = "$dir/$temp[$i]"; elseif(is_dir("$dir/$temp[$i]")) $stack[] = "$dir/$temp[$i]"; } if((count($temp) - 1) == $i) { if(isset($stack[$this->x])) { $i = -1; $dir = $stack[$this->x]; $temp = scandir($stack[$this->x]); $this->x++; } } } } } function parseXmlGroup( &$xmlLinesParams, $params ) { if (!empty($params)) { $params = $params->children(); // get param foreach($params as $param) { $label = trim($param->attributes('label')); if (!empty($label)) $xmlLinesParams[strtoupper($label)] = $label; $descr = trim($param->attributes('description')); if (!empty($descr)) $xmlLinesParams[strtoupper($descr)] = $descr; // get param options if ($param->children()) { foreach($param->children() as $option) { $data = trim($option->data()); if (!empty($data) && $option->name()=='option') $xmlLinesParams[strtoupper($data)] = $data; } } } } } //$language =& JFactory::getLanguage(); //$language->load( 'com_langinimaker' ); JToolBarHelper::title( JText::_( 'Language INI maker' ), 'generic.png' ); //JToolBarHelper::customX( 'update', 'unarchive.png', 'unarchive.png', 'Update' ); $task = JRequest::getCmd('task'); $db =& JFactory::getDBO(); $query = " SELECT concat(name, ' (', `option`, ')') as `text`, `option` as value FROM #__components WHERE parent=0 group by `option` order by `text`"; $db->setQuery( $query ); $arr_componets = $db->loadAssocList(); $options['languages'] = array(); $options['clients'] = array(); $options['clients']['S'] = JText::_('Site'); foreach (JLanguage::getKnownLanguages(JPATH_SITE) as $k=>$v) { $options['languages']['S_'.$k] = $options['clients']['S'] . ' - '.$v['name']; $all_langs[$k] = $v['name']; } $options['clients']['A'] = JText::_('Administrator'); foreach (JLanguage::getKnownLanguages(JPATH_ADMINISTRATOR) as $k=>$v) { $options['languages']['A_'.$k] = $options['clients']['A'] . ' - '.$v['name']; $all_langs[$k] = $v['name']; } foreach($all_langs as $k=>$v) { $langs[] = array('value'=>$k, 'text'=>$v); } $clients = array( array('value'=>'admin', 'text'=>JText::_('Administrator')), array('value'=>'site', 'text'=>JText::_('Site')) ); $comp = JRequest::getWord('component'); $sel_lang = JRequest::getVar('lang'); $client = JRequest::getCmd('client'); $excess = JRequest::getCmd('excess'); $checked = ($excess==1) ? 'checked' : ''; echo '
'; echo '
'.JText::_('Parameters').''; echo ''; echo ''; echo ''; echo ''; /*echo '';*/ echo ''; echo ''; echo ''; echo ''; echo ''; echo '
Description:
  • Будут выбраны все файлы в папке components/<компонент> или administrator/components/<компонент> в зависимости от параметра «Client» и «Component».
  • В PHP файлах делается поиск ключевых фраз для перевода по шаблону «JText::_(*)».
  • В XML файлах делается поиск ключевых фраз в params, state, layout, message, title, label, description.
  • Полученные ключевые фразы сравниваются и при необходимости объединяются с фразами уже имеющимися в INI файле указанного языка.
  • Результаты объединения ключевых фраз записываются в INI файл, старый файл переименовывается с расширением «bak».
  • Все имеющиеся в INI файле комментарии сохраняются.
'.JText::_('Client').':' . JHTML::_('select.genericlist', $clients, 'client', '', $key = 'value', $text = 'text', $client ) . '
'.JText::_('Component').':' . JHTML::_('select.genericlist', $arr_componets, 'component', '', $key = 'value', $text = 'text', $comp ) . '
'.JText::_('Language key phrases in source scripts').':' . JHTML::_('select.genericlist', $langs, 'lang', '', $key = 'value', $text = 'text', $sel_lang ) . '
'.JText::_('The excess generation').':
'.JText::_('not save').'
'; echo '
'; switch($task) { case 'update': //JToolBarHelper::customX( 'return', 'unarchive.png', 'unarchive.png', 'To return' ); $comp_path['site'] = JPATH_SITE . DS . 'components' . DS . $comp; $comp_path['admin'] = JPATH_ADMINISTRATOR . DS . 'components' . DS . $comp; $lang_path['site'] = JLanguage::getLanguagePath( JPATH_SITE, $sel_lang ); $lang_path['admin'] = JLanguage::getLanguagePath( JPATH_ADMINISTRATOR, $sel_lang ); $inifile['site'] = $lang_path['site'] . DS . $sel_lang . '.' . $comp . '.ini'; $inifile['admin'] = $lang_path['admin'] . DS . $sel_lang . '.' . $comp . '.ini'; //$language = JLanguage::getInstance( $lang ); //$old_debug = $language->setDebug(true); if (file_exists($inifile[$client]) && $content = file_get_contents($inifile[$client])) { // make language string array $registry = new JRegistry(); $registry->loadINI($content); $strings = $registry->toArray( ); // make comments array $explode_content = explode("\n", $content); foreach($explode_content as $k=>$v) { if ( (empty($v))||($v{0}=='#')||($v{0}==';') ) { $comments[$k] = $v; } else { list($key,$value) = explode('=',$v,2); $key = strtoupper($key); $fresh_strings[$key] = $value; $nocomments[$k] = $value; } } } if (empty($comments)) { $comments[] = "# \$Id: " . basename($inifile[$client]) . ' %%filesize%% ' . date('Y-m-d H:i:s'); $comments[] = "# Author "; $comments[] = "# Copyright "; $comments[] = "# License "; $comments[] = "# Note : \n"; } // get language strings from component scripts $fresh_strings = $xmlLinesMenu = $xmlLinesParams = array(); if ($excess) { $fresh_strings = $strings; } $obj = new list_dir; $obj->scan_dir($comp_path[$client]); $pattern = "/JText::_\(\s*\'(.*)\'\s*\)|JText::_\(\s*\"(.*)\"\s*\)". "|JText::sprintf\(\s*\"(.*)\"|JText::sprintf\(\s*\'(.*)\'". "|JText::printf\(\s*\'(.*)\'|JText::printf\(\s*\"(.*)\"/iU"; if ($obj->files) foreach($obj->files as $file) { $pathinfo = pathinfo($file); $lines = array(); // if PHP file if ($pathinfo['extension']=='php') { $str = file_get_contents($file); #preg_match_all("|JText::_\((.*)\)|Uis", $str, $out); #$lines = $out[1]; preg_match_all($pattern, $str, $matches, PREG_SET_ORDER ); foreach($matches as $match) { $lines[] = $match[1]; } } // if XML file if ($pathinfo['extension']=='xml') { $xml = JFactory::getXMLParser('Simple'); $xml->loadFile($file); // get params $params = $xml->document->getElementByPath('params'); parseXmlGroup( &$xmlLinesParams, $params ); // get metadata state $params = $xml->document->getElementByPath('state'); parseXmlGroup( &$xmlLinesParams, $params ); // get metadata layout $layout = $xml->document->getElementByPath('layout'); if (!empty($layout)) { $title = trim($layout->attributes('title')); if (!empty($title)) $xmlLinesParams[strtoupper($title)] = $title; foreach($layout->children() as $item) { $data = $item->data(); $data = trim(str_replace( array(''),'!!!',$data)); if (!empty($data)) { $xmlLinesParams[strtoupper($data)] = $data; } } } // get component menu $menu = $xml->document->getElementByPath('administration/menu'); if (!empty($menu)) { $xmlLinesMenu[strtoupper($comp)] = trim($menu->data()); // get submenu $submenu = $xml->document->getElementByPath('administration/submenu'); if ($submenu) foreach($submenu->children() as $item) { $data = trim($item->data()); if (!empty($data)) { $xmlLinesMenu[strtoupper($comp . '.' . $data)] = $data; } } } } /*if ($pathinfo['extension']=='xml') { preg_match_all('/label=[\"\'](.*)[\"\']|description=[\"\'](.*)[\"\']/Uis', $str, $out); $lines = array_unique(array_merge($out[1], $out[2])); }*/ if ($lines) foreach($lines as $k=>$v) { $acronim = trim(str_replace(array("'", '"'), '', $v)); if ( empty($acronim) || strpos(' '.$acronim, '$')>0 || preg_match("|•(.*)•|Uis", JText::_($acronim)) ) { continue; } $key = strtoupper($acronim); if (!empty($strings[$key])) { $fresh_strings[$key] = $strings[$key]; } else { $fresh_strings[$key] = $acronim; } } } $fresh_strings = array_merge($xmlLinesMenu, $fresh_strings); //$fresh_strings = array_merge($xmlLinesParams, $fresh_strings); foreach($xmlLinesParams as $key=>$val) { if (!empty($strings[$key])) { $fresh_strings[$key] = $strings[$key]; } else { $fresh_strings[$key] = $val; } } //print_r($fresh_strings);break(100); // concat language string and comments $result_strings = array(); $all_strings_count = count($fresh_strings) + count($comments); reset($fresh_strings); for($i=0;$i<$all_strings_count;$i++) { if (isset($comments[$i])) { $result_strings[$i] = $comments[$i]; } else { $line = each($fresh_strings); if (!empty($line)) $result_strings[$i] = strtoupper($line[0]) . '=' . $line[1]; } } // To supplement language strings the new values foreach($result_strings as $k=>$v) { $result_lines[] = $v; } $new_content = join("\n", $result_lines); $new_content = str_replace('%%filesize%%', strlen($new_content), $new_content); //$language->setDebug( $old_debug ); $writable = is_writable($inifile[$client]) || !is_file($inifile[$client]); $msg = ($writable) ? ''. $inifile[$client] .' - '.JText::_('writable').'' : '' . $inifile[$client] .' - not writable!'; if ($writable) $save = '
'; echo $save; echo $msg . '

'.JText::_('New language INI-file').''.nl2br($new_content).'
'.JText::_('Old language INI-file').''.nl2br($content).'
'; echo $save; break; case "save": // save new language strings to INI file $new_content = JRequest::getVar('new_content'); $inifile = JRequest::getVar('inifile'); if (file_exists($inifile.'.bak')) { unlink($inifile.'.bak'); } if (file_exists($inifile)) { rename($inifile, $inifile.'.bak'); } $lf = fopen($inifile, 'w'); fwrite($lf, $new_content); fclose($lf); $msg = JText::_('New language INI-file') . ' "' . $inifile . '" '; if ( is_file( $inifile ) ) { $msg .= JText::_('saved!'); $mainframe->redirect( 'index.php?option=com_langinimaker', $msg ); } else { JError::raiseWarning( 500, $msg . JText::_('NOT saved!') ); } break; } echo '
';