diff --git a/src/ChamiloLMS/Command/Translation/ExportLanguagesCommand.php b/src/ChamiloLMS/Command/Translation/ExportLanguagesCommand.php new file mode 100644 index 0000000000..c69e455319 --- /dev/null +++ b/src/ChamiloLMS/Command/Translation/ExportLanguagesCommand.php @@ -0,0 +1,117 @@ +setName('translation:export_to_gettext') + ->setDescription('Export chamilo translations to po files'); + //->addArgument('theme', InputArgument::OPTIONAL, 'The theme to dump, if none is set then all themes will be generated', null); + } + + /** + * @param Console\Input\InputInterface $input + * @param Console\Output\OutputInterface $output + * @return int|null|void + */ + protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) + { + $languageList = array('spanish'); + foreach ($languageList as $lang) { + $output->writeln('Generating lang po files for $lang'); + $this->convertLanguageToGettext($lang, $output); + } + } + + private function convertLanguageToGettext($destinationLanguage, $output) + { + /** @var \Silex\Application $app */ + $app = $this->getApplication()->getSilexApplication(); + $tempPath = $app['paths']['sys_temp_path'].'langs'; + + if (!is_dir($tempPath)) { + mkdir($tempPath); + $output->writeln('folder $tempPath created '); + } + + $isocode = api_get_language_isocode($destinationLanguage); + + $englishPath = $app['paths']['root_sys'].'main/lang/english/'; + + // Translate this language. + + $toLanguagePath = $app['paths']['root_sys'].'main/lang/'.$destinationLanguage; + + if (is_dir($englishPath)) { + if ($dh = opendir($englishPath)) { + while (($file = readdir($dh)) !== false) { + $info = pathinfo($file); + if ($info['extension'] != 'php') { + continue; + } + $translations = array(); + $filename = $englishPath.'/'.$file; + $po = file($filename); + if (!file_exists($filename) || !file_exists($toLanguagePath.'/'.$file)) { + continue; + } + + foreach ($po as $line) { + $pos = strpos($line, '='); + if ($pos) { + $variable = (substr($line, 1, $pos-1)); + $variable = trim($variable); + require $filename; + $my_variable_in_english = $$variable; + require $toLanguagePath.'/'.$file; + $my_variable = $$variable; + $translations[] = array('msgid' =>$my_variable_in_english, 'msgstr' =>$my_variable); + } + } + + //var_dump($translations); + $info['filename'] = explode('.', $info['filename']); + $info['filename'] = $info['filename'][0]; + + if (!is_dir($tempPath.'/'.$info['filename'])) { + mkdir($tempPath.'/'.$info['filename']); + $output->writeln('folder '.$tempPath.'/'.$info['filename'].' created '); + } + + $new_po_file = $tempPath.'/'.$info['filename'].'/'.$isocode.'.po'; + $fp = fopen($new_po_file, 'w'); + + foreach ($translations as $item) { + $line = 'msgid "'.addslashes($item['msgid']).'"'."\n"; + $line .= 'msgstr "'.addslashes($item['msgstr']).'"'."\n\n"; + fwrite($fp, $line); + } + fclose($fp); + + $message = "File $file converted to ".$new_po_file; + $output->writeln($message); + + } + closedir($dh); + } + } + } +}