Further work on Moodle 1.9 integration.
[moodle/mihaisucan.git] / lib / paintweb / ext / moodle / gen_moodlelang.php
blob3cf8baa97141fc70038755cbce29fc11079637fd
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-08-10 20:41:45 +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 // This script works with Moodle 1.9 and Moodle 2.0.
41 $paintweblangdir = '../../build/lang';
42 $moodlelangdir = '../../../../lang';
43 $moodlelangfile = 'paintweb.php';
45 if (!is_dir($paintweblangdir)) {
46 echo "The PaintWeb folder could not be found: $paintweblangdir\n";
47 return 1;
50 if (!is_dir($moodlelangdir)) {
51 echo "The Moodle folder could not be found: $moodlelangdir\n";
52 return 1;
55 if (isset($_SERVER['argv'][1])) {
56 $file = $_SERVER['argv'][1] . '.json';
58 if (!file_exists($paintweblangdir . '/' . $file)) {
59 echo "The PaintWeb language file was not found: $paintweblangdir/$file\n";
60 return 1;
63 paintweb_convert_json_file($file);
65 return 0;
68 $files = glob($paintweblangdir . '/*.json');
69 foreach($files as $file) {
70 paintweb_convert_json_file($file);
73 /**
74 * Convert a PaintWeb JSON language file to a Moodle PHP language file.
76 * @param string $file The file you want to convert.
77 * @return boolean True if the operation executed successfully, or false
78 * otherwise.
80 function paintweb_convert_json_file($file) {
81 global $moodlelangdir, $moodlelangfile, $paintweblangdir;
83 $lang = basename($file, '.json');
85 $outputfolder = $moodlelangdir . '/' . ($lang === 'en' ? 'en_utf8' : $lang);
87 if (!is_dir($outputfolder)) {
88 echo "Skipping $file because $outputfolder was not found.\n";
89 return false;
92 $langparsed = file_get_contents($paintweblangdir . '/' . $file);
93 $langparsed = preg_replace(array('/\s*\/\*.+?\*\//ms', '/\s*\\/\\/.+/'), '', $langparsed);
95 $langparsed = json_decode($langparsed, true);
96 if (!$langparsed) {
97 echo "Parsing $file failed. \n";
98 return false;
101 $output = "<?php\n". paintweb_json2php($langparsed);
103 if (file_put_contents($outputfolder . '/' . $moodlelangfile, $output)) {
104 echo "Generated $outputfolder/$moodlelangfile\n";
105 } else {
106 echo "Failed to write $outputfolder/$moodlelangfile\n";
109 return true;
113 * Convert a PaintWeb JSON object parsed from a language file, to a Moodle PHP
114 * language file.
116 * @param array $obj The array object returned by json_decode().
117 * @param string $prefix The accumulated language group.
118 * @return string The Moodle PHP language file.
120 function paintweb_json2php($obj, $prefix='') {
121 $result = '';
123 foreach ($obj as $key => $val) {
124 if (is_array($val)) {
125 $result .= paintweb_json2php($val, $prefix . $key . ':');
126 } else {
127 $key = var_export($prefix . $key, true);
128 $val = var_export($val, true);
129 $result .= "\$string[$key] = $val;\n";
133 return $result;
136 // vim:set spell spl=en fo=tanqrowcb tw=80 ts=4 sw=4 sts=4 sta et noai nocin fenc=utf-8 ff=unix: