function import_language_files($languagefiles)
{
    // process the files and import strings
    foreach ($languagefiles as $currentlang => $filepaths) {
        $strings = array();
        $strings = read_language_file($currentlang);
        if ($strings === FALSE) {
            print "{$currentlang} is not available in Moodle - skipped.\n";
            continue;
        }
        print $currentlang . ' - ';
        if (!empty($strings)) {
            print 'loaded ' . count($strings) . ' current strings - ';
        } else {
            print 'currently empty - ';
        }
        $importedstrings = 0;
        $addedstrings = 0;
        foreach ($filepaths as $currentpath => $moduletypes) {
            foreach ($moduletypes as $moduletype => $filenames) {
                foreach ($filenames as $filename) {
                    $subsection = '';
                    $file = file($currentpath . '/' . $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
                    $lastline = trim(array_pop($file));
                    // remove section ending line
                    if ($lastline == '});') {
                        $filetype = 'submodule';
                        $currentline = explode("'", array_shift($file));
                        $section = substr($currentline[1], 3) . ':';
                        // remove language code, keep section
                    } else {
                        $filetype = 'main';
                        $currentline = explode('{', array_shift($file));
                        $section = substr($currentline[1], 3);
                        // remove language code, keep section
                    }
                    //print($currentline[1] ."\n");
                    $linenumber = 1;
                    while (!empty($file)) {
                        $currentline = trim(array_shift($file));
                        if ($filetype == 'main' && ($pos = strpos($currentline, ':{')) !== false) {
                            // subsections in main file
                            $subsection = substr($currentline, 0, $pos + 1);
                        } elseif (($pos = strpos($currentline, '\',{')) !== false) {
                            // subsection in dialog files
                            $subsection = substr($currentline, 21, $pos) + ',';
                        } elseif ($currentline == '},') {
                            // subsection closing
                            continue;
                        } elseif (($pos = strpos($currentline, ':')) !== false) {
                            // string
                            $stringid = substr($currentline, 0, $pos);
                            $stringvalue = preg_replace('/^(")(.*)(",?)$/', '\\2', trim(substr($currentline, $pos + 1)));
                            $modulestring = '';
                            if (!empty($moduletype)) {
                                $modulestring = $moduletype . '/';
                            }
                            $key = $modulestring . $section . $subsection . $stringid;
                            $value = encode_text($stringvalue);
                            // we're only adding new strings. No removals, no updates.
                            if (!array_key_exists($key, $strings)) {
                                $strings[$key] = $value;
                                //echo "added $key:$value\n";
                                $addedstrings++;
                            }
                            $importedstrings++;
                        } else {
                            // wrong line !?
                            print "\n!!! problem in {$currentpath}/{$filename}:{$linenumber} !!!\n";
                        }
                        $linenumber++;
                    }
                }
            }
        }
        write_language_file($currentlang, $strings);
        print "imported {$importedstrings} strings, added {$addedstrings}.\n";
    }
}
Exemplo n.º 2
0
function import_to_moodle()
{
    // build file list
    $languagefiles = array();
    $langfolders = array('main' => '/langs/', 'plugins' => '/plugins/' . '*/langs/', 'themes' => '/themes/' . '*/langs/');
    foreach ($langfolders as $moduletype => $folders) {
        foreach (glob(LANGPACK_PATH . $folders . '*.js') as $file) {
            $fileinfo = pathinfo($file);
            $filelang = substr($fileinfo['filename'], 0, 2);
            // some files are 'lang.js' and some are 'lang_dlg.js'
            $filepath = $fileinfo['dirname'];
            $filename = $fileinfo['basename'];
            $languagefiles[$filelang][$filepath][$moduletype][] = $filename;
        }
    }
    // process the files and import strings
    foreach ($languagefiles as $currentlang => $filepaths) {
        print $filename . ' - ';
        $strings = array();
        $strings = read_language_file($currentlang);
        if (!empty($strings)) {
            print 'loaded ' . count($strings) . ' current strings - ';
        } else {
            print 'currently empty - ';
        }
        $importedstrings = 0;
        foreach ($filepaths as $currentpath => $moduletypes) {
            foreach ($moduletypes as $moduletype => $filenames) {
                foreach ($filenames as $filename) {
                    $subsection = '';
                    $file = file($currentpath . '/' . $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
                    $lastline = trim(array_pop($file));
                    // remove section ending line
                    if ($lastline == '});') {
                        $filetype = 'submodule';
                        $currentline = explode("'", array_shift($file));
                        $section = substr($currentline[1], 3) . ':';
                        // remove language code, keep section
                    } else {
                        $filetype = 'main';
                        $currentline = explode('{', array_shift($file));
                        $section = substr($currentline[1], 3);
                        // remove language code, keep section
                    }
                    //print($currentline[1] ."\n");
                    $linenumber = 1;
                    while (!empty($file)) {
                        $currentline = trim(array_shift($file));
                        if ($filetype == 'main' && ($pos = strpos($currentline, ':{')) !== false) {
                            // subsections in main file
                            $subsection = substr($currentline, 0, $pos + 1);
                        } elseif (($pos = strpos($currentline, '\',{')) !== false) {
                            // subsection in dialog files
                            $subsection = substr($currentline, 21, $pos) + ',';
                        } elseif ($currentline == '},') {
                            // subsection closing
                            continue;
                        } elseif (($pos = strpos($currentline, ':')) !== false) {
                            // string
                            $stringid = substr($currentline, 0, $pos);
                            $stringvalue = preg_replace('/^(["\'])(.*)(["\'],?)$/', '\\2', trim(substr($currentline, $pos + 1)));
                            $modulestring = '';
                            if (!empty($moduletype)) {
                                $modulestring = $moduletype . '/';
                            }
                            $strings[$modulestring . $section . $subsection . $stringid] = $stringvalue;
                            $importedstrings++;
                        } else {
                            // wrong line !?
                            print "\n!!! problem in {$currentpath}/{$filename}:{$linenumber} !!!\n";
                        }
                        $linenumber++;
                    }
                }
            }
        }
        write_language_file($currentlang, $strings);
        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";
}