#!/usr/bin/php
<?php
+if (isset($_SERVER['REMOTE_ADDR'])) { // if the script is accessed via the web.
+ exit;
+}
+
$sec = time();
/**
// code in Moodle. Note that languages without an existing folder are
// ignored if they are not in this array.
$langmatches = array(
- 'sr' => 'sr_lt',
+ 'nb' => 'no',
+ //'sr' => 'sr_lt', // ignore the Serbian translation
'zh' => 'zh_tw',
);
}
// Do it.
-import_language_files();
+build_language_files();
/****************************************************************************
* Everything's a function.
}
if (file_exists($langfile)) {
include($langfile);
- }
- return $string;
+ }
+ $encodedstrings = array();
+ foreach($string as $key => $value) {
+ $encodedstrings[strtr($key, array('\\\'' =>'\\\'', '\'' => '\\\''))] = strtr($value, array('\\\'' =>'\\\'', '\'' => '\\\''));
+ }
+ return $encodedstrings;
}
/**
}
$file = fopen(find_language_file($lang), 'w');
- fwrite($file, "<?php\n");
- 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");
+ fwrite($file, "<?php // \$Id\$\n // tinymce.php - created by the automatic import script\n\n\n");
+ //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 ."'] = '". $line ."';\n");
}
- fwrite($file, "?>");
+ fwrite($file, "\n?>\n");
fclose($file);
}
+function encode_text($text) {
+ // the next two lines are there to make you enjoy how php deals with backslashes
+ $text = preg_replace_callback('/\\\\u([0-9A-F]{4})/', 'unichr', $text); // we're matching something like \u00E9
+ // we're only escaping single quotes, but we gotta prevent escaping those that have already been escaped.
+ $text = strtr($text, array('\\\'' => '\\\'', '\'' => '\\\'', '%' => '%%', '$' => '\$'));
+ return $text;
+}
+
/**
* Unicode aware version of chr, while we wait for PHP to reach the 21st century.
* Taken on http://au.php.net/manual/en/function.chr.php#77911
return $s;
}
-function import_language_files() {
+function build_language_files() {
// build file list
$languagefiles = array();
$langfolders = array(
}
}
+ // process English first - it's necessary to compare other languages later on
+ import_language_files($languagefiles);
+
+ 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\nThe english translation file was only saved in HEAD. You will need \nto copy it to any other necessary checkout manually.\n\n");
+}
+
+function import_language_files($languagefiles) {
+
// process the files and import strings
foreach ($languagefiles as $currentlang => $filepaths) {
continue;
}
- print($filename .' - ');
+ print($currentlang .' - ');
if (!empty($strings)) {
print('loaded '. count($strings) .' current strings - ');
}
$importedstrings = 0;
+ $addedstrings = 0;
foreach ($filepaths as $currentpath => $moduletypes) {
foreach ($moduletypes as $moduletype => $filenames) {
foreach ($filenames as $filename) {
if (!empty($moduletype)) {
$modulestring = $moduletype .'/';
}
- $strings[$modulestring . $section . $subsection . $stringid] = $stringvalue;
+
+ $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 !?
}
write_language_file($currentlang, $strings);
- print("imported $importedstrings strings.\n");
+ print("imported $importedstrings strings, added $addedstrings.\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\nThe english translation file was only saved in head. You will need \n\nto copy it to any other necessary checkout manually.\n\n");
}
-
?>