From: skodak Date: Sun, 17 May 2009 10:46:16 +0000 (+0000) Subject: MDL-19184 moodle specific tinymce localization X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f3920b6750449f29532bd527e100cbc117a454b0;p=moodle.git MDL-19184 moodle specific tinymce localization --- diff --git a/lang/en_utf8/editor_tinymce.php b/lang/en_utf8/editor_tinymce.php index db97d07096..e5c01bb0de 100644 --- a/lang/en_utf8/editor_tinymce.php +++ b/lang/en_utf8/editor_tinymce.php @@ -1,707 +1,757 @@ array(), 'plugins'=>array(), 'themes'=>array()); +$result = array(); foreach ($string as $key=>$value) { - $parts = preg_split('|[/:]|', $key); - if (count($parts) != 3) { + $parts = explode(':', $key); + if (count($parts) != 2) { // incorrect string - ignore continue; } - $result[$parts[0]][$parts[1]][$parts[2]] = $value; + $result[$parts[0]][$parts[1]] = $value; } $lang = str_replace('_utf8', '', $lang); // use more standard language codes -$output = ''; - -//main -$output .= 'tinyMCE.addI18n({'.$lang.':'.json_encode($result['main']).'});'; - -//plugins -foreach ($result['plugins'] as $pluginname=>$plugin) { - $output .= "tinyMCE.addI18n('$lang.$pluginname',".json_encode($plugin).');'; -} - -if (!empty($result['themes'][$theme])) { - $output .= "tinyMCE.addI18n('$lang.$theme',".json_encode($result['themes'][$theme]).');'; -} -if (!empty($result['themes'][$theme.'_dlg'])) { - $output .= "tinyMCE.addI18n('$lang.{$theme}_dlg',".json_encode($result['themes'][$theme.'_dlg']).');'; -} - +$output = 'tinyMCE.addI18n({'.$lang.':'.json_encode($result).'});'; $lifetime = '10'; // TODO: increase later @header('Content-type: text/javascript; charset=utf-8'); diff --git a/lib/editor/tinymce/extra/tools/create_langfiles.php b/lib/editor/tinymce/extra/tools/create_langfiles.php deleted file mode 100755 index 921f653a41..0000000000 --- a/lib/editor/tinymce/extra/tools/create_langfiles.php +++ /dev/null @@ -1,287 +0,0 @@ -#!/usr/bin/php - 'no', - //'sr' => 'sr_lt', // ignore the Serbian translation - 'zh' => 'zh_tw', -); - -/** - * I don't recommend making this script web-accessible. Really, it should only - * be run by developers. - */ -if (isset($_SERVER['REMOTE_ADDR'])) { // if the script is accessed via the web. - exit; -} - -// Do it. -build_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 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 = find_language_file($lang); - if ($langfile === FALSE) { - return FALSE; - } - if (file_exists($langfile)) { - include($langfile); - } - $encodedstrings = array(); - foreach($string as $key => $value) { - $encodedstrings[strtr($key, array('\\\'' =>'\\\'', '\'' => '\\\''))] = strtr($value, array('\\\'' =>'\\\'', '\'' => '\\\'')); - } - return $encodedstrings; -} - -/** - * write_language_file - * @param string $lang language-code specifying the translation file to write - * @param array $langdata a $string array to be written into the tinymce translation file - * @return void - */ -function write_language_file($lang, $langdata) { - - $languagefile = find_language_file($lang); - if ($languagefile === FALSE) { - return; - } - - $file = fopen(find_language_file($lang), 'w'); - fwrite($file, " $line) { - fwrite($file, '$string[\''. $id ."'] = '". $line ."';\n"); - } - - 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 - * - * This function expects to be used as callback from preg_replace_callback, so - * the only argument is an array. - */ -function unichr($c) { - $c = hexdec($c[1]); - if ($c <= 0x7F) { - $s = chr($c); - } else if ($c <= 0x7FF) { - $s = chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F); - } else if ($c <= 0xFFFF) { - $s = chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F) - . chr(0x80 | $c & 0x3F); - } else if ($c <= 0x10FFFF) { - $s = chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F) - . chr(0x80 | $c >> 6 & 0x3F) - . chr(0x80 | $c & 0x3F); - } else { - return ''; - } - return $s; -} - -function build_language_files() { - // build file list - $languagefiles = array(); - $langfolders = array( - // this array makes is easier to carry enough information to eventually - // create the xml file TinyMCE wants to provide translation files. - // funny quoting to prevent problems in vim indentation - '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 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) { - - $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"); - } -} - -?> diff --git a/lib/editor/tinymce/extra/tools/get_langs.sh b/lib/editor/tinymce/extra/tools/get_langs.sh index 2fc0cf9f7f..485743bf94 100644 --- a/lib/editor/tinymce/extra/tools/get_langs.sh +++ b/lib/editor/tinymce/extra/tools/get_langs.sh @@ -1,5 +1,5 @@ #!/bin/bash -for lang in 'sq' 'ar' 'az' 'be' 'bn' 'nb' 'bs' 'br' 'bg' 'ca' 'ch' 'zh' 'hr' 'cs' 'da' 'dv' 'nl' 'en' 'et' 'fi' 'fr' 'gl' 'de' 'el' 'gu' 'he' 'hu' 'is' 'id' 'ia' 'it' 'ja' 'ko' 'lv' 'lt' 'mk' 'ms' 'mn' 'se' 'no' 'nn' 'fa' 'pl' 'pt' 'ro' 'ru' 'sc' 'sr' 'ii' 'si' 'sk' 'sl' 'es' 'sv' 'tt' 'th' 'tr' 'tw' 'uk' 'cy' 'vi' +for lang in 'sq' 'ar' 'az' 'be' 'bn' 'nb' 'bs' 'br' 'bg' 'ca' 'ch' 'zh' 'hr' 'cs' 'da' 'dv' 'nl' 'en' 'et' 'fi' 'fr' 'gl' 'de' 'el' 'gu' 'he' 'hu' 'is' 'id' 'ia' 'it' 'ja' 'ko' 'lv' 'lt' 'mk' 'ms' 'mn' 'se' 'no' 'nn' 'fa' 'pl' 'pt' 'ro' 'ru' 'sc' 'sr' 'ii' 'si' 'sk' 'sl' 'es' 'sv' 'tt' 'th' 'tr' 'tw' 'uk' 'cy' 'vi' do wget "http://services.moxiecode.com/i18n/download.aspx?format=xml&code=$lang&product=tinymce" -O temp/$lang.xml done \ No newline at end of file diff --git a/lib/editor/tinymce/extra/tools/langlist.php b/lib/editor/tinymce/extra/tools/langlist.php deleted file mode 100644 index 45dca6f245..0000000000 --- a/lib/editor/tinymce/extra/tools/langlist.php +++ /dev/null @@ -1 +0,0 @@ -theme_standard_styles : "aa=aa;ab=ab;ae=ae;af=af;am=am;ar=ar;as=as;ay=ay;az=az;ba=ba;be=be;bg=bg;bh=bh;bi=bi;bn=bn;bo=bo;br=br;bs=bs;ca=ca;ce=ce;ch=ch;co=co;cs=cs;cu=cu;cv=cv;cy=cy;da=da;de=de;dz=dz;el=el;en=en;eo=eo;es=es;et=et;eu=eu;fa=fa;fi=fi;fj=fj;fo=fo;fr=fr;fy=fy;ga=ga;gd=gd;gl=gl;gn=gn;gu=gu;gv=gv;ha=ha;he=he;hi=hi;ho=ho;hr=hr;hu=hu;hy=hy;hz=hz;ia=ia;id=id;ie=ie;ik=ik;is=is;it=it;iu=iu;ja=ja;jw=jw;ka=ka;ki=ki;kj=kj;kk=kk;kl=kl;km=km;kn=kn;ko=ko;ks=ks;ku=ku;kv=kv;kw=kw;ky=ky;la=la;lb=lb;ln=ln;lo=lo;lt=lt;lv=lv;mg=mg;mh=mh;mi=mi;mk=mk;ml=ml;mn=mn;mo=mo;mr=mr;ms=ms;mt=mt;my=my;na=na;nb=nb;nd=nd;ne=ne;ng=ng;nl=nl;nn=nn;no=no;nr=nr;nv=nv;ny=ny;oc=oc;om=om;or=or;os=os;pa=pa;ph=ph;pi=pi;pl=pl;ps=ps;pt=pt;pt-br=pt-br;qu=qu;rm=rm;rn=rn;ro=ro;ru=ru;rw=rw;sa=sa;sc=sc;sd=sd;se=se;sg=sg;si=si;sk=sk;sl=sl;sm=sm;sn=sn;so=so;sq=sq;sr=sr;ss=ss;st=st;su=su;sv=sv;sw=sw;ta=ta;te=te;tg=tg;th=th;tk=tk;tl=tl;tn=tn;tr=tr;ts=ts;tt=tt;tw=tw;ty=ty;ug=ug;uk=uk;ur=ur;uz=uz;vi=vi;vo=vo;wo=wo;xh=xh;yi=yi;za=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ML;ml_ml=ml_ML;mn_ml=mn_ML;mo_ml=mo_ML;mr_ml=mr_ML;ms_ml=ms_ML;mt_ml=mt_ML;my_ml=my_ML;na_ml=na_ML;nb_ml=nb_ML;nd_ml=nd_ML;ne_ml=ne_ML;ng_ml=ng_ML;nl_ml=nl_ML;nn_ml=nn_ML;no_ml=no_ML;nr_ml=nr_ML;nv_ml=nv_ML;ny_ml=ny_ML;oc_ml=oc_ML;om_ml=om_ML;or_ml=or_ML;os_ml=os_ML;pa_ml=pa_ML;ph_ml=ph_ML;pi_ml=pi_ML;pl_ml=pl_ML;ps_ml=ps_ML;pt_ml=pt_ML;pt-br_ml=pt-br_ML;qu_ml=qu_ML;rm_ml=rm_ML;rn_ml=rn_ML;ro_ml=ro_ML;ru_ml=ru_ML;rw_ml=rw_ML;sa_ml=sa_ML;sc_ml=sc_ML;sd_ml=sd_ML;se_ml=se_ML;sg_ml=sg_ML;si_ml=si_ML;sk_ml=sk_ML;sl_ml=sl_ML;sm_ml=sm_ML;sn_ml=sn_ML;so_ml=so_ML;sq_ml=sq_ML;sr_ml=sr_ML;ss_ml=ss_ML;st_ml=st_ML;su_ml=su_ML;sv_ml=sv_ML;sw_ml=sw_ML;ta_ml=ta_ML;te_ml=te_ML;tg_ml=tg_ML;th_ml=th_ML;tk_ml=tk_ML;tl_ml=tl_ML;tn_ml=tn_ML;tr_ml=tr_ML;ts_ml=ts_ML;tt_ml=tt_ML;tw_ml=tw_ML;ty_ml=ty_ML;ug_ml=ug_ML;uk_ml=uk_ML;ur_ml=ur_ML;uz_ml=uz_ML;vi_ml=vi_ML;vo_ml=vo_ML;wo_ml=wo_ML;xh_ml=xh_ML;yi_ml=yi_ML;za_ml=za_ML;zh_ml=zh_ML;zu_ml=zu_ML", \ No newline at end of file diff --git a/lib/editor/tinymce/extra/tools/update_lang_files.php b/lib/editor/tinymce/extra/tools/update_lang_files.php new file mode 100644 index 0000000000..de71cdb769 --- /dev/null +++ b/lib/editor/tinymce/extra/tools/update_lang_files.php @@ -0,0 +1,205 @@ +"; +} + +require_once('../../../../../config.php'); + +if (!debugging('', DEBUG_DEVELOPER)) { + die('Only for developers!!!!!'); +} + +// 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. +$langconversion = array( + 'no' => 'nb', + 'ko' => false, // ignore Korean translation for now - does not parse + 'sr_lt' => false, //'sr_lt' => 'sr' ignore the Serbian translation + 'zh_tw' => 'zh', +); + +$targetlangdir = "$CFG->dirroot/../lang"; // change if needed +$tempdir = "$CFG->dirroot/lib/editor/tinymce/extra/tools/temp"; +$enfile = "$CFG->dirroot/lang/en_utf8/editor_tinymce.php"; + + +/// first update English lang pack +if (file_exists("$tempdir/en.xml")) { + $old_strings = editor_tinymce_get_all_strings($enfile); + + + //remove all strings from upstream - ignore our modifications for now + // TODO: add support for merging of our tweaks + $parsed = editor_tinymce_parse_xml_lang("$tempdir/en.xml"); + foreach ($parsed as $key=>$value) { + if (array_key_exists($key, $old_strings)) { + unset($old_strings[$key]); + } + } + + if (!$handle = fopen($enfile, 'w')) { + echo "Cannot write to $filename !!"; + exit; + } + + fwrite($handle, "$value) { + fwrite($handle, editor_tinymce_encode_stringline($key, $value)); + } + + fwrite($handle, "\n\n\n\n// Automatically created strings from original TinyMCE lang files, please do not update yet\n\n"); + + foreach ($parsed as $key=>$value) { + fwrite($handle, editor_tinymce_encode_stringline($key, $value)); + } + + fclose($handle); +} + +//now update all other langs +$en_strings = editor_tinymce_get_all_strings($enfile); + +if (!file_exists($targetlangdir)) { + echo "Can not find target lang dir: $targetlangdir !!"; +} + +$langs = new DirectoryIterator($targetlangdir); +foreach ($langs as $lang) { + if ($lang->isDot() or $lang->isLink() or !$lang->isDir()) { + continue; + } + + $lang = $lang->getFilename(); + + if ($lang == 'en' or $lang == 'en_utf8' or $lang == 'CVS' or $lang == '.settings') { + continue; + } + + $xmllang = str_replace('_utf8', '', $lang); + if (array_key_exists($xmllang, $langconversion)) { + $xmllang = $langconversion[$xmllang]; + if (empty($xmllang)) { + echo " Ignoring: $lang\n"; + continue; + } + } + + $xmlfile = "$tempdir/$xmllang.xml"; + if (!file_exists($xmlfile)) { + echo " Skipping: $lang\n"; + continue; + } + + $langfile = "$targetlangdir/$lang/editor_tinymce.php"; + + if (file_exists($langfile)) { + $old_strings = editor_tinymce_get_all_strings($langfile); + foreach ($old_strings as $key=>$value) { + if (!array_key_exists($key, $en_strings)) { + unset($old_strings[$key]); + } + } + } else { + $old_strings = array(); + } + + //remove all strings from upstream - ignore our modifications for now + // TODO: add support for merging of our tweaks + $parsed = editor_tinymce_parse_xml_lang($xmlfile); + foreach ($parsed as $key=>$value) { + if (array_key_exists($key, $old_strings)) { + unset($old_strings[$key]); + } + } + + if (!$handle = fopen($langfile, 'w')) { + echo "Cannot write to $filename !!"; + continue; + } + echo "Modifying: $lang\n"; + + fwrite($handle, "$value) { + fwrite($handle, editor_tinymce_encode_stringline($key, $value)); + } + + fwrite($handle, "\n\n\n\n// Automatically created strings from original TinyMCE lang files, please do not update yet\n\n"); + + foreach ($parsed as $key=>$value) { + fwrite($handle, editor_tinymce_encode_stringline($key, $value)); + } + fclose($handle); +} +unset($langs); + + +die("\nFinished!"); + + + + + + + +/// ============ Utility functions ======================== + +function editor_tinymce_encode_stringline($key, $value) { + $value = str_replace("%","%%",$value); // Escape % characters + $value = trim($value); // Delete leading/trailing white space + return "\$string['$key'] = ".var_export($value, true).";\n"; +} + +function editor_tinymce_parse_xml_lang($file) { + $result = array(); + + $doc = new DOMDocument(); + $doc->load($file); + $groups = $doc->getElementsByTagName('group'); + foreach($groups as $group) { + $section = $group->getAttribute('target'); + $items = $group->getElementsByTagName('item'); + foreach($items as $item) { + $name = $item->getAttribute('name'); + $value = $item->textContent; + $result["$section:$name"] = $value; + } + } + return $result; +} + +function editor_tinymce_get_all_strings($file) { + global $CFG; + + $string = array(); + require_once($file); + + return $string; +} \ No newline at end of file