]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15688: Import only languages translated in Moodle, and import at the right place.
authorscyrma <scyrma>
Thu, 31 Jul 2008 08:49:40 +0000 (08:49 +0000)
committerscyrma <scyrma>
Thu, 31 Jul 2008 08:49:40 +0000 (08:49 +0000)
lib/editor/tinymce/create_langfiles.php

index 600f29b502cc4a699196a2c2e4dc7f1cabafe619..534f6789b1d053f8ee869ef44374db23c98f3773 100755 (executable)
@@ -17,11 +17,23 @@ error_reporting(E_ALL | E_NOTICE);
 // this should be the path to a checkout of the translations tree
 // the english file will need to be separately committed to all
 // relevant cvs checkouts (2.0 and up)
-define('MOODLE_LANG_PATH', '/home/mathieu/workspace/moodle_langs');
+define('MOODLE_LANG_PATH', '/home/mathieu/workspace/lang');
 
 // this should be the path to the unzipped tinymce langpacks
 define('LANGPACK_PATH', '/home/mathieu/workspace/tinymce_langs');
 
+// this should be the path to your HEAD checkout - the english translation
+// will be saved there.
+define('MOODLE_CVSHEAD_PATH', '/home/mathieu/workspace/head');
+
+// language correspondance: codes used in TinyMCE that matches a different 
+// code in Moodle. Note that languages without an existing folder are 
+// ignored if they are not in this array.
+$langmatches = array(
+    'sr' => 'sr_lt',
+    'zh' => 'zh_tw',
+);
+
 /**
  * I don't recommend making this script web-accessible. Really, it should only
  * be run by developers.
@@ -31,21 +43,54 @@ if (isset($_SERVER['REMOTE_ADDR'])) { // if the script is accessed via the web.
 }
 
 // Do it.
-import_to_moodle();
+import_language_files();
 
 /****************************************************************************
  * Everything's a function.
  */
 
+/**
+ * Find the language file for a given language.
+ * @param string $lang the language code
+ * @return mixed a string to the language file (does not mean the file exists! - just that this is where it should be) or boolean false if the language is invalid for Moodle.
+ */
+function find_language_file($lang) {
+    global $langmatches;
+
+    if (array_key_exists($lang, $langmatches) && ($langmatches[$lang] != $lang)) {
+        $lang = $langmatches[$lang];
+    }
+
+    if ($lang == 'en') {
+        $filepath = MOODLE_CVSHEAD_PATH .'/lang/en_utf8';
+    } else {
+        $filepath = MOODLE_LANG_PATH .'/'. $lang .'_utf8';
+    }
+
+    // These conditions will be ambiguous if a file (not a folder) exists with the name a of language code. 
+    // This shouldn't happen though and would be a bug on it's own.
+    if (file_exists($filepath) && !is_dir($filepath)) {
+        die(" * path $filepath already exists, but is not a folder, impossible to write translations there.\nFix this and try again.");
+
+    } elseif (!is_dir($filepath)) {
+        return FALSE;
+    }
+
+    return $filepath .'/tinymce.php';
+}
+
 /**
  * read_language_file - reads a translation file
  * @param string $lang language-code specifying a translation file to read
- * @return array a $string array, empty if no translation currently exists.
+ * @return mixed an array, empty if no translation currently exists or boolean false if the language is invalid for Moodle
  */
 function read_language_file($lang) {
     $string = array();
     // we load the current translation file, if it exists, so we don't loose old strings.
-    $langfile = MOODLE_LANG_PATH .'/'. $lang .'_utf8/tinymce.php';
+    $langfile = find_language_file($lang);
+    if ($langfile === FALSE) {
+        return FALSE;
+    }
     if (file_exists($langfile)) {
         include($langfile);
     } 
@@ -59,22 +104,23 @@ function read_language_file($lang) {
  * @return void 
  */
 function write_language_file($lang, $langdata) {
-    $filepath = MOODLE_LANG_PATH .'/'. $lang .'_utf8';
-    if (file_exists($filepath) && !is_dir($filepath)) {
-        die(" * path $filepath already exists, but is not a folder, impossible to write translations there.\nFix this and try again.");
-    } elseif (!file_exists($filepath) && !is_dir($filepath)) {
-        print("\n * folder $filepath did not exists and was created.");
-        mkdir($filepath);
+
+    $languagefile = find_language_file($lang);
+    if ($languagefile === FALSE) {
+        return;
     }
-    $file = fopen($filepath .'/tinymce.php', 'w');
+
+    $file = fopen(find_language_file($lang), 'w');
     fwrite($file, "<?php\n");
-    fwrite($file, "/* this file was automatically imported from TinyMCE's translations */\n");
-    foreach($langdata as $id => $line) {
+    fwrite($file, "/* Note to translators: this file was automatically imported from TinyMCE's \n * translations. Any change to an automatically imported string will be overwritten \n * the next time the import script is run. */\n");
+
+    foreach ($langdata as $id => $line) {
         // the next two lines are there to make you enjoy how php deals with backslashes
         $line = preg_replace_callback('/\\\\u([0-9A-F]{4})/', 'unichr', $line); // we're matching something like \u00E9
         // we're only escaping single quotes, but we gotta prevent escaping those that have already been escaped.
-        fwrite($file, '$string[\''. $id ."']='". strtr($line, array('\\\'' => '\\\'', '\'' => '\\\'')) ."';\n"); 
+        fwrite($file, '$string[\''. $id ."']='". strtr($line, array('\\\'' => '\\\'', '\'' => '\\\'', '%' => '%%', '$' => '\$')) ."';\n"); 
     }
+
     fwrite($file, "?>");
     fclose($file);
 }
@@ -105,7 +151,7 @@ function unichr($c) {
     return $s;
 }
 
-function import_to_moodle() {
+function import_language_files() {
     // build file list
     $languagefiles = array();
     $langfolders = array(
@@ -131,10 +177,15 @@ function import_to_moodle() {
     // process the files and import strings
     foreach ($languagefiles as $currentlang => $filepaths) {
 
-        print($filename .' - ');
-
         $strings = array();
         $strings = read_language_file($currentlang);
+        if ($strings === FALSE) {
+            print("$currentlang is not available in Moodle - skipped.\n");
+            continue;
+        }
+
+        print($filename .' - ');
+
         if (!empty($strings)) {
             print('loaded '. count($strings) .' current strings - ');
         } else {
@@ -198,7 +249,7 @@ function import_to_moodle() {
         print("imported $importedstrings strings.\n");
     }
     global $sec;
-    print("\nIt is suggested you run this script twice. This will include() \nthe generated files once and detect parse errors before you \ncommit the files and wreach havoc on every Moodle site out there. \n\nReally. \n\nIt's quick (this script only took ". (time() - $sec) ." seconds to run), so do it!\n\n");
+    print("\nIt is suggested you run this script twice. This will include() \nthe generated files once and detect parse errors before you \ncommit the files and wreach havoc on every Moodle site out there. \n\nReally. \n\nIt's quick (this script only took ". (time() - $sec) ." seconds to run), so do it!\n\nThe english translation file was only saved in head. You will need \n\nto copy it to any other necessary checkout manually.\n\n");
 }