~intrahealth+informatics/i2ce/3.2-dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#!/usr/bin/php
<?php
/*
 * © Copyright 2007, 2008 IntraHealth International, Inc.
 * 
 * This File is part of iHRIS
 * 
 * iHRIS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Translate Templates
 *
 * @package I2CE
 * @subpackage Core
 * @author Carl Leitner <litlfred@ibiblio.org>
 * @copyright Copyright &copy; 2008, 2008 IntraHealth International, Inc. 
 * @version 1.0
 */



$archive ="." . DIRECTORY_SEPARATOR . "translations" .  DIRECTORY_SEPARATOR . 'launchpad-export.tar.gz';
$locales = false;
$usage[] =     
     "[--archive=\$archive]: The archive consisting of all translationd\n\tDefaults to $archive\n"
    . "[--locales=\$locale1,\$locale2..\$localeN]: The locales we wish to translate for\n"
    . "\tIf not specified, it uses  every valid subdirectory of in trhe translations archive file\n"
    . "[--only_changed=T/F]: produce tranlslated files only when something was translated from the source document.\n"
    . "\tDefaults to T=true\n";
    


$booleans['only_changed'] = true;

require_once ("translate_base.php");  

@require_once ("Archive/Tar.php");
if (!class_exists('Archive_Tar')) {
    usage('Please install the PEAR Archive_Tar package');
}



foreach ($args as $key=>$val) {
    switch($key) {
    case 'archive':
        $archive = $archive;
        break;
    case 'locales':
        $locales = preg_split('/,/',$val,-1,PREG_SPLIT_NO_EMPTY);
        break;
    }
}

if (!is_readable($archive)) {
    usage("The file $archive is not readable");
}

$pos = strrpos($archive,'.');
if ($pos === false) {
    usage("Could not determine archive type for $archive -- skipping\n");
}
switch ( substr($archive,$pos+1)) {
case 'tgz':
case 'gz':
    $compression  = 'z';
case 'bz2':
    $compression  = 'j';
case 'tar':
    $compression  = '';
    break;
default:
    usage("Could not determine archive type for $archive -- skipping\n");
}


$tar =  new Archive_Tar($archive, $compression);
$tar_files =array();
$tar_dirs =array();
$files = $tar->listContent();
foreach ($files as $data) {
        if (!array_key_exists('filename',$data)) {
            continue;
        }
        if ($data['typeflag'] == 5) {
            $tar_dirs[] =$data['filename'];
        } else  if ($data['typeflag'] == 0) {
            $tar_files[] =$data['filename'];
        }
}
if ($locales == false) {
    $locales = array();
    foreach ($tar_dirs as $dir) {
        if (preg_match('/^([a-zA-Z_\-]+)\/LC_MESSAGES\/$/', $dir,$matches)) {
            $locales[] = $matches[1];
        }
    }
} else {
    foreach ($locales as $i=>$locale) {
        if(!in_array($locale,$tar_file)) {
            echo "WARNING: the locale $locale does not exist in " . basename($archive) .  "  -- Skipping\n";
            unset($locales[$i]);
        }
    }
}


if (count($locales)==0) {
    usage("No valid locales specified");
}
echo "Translating for locales:\n\t" . implode(',',$locales) . "\n";




$templates = getTranslatableDocuments(false);
$configs = getTranslatableConfigs(false);




//http://www.i18nguy.com/temp/rtl.html
//http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes
$RTLS = array('ar','ara','arc','aze','az','cmc','div','dv','fa','fas','he',
              'jav','jv','jpr','jrb','kas','kaz','kk','ku','kur','ks','lad',
              'ma','mal','may','ms','msa','pa','pal','pan','peo','per','ps',
              'pus','sam','sd','so','som','snd','syc','syr','tmh','tk','tuk'
              ,'ug','uig','ur','urd');

$first_time = true;
$overwrite_all = null;
$changed_text =false;
foreach ($locales as $locale) {    
    $locale_dashed = $locale;
    if ( ($dash_pos = strpos($locale_dashed,'-')) === false ) {
        if ( ($pos = strpos($locale_dashed,'_')) !== false) {
            $two_letter = substr($locale,0,$pos);
            $locale_dashed[$pos] = '-';
        } else {
            $two_letter = $locale;
        }
    } else {
        $two_letter = substr($locale,0,$dash_pos);
    }
    $rtl = in_array( $two_letter, $RTLS);
    echo "{$red}Translating on Locale $locale$black\n";



    foreach ($found_modules as $module=>$top_module) {
        if (!array_key_exists($module,$templates) || !is_array($templates[$module])) {
            continue;
        }
        $mo_file =  $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $module . '.mo';        
        if (!in_array($mo_file,$tar_files)) {
            continue;
        }
        $mo_resource = fopen("php://temp", 'r+');
        fputs($mo_resource,$tar->extractInString($mo_file));
        rewind($mo_resource);
        $translations = loadMO($mo_resource);
        if (count($translations) == 0) {
            continue 2;
        }
    }

    foreach ($found_modules as $module=>$top_module) {
        if (!array_key_exists($module,$configs)) {
            continue;
        }
        $file = null;
        if ( ! $storage->setIfIsSet($file,"/config/data/$module/file")) {
            echo "No config file for $module -- Skipping\n";
            continue;
        }    
        $file = basename($file);
        $source_file = $configs[$module] . DIRECTORY_SEPARATOR . I2CE_Locales::DEFAULT_LOCALE . DIRECTORY_SEPARATOR . $file;
        if (!is_readable($source_file)) {
            echo "Config file $source_file is not readable. -- Skipping\n";
            continue;
        }    
        $dom = new DOMDocument();
        if (!$dom->load($source_file)) {
            echo "Could not load $source_file -- Skipping\n";
            continue;
        }
        $xpath = new DOMXPath($dom);
        $results = $xpath->query(
            '/I2CEConfiguration/configurationGroup//value[@locale="' . I2CE_Locales::DEFAULT_LOCALE  . '"] ' .
//value nodes inherit locale
            '| /I2CEConfiguration/configurationGroup/descendant-or-self::node[@locale="' . I2CE_Locales::DEFAULT_LOCALE . '"]//value' .
            '| /I2CEConfiguration/configurationGroup//description[@locale="' . I2CE_Locales::DEFAULT_LOCALE  . '"] ' .
            '| /I2CEConfiguration/configurationGroup//displayName[@locale="' . I2CE_Locales::DEFAULT_LOCALE  . '"] ' .
//           description and displayname nodes do not inherit locale so we don't clutter up the .pot files
//            '| /I2CEConfiguration/configurationGroup/descendant-or-self::node[@locale="' . I2CE_Locales::DEFAULT_LOCALE . '"]//description' .
//            '| /I2CEConfiguration/configurationGroup/descendant-or-self::node[@locale="' . I2CE_Locales::DEFAULT_LOCALE . '"]//displayName' .
            '| /I2CEConfiguration/metadata/description[@locale="' . I2CE_Locales::DEFAULT_LOCALE  . '"]' .
            '| /I2CEConfiguration/metadata/displayName[@locale="' . I2CE_Locales::DEFAULT_LOCALE  . '"]' .
            '| /I2CEConfiguration/metadata/category[@locale="' . I2CE_Locales::DEFAULT_LOCALE  . '"]' 
            );
        if ($results->length == 0 ) {
            echo "No translatable strings found in $source_file -- Skipping\n";
            continue;
        }
        $has_trans = false;
        for ($i=0; $i < $results->length; $i++) {
            $node = $results->item($i);
            $text = trim($node->textContent);
            $key = false;
            if ($node->tagName == 'value' && ($node->parentNode->getAttribute('type') == 'delimited')) {                  
                list($key,$text) = explode(':',$text);                
            }
            $trans = translate($text);
            if ($trans === false) { 
                //no translation
                //we have to keep the metadata displyaname as it is a required tag.  
                //otherwise, we can't translate it, so we remove it from the tree
                if ( ! ($node->parentNode->tagName == 'metadata' && $node->tagName == 'displayName')) {
                    $node->parentNode->removeChild($node);
                }
                continue;
            } 
            //we have a translation
            $has_trans = true;
            while ($node->hasChildren()) {
                $node->removeChild($node->firstChild);
            }
            if ($key !== false) {
                $trans = $key . ':' . $trans;
            }
            $node->appendChild($dom->createTextNode($trans));
        }        
        if (!$has_trans) {
            echo "No configuration strings in {$blue}$module{$black} were translated.  Skipping\n";
            continue;
        }
        $results = $xpath->query('//*[@locale]');
        for ($i=0; $i < $results->length; $i++) {
            $results->item($i)->setAttribute('locale',$locale);
        }
        $out = $dom->saveXML();
        if (function_exists('tidy_get_output')) {
            $tidy = new tidy();
            $tidy_config = array(
                'input-xml'=>true,
                'output-xml'=>true,
                'indent'=>true,
                'wrap'=>0,
                );
            $tidy->isXML();
            $tidy->parseString($out,$tidy_config);
            $tidy->cleanRepair();
            $out = tidy_get_output($tidy);
        }
        if (!file_put_contents($target_file,$out)) {
            echo "Could not save $file localized to $locale for $module\n:\t$target_file\n";
        }
    }



    foreach ($found_modules as $module=>$top_module) {
        if (!array_key_exists($module,$templates) || !is_array($templates[$module])) {
            continue;
        }
        foreach ($templates[$module] as $basePath=>$files) {
            $topDir = dirname($basePath);
            $translated_files = array();
            foreach($files as $file) {
                $changed_text =false; 
                $trans = translateTemplate($file);
                if (false === $trans) {
                    echo "WARNING: Could not translate $template\n";
                }
                if (!trim($trans)) { //empty contents
                    continue;
                }
                if ($booleans['only_changed'] && !$changed_text) {
                    continue;
                }
                $translated_files[basename($file)] = $trans;
            }
            if (count($translated_files) == 0) {
                continue;
            }
            echo "In {$blue}$module{$black}, we have translations for the following template files:\n\t" . implode(",", array_keys($translated_files)) . "\n";
            $locDir = $topDir . DIRECTORY_SEPARATOR . $locale;
            if (!is_dir($locDir)) {
                if (!mkdir($locDir,0775,true)) {
                    echo "WARNING: Could not make $locDir -- skipping\n";
                    continue;
                }
            }            
            if (!is_writable($locDir)) {
                echo "WARNING: Cannot write to directory $locDir -- skipping\n";
                continue;
            }
            foreach ($translated_files as $file=>$contents) {
                $target = $locDir . DIRECTORY_SEPARATOR . $file;
                if (is_file($target) && is_readable($target)) {
                    $existing = file_get_contents($target);
                    if (false === $existing) {
                        echo "WARNING: Translation to $locale for $file in $module exists but is not readable. -- Skipping\n";
                        continue;
                    }
                    if ($contents == $existing) {
                        continue;
                    } else {
                        $msg ="There is an existing translation to $locale for $file in the module $module.  Overwrite? ";
                        if (function_exists('xdiff_string_diff')) {
                            $diff = xdiff_string_diff($contents, $existing,1);
                        } else {
                            if ($first_time) {
                                $msg .= "\nFor more information, run (on ubunutu):
\tsudo apt-get install re2c  php5-dev build-essential wget
\twget http://www.xmailserver.org/libxdiff-0.22.tar.gz
\tmkdir -p /tmp/src
\tcd /tmp/src
\ttar -xzf libxdiff-0.22.tar.gz 
\tcd libxdiff-0.22
\t./configure
\tmake
\tsudo make install
\tsudo pecl install xdiff
\techo \"extension=xdiff.so\" | sudo tee /etc/php5/conf.d/xdiff.ini \n";
                            }
                            $first_time  = false;
                            $diff = false;
                        }
                        if (prompt($msg, $overwrite_all, $diff)) {
                            unlink($target);
                        } else {
                            continue;
                        }
                    }
                }
                if (false === file_put_contents($target,$contents)) {
                    echo "WARNING: Could not create file $target\n";
                } else {
                    echo "Translate $target\n";
                }
            }
        }        
    }
}








function translate($text) {
    global $changed_text;
    global $translations;
    if (!is_string($text)) {
        return false;
    }
    $text = trim($text);
    if (strlen($text) == 0) {
        return false;
    }
    if (!array_key_exists($text,$translations)) {
        return false;
    }
    $trans = $translations[$text];
    if (strlen($trans) == 0) {
        $trans = false;
    } else  if ($trans === $text) {
        $trans = false;
    } else {
        $changed_text = true;
    }
    return $trans;
}



// $plural_forms =    
//     array(
//         'af'=>2,  'am'=>2,  'ar'=>6,  'arn'=>2,  'az'=>2,  'be'=>3,  'bg'=>2,  'bn'=>2,  'bo'=>1,  'bs'=>3, 
//         'ca'=>2,  'cs'=>3,  'cy'=>4,  'da'=>2,  'de'=>2,  'dz'=>1,  'el'=>2,  'en'=>2,  'eo'=>2,  'es'=>2, 
//         'es_AR'=>2,  'et'=>2,  'eu'=>2,  'fa'=>1,  'fi'=>2,  'fil'=>2,  'fo'=>2,  'fr'=>2,  'fur'=>2,  'fy'=>2, 
//         'ga'=>5,  'gl'=>2,  'gu'=>2,  'gun'=>1,  'ha'=>2,  'he'=>2,  'hi'=>2,  'hy'=>1,  'hr'=>3,  'hu'=>1, 
//         'id'=>1,  'is'=>2,  'it'=>2,  'ja'=>1,  'jv'=>2,  'ka'=>1,  'km'=>1,  'kn'=>1,  'ko'=>1,  'ku'=>2, 
//         'ky'=>1,  'lb'=>2,  'ln'=>2,  'lt'=>3,  'lv'=>3,  'mk'=>3,  'mg'=>2,  'mi'=>2,  'ml'=>2,  'ms'=>1, 
//         'mt'=>4,  'mr'=>2,  'mn'=>2,  'nah'=>2,  'nb'=>2,  'ne'=>2,  'nl'=>2,  'nn'=>2,  'no'=>2,  'nso'=>2, 
//         'or'=>2,  'pa'=>2,  'pap'=>2,  'pl'=>3,  'pt'=>2,  'pt_BR'=>2,  'ro'=>3,  'ru'=>3,  'sco'=>2,  'sk'=>3, 
//         'sl'=>4,  'so'=>2,  'sq'=>2,  'sr'=>4,  'su'=>1,  'sv'=>2,  'ta'=>2,  'te'=>2,  'tg'=>2,  'ti'=>2, 
//         'th'=>1,  'tk'=>2,  'tr'=>1,  'uk'=>3,  'ur'=>2,  'uz'=>1,  'vi'=>1
//         );









function extractPrintF($text) {
    $text = trim($text);
    if (strlen($text) == 0) {
        return '';
    }
    $in_escape = false;
    $begin_quote = false;
    for ($i=0; $i < strlen($text); $i++) {
        $c = $text[$i];
        if ($in_escape) {
            $in_escape = false;
        } else {
            switch ($c) {
            case '"':
            case "'":
                if (!$begin_quote) {
                    $begin_quote = $c;
                } else  if ($begin_quote == $c) {
                    return array(substr($text,1,$i-1),substr($text,$i+1)); //return the unquoted part of the string as well as it's remainder
                } else {
                    //no nothing
                }
                break;
            case '\\':
                $in_escape = true;
                break;
            default:
                //do nothing;
            }
        }
    }
    echo "\tInvalid string $text -- could not extract first full string.  Ignoring\n";
    return array('','');
}


function translateNode($node) {
    global $locale_dashed;
    global $translations;
    global $changed_text;
    global $rtl;
    $attrs = array('span'=>array('text', 'head'));
    if ($node instanceof DOMText) {        
        $trans = translate($node->textContent);       
        if ($trans !== false) {
            $node->deleteData(0,$node->length);
            $node->appendData($trans);            
            if ($rtl && $node->parentNode instanceof DOMElement) {
                $node->parentNode->setAttribute('dir','rtl');
            }
        }
    } 
    $block = false;
    if ($node instanceof DOMElement) {
        if ($node->hasAttribute('translator_comment')) {
            $node->removeAttribute('translator_comment');
        }
        if ($node->hasAttribute('printf')) {
            list($trans_sing,$trans_remainder) = extractPrintF($node->getAttribute('printf'));
            if ($node->hasAttribute('printf_plural') && $node->hasAttribute('printf_form'))  {
                if (array_key_exists($trans_sing,$translations)  && is_array($translations[$trans_sing])) {
                    $changed_text = true;
                    //we have translated plural information.  
                    $node->removeAttribute('printf_plural');
                    $node->removeAttribute('printf_sing');
                    foreach ($translations[$trans_sing] as $n=>$trans) {
                        $node->setAttribute('printf_' . $n , $trans);
                    }
                    $node->setAttribute('lang',$locale_dashed);
                    if ($rtl) {
                        $node->setAttribute('dir','rtl');
                    }
                }
            }  else {
                $trans_sing = translate($trans_sing);
                if ($trans_sing !== false) {
                    $node->setAttribute('printf',$trans_sing . $trans_remainder);
                    if ($rtl) {
                        $node->setAttribute('dir','rtl');
                    }
                }
            }
            return;
        } else  if ($node->hasAttribute('lang')) {
            $lang = $node->getAttribute('lang');
            if ( (strtr($lang,'-','-') ==  I2CE_Locales::DEFAULT_LOCALES)) {
                //if we have lang='en-US' of lang='en_US' attribute, treat this as a block
                $block = $node->tagName;
            }
        } else {
            if (array_key_exists($node->tagName,$attrs)) {
                foreach ($attrs[$node->tagName] as $attr) {
                    if ($node->hasAttribute($attr)) {
                        $trans = translate($node->getAttribute($attr));
                        if ($trans !== false) {
                            $node->setAttribute($attr,$trans);
                        }
                    }
                }
            }
        }
    }
    if (!$block) {
        for ($i = 0; $i < $node->childNodes->length; $i++) {
            translateNode($node->childNodes->item($i));        
        }
    } else {
        $trans = translate($node->textContent);
        if ($trans !== false) {
            $html = false;
            $body = false;
            $head = false;
            $wrapped = wrapHTMLinUTF8($trans);
            $import_dom = new DOMDocument();
            $orig_dom = $node->ownerDocument;
            libxml_clear_errors();
            $success = $orig_dom->loadHTML($wrapped);
            $success = ($success && (count(libxml_get_errors()) ==  0));
            if (!$success) {
                $tmp_dom = new DOMDocument();
                if (!$tmp_dom->loadHTML($trans)) {
                    die("Problem importing translated block");
                } else {
                    die("Yell at carl -- problem with wrapping");
                }
            }
            $xpath = new DOMXpath($import_dom);
            $nodeList = $xpath->query('//'. $block . '[1]');
            if ($nodeList->length != 1) {
                die("Yell at carl about finding blocks");
            }
            $new_node = $orig_dom->importNode($nodeList->item(0),true);
            $new_node->setAttribute('lang', $locale_dashed);
            if ($rtl) {
                $new_node->setAttribute('dir','rtl');
            }
            $node->parentNode->replaceChild($new_node,$node);
        }
    }
}


function translateTemplate($template) {
    libxml_use_internal_errors(true);
    libxml_clear_errors();
    $dom = new DOMDocument('1.0','UTF-8');
    $dom->substituteEntities = false;
    $dom->encoding = 'UTF-8';
    $contents = file_get_contents($template);
    if ($contents === false) {
        echo "Could not read file $template\n";
        return false;
    }
    $html = false;
    $body = false;
    $head = false;
    if (preg_match('/\<\s*head/m', $contents)) {
        $head = true;
    }
    if (preg_match('/\<\s*body/m', $contents)) {
        $body = true;
    }
    if (preg_match('/\<\s*html/m', $contents)) {
        $html = true;
    }    
    if (!$head) {
        if (!$html) {
            $template_contents = '<html>
 <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
 </head><body>' . $contents . '</body></html>';            
        } else {
            echo ("ARGH no head but html on $template\n");
            return false;
        }
    } else {
        if (!preg_match("/^(.*?<\/?head((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>)(.*)/ism", $contents, $matches)) {
            echo ("ARGH head but wierdness on $template for:\n$trans\n");
            return false;                
        }
        $template_contents = $matches[1] . '<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $matches[7];
    }
    if (!$dom->loadHTML($template_contents)){
        $tmp_dom = new DOMDocument();
        if (!$tmp_dom->loadHTML($contents)) {
            echo "Could not load file $template\n";
            return false;
        } else {
            echo "Could not load wrapped file $template -- yell at carl\n";
            return false;
        }
    }
    if (count(libxml_get_errors()) > 0) {
        $tmp_dom = new DOMDocument();
        if (!$tmp_dom->loadHTML($contents)) {
            echo "Could not load file -- DOM Error on  $template\n";
            return false;
        } else {
            echo "Could not load wrapped file $template -- DOM Error -- yell at carl\n";
            return false;
        }
        //return false;
    }
    translateNode($dom->documentElement);    
    $first_tag = '';
    $trans = $dom->saveHTML();    
    if ($html) {
        $preamble = '';
        if (preg_match("/^(.*?)<\/?html((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>/ism", $contents, $matches)) { 
            $preamble = $matches[1];
        }
        if (preg_match("/<\/?html((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>.*<\/html>/ism", $trans, $matches)) { 
            $trans = $preamble . $matches[0];
        } else {
            echo "Could not find html tag in translation\n";
            return false;
        }
    } else if ($body) {
        if (preg_match("/<\/?body((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>.*<\/body>/ism", $trans, $matches)) { 
            $trans = $matches[0];
        } else {
            echo "Could not find body tag in translation\n";
            return false;
        }
    } else {
        if (preg_match("/<\/?body((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>(.*)<\/body>/ism", $trans, $matches)) { 
            $trans = $matches[5];
        } else {
            echo "Could not find body tag in translation\n";
            return false;
        }
    }
    return $trans;
}