Attempt to implement image save using data URLs.
[moodle/mihaisucan.git] / lib / paintweb / ext / moodle / gen_moodlelang.php
blob68ee9b540ddb36d6cfea9053f745b9ae0d3735d5
1 <?php
2 /*
3 * Copyright (C) 2009 Mihai Şucan
5 * This file is part of PaintWeb.
7 * PaintWeb is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * PaintWeb is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
20 * $URL: http://code.google.com/p/paintweb $
21 * $Date: 2009-07-24 19:55:28 +0300 $
24 // This script allows you to convert PaintWeb JSON language files into Moodle
25 // PHP language files.
27 // In a typical setup, Moodle holds PaintWeb in lib/paintweb.
29 // If you execute this script without any arguments, all the PaintWeb language
30 // files will be converted into Moodle PHP language files.
32 // Optionally, you can give this script one argument, to tell it which language
33 // you want to convert. This allows you to convert one language file, instead of
34 // all at once.
36 // Warning: running this script will overwrite "paintweb.php" in your Moodle
37 // lang/*/ folders.
39 $paintwebLangDir = '../../build/lang';
40 $moodleLangDir = '../../../../lang';
41 $moodleLangFile = 'paintweb.php';
43 if (!is_dir($paintwebLangDir)) {
44 echo "The PaintWeb folder could not be found: $paintwebLangDir\n";
45 return 1;
48 if (!is_dir($moodleLangDir)) {
49 echo "The Moodle folder could not be found: $moodleLangDir\n";
50 return 1;
53 if (isset($_SERVER['argv'][1])) {
54 $file = $_SERVER['argv'][1] . '.json';
56 if (!file_exists($paintwebLangDir . '/' . $file)) {
57 echo "The PaintWeb language file was not found: $paintwebLangDir/$file\n";
58 return 1;
61 convertFile($file);
63 return 0;
66 $dir = opendir($paintwebLangDir);
67 while ($file = readdir($dir)) {
68 if (!preg_match('/\.json$/', $file)) {
69 continue;
72 convertFile($file);
75 function convertFile ($file) {
76 global $moodleLangDir, $moodleLangFile, $paintwebLangDir;
78 $lang = str_replace('.json', '', $file);
80 $outputFolder = $moodleLangDir . '/' . ($lang === 'en' ? 'en_utf8' : $lang);
82 if (!is_dir($outputFolder)) {
83 echo "Skipping $file because $outputFolder was not found.\n";
84 continue;
87 $langParsed = file_get_contents($paintwebLangDir . '/' . $file);
88 $langParsed = preg_replace(array('/\s*\/\*.+?\*\//ms', '/\s*\\/\\/.+/'), '', $langParsed);
90 $langParsed = json_decode($langParsed, true);
91 if (!$langParsed) {
92 echo "Parsing $file failed. \n";
93 continue;
96 $output = "<?php\n". json2php($langParsed, '');
98 if (file_put_contents($outputFolder . '/' . $moodleLangFile, $output)) {
99 echo "Generated $outputFolder/$moodleLangFile\n";
100 } else {
101 echo "Failed to write $outputFolder/$moodleLangFile\n";
105 function json2php ($obj, $prefix) {
106 $result = '';
108 foreach ($obj as $key => $val) {
109 if (is_array($val)) {
110 $result .= json2php($val, $prefix . $key . ':');
111 } else {
112 $val = str_replace("'", "\\'", $val);
113 $result .= "\$string['$prefix$key'] = '" . $val . "';\n";
117 return $result;
120 // vim:set spell spl=en fo=anl1qrowcb tw=80 ts=2 sw=2 sts=2 sta et noai nocin fenc=utf-8 ff=unix: