#!/usr/local/php5/bin/php
<?php /*

xslt_localizer - Create localized versions of XSLT Templates
Copyright (C) 2004 Jeroen Pulles

This program 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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */ 
?><?php


    
/* Mini options helper class for this script */
    
class Settings
    
{
        var 
$lang;
        var 
$catalog;
        var 
$stylesheet;
        var 
$file;

        function 
Settings()
        {
            global 
$argv$argc;
            if (
count($argc) == 0) {
                die(
'Expecting $argv. This is a command line PHP script.');
            }
            else if (
count($argv) == 1) {
                die(
"Usage: 
    xslt_localizer.php language catalog stylesheet [file]
"
);
            }
            if (isset(
$argv[1])) {
                
$this->lang $argv[1];
            }
            if (isset(
$argv[2])) {
                
$this->catalog $argv[2];
            }
            if (isset(
$argv[3])) {
                
$this->stylesheet $argv[3];
            }
            if (isset(
$argv[4])) {
                
$this->file $argv[4];
            } else {
                
$this->file false;
            }
        }
    }


    
$settings = new Settings();

    
# Step 1: Create a stylesheet from the message catalog:
    
$localedoc = new DomDocument();
    
$localedoc->load($settings->catalog.'.'.$settings->lang);

    
$creator = new DomDocument();
    
$creator->load('xslt_localizer.xsl');
    
$xslt = new XSLTProcessor();
    
$xslt->setParameter('''lang'$settings->lang);
    
$xslt->importStylesheet($creator);
    
$localizer $xslt->transformToDoc($localedoc);

    
# Step 2: Apply the localization to the template:
    
$true_template = new DomDocument();
    
$true_template->load($settings->stylesheet);

    
$xslt->importStylesheet($localizer);
    
$localized_template $xslt->transformToDoc($true_template);

    
# Step 3: Transform the source document with the template:
    
if ($settings->file) {
        
$content_doc = new DomDocument();
        
$content_doc->load($settings->file);
        
$xslt->importStylesheet($localized_template);
        
$result_doc $xslt->transformToDoc($content_doc);
        print(
$result_doc->saveXML());
    } 
    else {
        print(
$localized_template->saveXML());
    }

    exit(
0);

?>