Browse Source

Adding commented code that handles translations

Julio Montoya 11 years ago
parent
commit
0df0c967a9

+ 2 - 1
main/inc/lib/internationalization.lib.php

@@ -140,8 +140,9 @@ function get_lang($variable, $reserved = null, $language = null)
     }
 
     return $translated;
-    global $app;*/
+    */
 
+    global $app;
     $language_interface = isset($app['language_interface']) ? $app['language_interface'] : api_get_language_interface();
 
     global

+ 41 - 23
main/inc/services.php

@@ -111,33 +111,51 @@ $app->register(new Silex\Provider\ValidatorServiceProvider());
 
 // Implements Symfony2 translator (needed when using forms in Twig)
 $app->register(new Silex\Provider\TranslationServiceProvider(), array(
-    'locale' => 'en',
-    'locale_fallback' => 'en'
+    'locale' => 'es',
+    'locale_fallback' => 'es'
 ));
 
-// Handling po files
-
-/*
+// Handling po files (gettext)
 use Symfony\Component\Translation\Loader\PoFileLoader;
-use Symfony\Component\Translation\Dumper\PoFileDumper;
-
-$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
-$translator->addLoader('pofile', new PoFileLoader());
-
-$language = api_get_language_interface();
-$iterator = new FilesystemIterator(api_get_path(SYS_PATH).'resources/locale/'.$language);
-$filter = new RegexIterator($iterator, '/\.(po)$/');
-
-foreach ($filter as $entry) {
-//$domain = $entry->getBasename('.inc.po');
-$locale = api_get_language_isocode($language); //'es_ES';
-//$translator->addResource('pofile', $entry->getPathname(), $locale, $domain);
-$translator->addResource('pofile', $entry->getPathname(), $locale, 'messages');
-}
-return $translator;
+use Symfony\Component\Translation\Loader\MoFileLoader;
+use Symfony\Component\Finder\Finder;
+
+$app['translator.cache.enabled'] = true;
+
+//$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
+    /** @var Symfony\Component\Translation\Translator $translator  */
+/*    if ($app['translator.cache.enabled']) {
+
+        $locale = $translator->getLocale();
+        //$phpFileDumper = new Symfony\Component\Translation\Dumper\PhpFileDumper();
+        $dumper = new Symfony\Component\Translation\Dumper\MoFileDumper();
+        $catalogue = new Symfony\Component\Translation\MessageCatalogue($locale);
+        $catalogue->add(array('foo' => 'bar'));
+        $dumper->dump($catalogue, array('path' => $app['sys_temp_path']));
+
+    } else {
+
+        $translator->addLoader('pofile', new PoFileLoader());
+
+        $finder = new Finder();
+        $files = $finder->files()->name('*.po')->in(api_get_path(SYS_PATH).'temp/langs/');
+        //$language = api_get_language_interface();
+        // @var SplFileInfo $entry
+        foreach ($files as $entry) {
+            $domain = basename($entry->getPath());
+            $code = $entry->getBasename('.po');
+            //$domain = $entry->getBasename('.inc.po');
+            //$locale = api_get_language_isocode($language); //'es_ES';
+            //if ($domain == 'admin') {
+              //  var_dump($entry->getPathname());
+                //$translator->addResource('pofile', $entry->getPathname(), $code, $domain);
+                $translator->addResource('pofile', $entry->getPathname(), $code);
+            //}
+            //$translator->addResource('pofile', $entry->getPathname(), $locale, 'messages');
+        }
+        return $translator;
+    }
 }));
-
-//$app['translator.domains'] = array();
 */
 
 // Form provider

+ 35 - 9
src/ChamiloLMS/Command/Translation/ExportLanguagesCommand.php

@@ -34,14 +34,19 @@ class ExportLanguagesCommand extends Command
      */
     protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
     {
-        $languageList = array('spanish');
+        $languageList = array('english', 'spanish', 'french');
+        //$languageList = array('spanish');
         foreach ($languageList as $lang) {
-            $output->writeln('<info>Generating lang po files for $lang</info>');
+            $output->writeln("<info>Generating lang po files for: $lang</info>");
             $this->convertLanguageToGettext($lang, $output);
         }
     }
 
-    private function convertLanguageToGettext($destinationLanguage, $output)
+    /**
+     * @param string $destinationLanguage chamilo language 'spanish', 'french', etc
+     * @param $output
+     */
+    private function convertLanguageToGettext($destinationLanguage, Console\Output\OutputInterface $output)
     {
         /** @var \Silex\Application $app */
         $app = $this->getApplication()->getSilexApplication();
@@ -57,7 +62,6 @@ class ExportLanguagesCommand extends Command
         $englishPath = $app['paths']['root_sys'].'main/lang/english/';
 
         // Translate this language.
-
         $toLanguagePath = $app['paths']['root_sys'].'main/lang/'.$destinationLanguage;
 
         if (is_dir($englishPath)) {
@@ -67,6 +71,13 @@ class ExportLanguagesCommand extends Command
                     if ($info['extension'] != 'php') {
                         continue;
                     }
+                    $info['filename'] = explode('.', $info['filename']);
+                    $info['filename'] = $info['filename'][0];
+
+                    if ($info['filename'] != 'admin') {
+                        //continue;
+                    }
+
                     $translations = array();
                     $filename = $englishPath.'/'.$file;
                     $po = file($filename);
@@ -79,17 +90,26 @@ class ExportLanguagesCommand extends Command
                         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);
+
+                            if (strpos($variable, 'langNameOfLang') === false) {
+
+                                $translations[] = array(
+                                    'msgid'  => $variable,
+                                    'msgstr' => $my_variable
+                                );
+                            } else {
+                                continue;
+                            }
                         }
                     }
 
                     //var_dump($translations);
-                    $info['filename'] = explode('.', $info['filename']);
-                    $info['filename'] = $info['filename'][0];
+
 
                     if (!is_dir($tempPath.'/'.$info['filename'])) {
                         mkdir($tempPath.'/'.$info['filename']);
@@ -98,10 +118,16 @@ class ExportLanguagesCommand extends Command
 
                     $new_po_file = $tempPath.'/'.$info['filename'].'/'.$isocode.'.po';
                     $fp = fopen($new_po_file, 'w');
-
+                    $header = 'msgid ""'."\n".'msgstr ""'."\n".'"Content-Type: text/plain; charset=utf-8 \n"';
+                    fwrite($fp, $header);
+                    fwrite($fp, "\n\n");
                     foreach ($translations as $item) {
                         $line = 'msgid "'.addslashes($item['msgid']).'"'."\n";
-                        $line .= 'msgstr "'.addslashes($item['msgstr']).'"'."\n\n";
+
+                        $translated = $item['msgstr'];
+                        $translated = addslashes($translated);
+                        $translated = str_replace(array("\\'"), "'", $translated);
+                        $line .= 'msgstr "'.$translated.'"'."\n\n";
                         fwrite($fp, $line);
                     }
                     fclose($fp);