Login   Register  
PHP Classes
elePHPant
Icontem

File: tools/i18n_conversion.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Alan H. Lake  >  I18N class  >  tools/i18n_conversion.php  >  Download  
File: tools/i18n_conversion.php
Role: Application script
Content type: text/plain
Description: Program to convert Locale data from one format to another
Class: I18N class
Get translation texts from different containers
Author: By
Last change: Change reference to ".inc" files to ".inc.php" for greater security.
Date: 2005-09-25 12:10
Size: 10,288 bytes
 

Contents

Class file image Download
<?php

    /*
       i18n_conversion.php - Convert from one XL() storage format to another
       Copyright (C) 2005  Alan H. Lake

         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., 675 Mass Ave, Cambridge, MA 02139, USA.

       Current version: 1.0.1 (September 10, 2005)
    */

    require ("useful.php");
    require ("i18n_tools.inc.php");

    // Name of this script (may be the path to this script)
    $ScriptName = "i18n_conversion.php";

    // Array of known locale information storage types
    $localetypes = array (
        "onefile" => "Monolithic locale file for each locale",
        "severalfiles" => "Locale files for each locale, " .
                          "split by first letter",
        "dbm"          => "DBM database for each locale",
        "SQL"          => "SQL database"
    );

/* ----------------------------------------------------------------------- */

    function DisplayLocaleTypeForm() {
        // Display the form requesting storage type

        // Global variables
        global $localetypes;
        global $ScriptName;

        // Display the form
        echo "<p><h2>I18N Data Format Conversion</h2></p>\n";
        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "Input Storage Mechanism&nbsp;&nbsp;";
        DisplaySelect ($localetypes, "localetype");
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }

    function DisplayLocalePathForm() {
        // Display the form requesting locale path

        // Global variables
        global $localetypes;
        global $ScriptName;
        global $localetype;
        global $olocaletype;

        // Display the form
        echo "<p><h2>I18N Data Format Conversion</h2></p>\n";
        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "<input type=\"HIDDEN\" name=localetype value=\"$localetype\">\n";
        echo "<p>Input Storage Mechanism:&nbsp;&nbsp;$localetype</p>";
        echo "Path to the input locale files:&nbsp;&nbsp;";
        echo "<input maxlen=255 size=50 name=localepath><br><br>\n";
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }


    function DisplayLocaleSelectionForm ($localepath, $localetype) {
        // Display the form requesting selection of a locale to test

        // Global variables
        global $localetypes;
        global $ScriptName;

        echo "<p><h2>I18N Data Format Conversion</h2></p>\n";
        if(substr($localepath,-1) != '/')
          $localepath .= '/';
        $locales = BuildLocaleList ($localepath, $localetype);
        if (empty ($locales))
            die ("<p>The locale path you specified does not contain " .
                 "any translation files based on the storage mechanism " .
                 "you selected ($localetypes[$localetype]). " .
                 "<a href=\"$ScriptName\">Please try again.</a>\n");

        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "<p>Input Storage Mechanism:&nbsp;&nbsp;$localetype</p>";
        if(isset($localepath))
          echo "<p>Input Locale Path:&nbsp;&nbsp;$localepath</p>";
        echo "<input type=\"HIDDEN\" name=localepath value=\"$localepath\">\n";
        echo "<input type=\"HIDDEN\" name=localetype value=\"$localetype\">\n";
        echo "Locale to convert: ";
        DisplaySelect ($locales, "selectedlocale");
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }

    function DisplayLocaleTypeFormOutput($localepath, $localetype, $selectedlocale){
        // Display the form requesting locale storage type

        // Global variables
        global $localetypes;
        global $ScriptName;

        // Display the form
        echo "<p><h2>I18N Data Format Conversion</h2></p>\n";
        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "<p>Input Storage Mechanism:&nbsp;&nbsp; $localetype</p>";
        if(isset($localepath))
          echo "<p>Input Locale Path:&nbsp;&nbsp;$localepath</p>";
        echo "<p>Selected Locale:&nbsp;&nbsp;$selectedlocale</p>";
        echo "<input type=\"HIDDEN\" name=localepath value=\"$localepath\">\n";
        echo "<input type=\"HIDDEN\" name=localetype value=\"$localetype\">\n";
        echo "<input type=\"HIDDEN\" name=selectedlocale ",
             "value=\"$selectedlocale\">\n";
        echo "<p>Output Storage Mechanism:&nbsp;&nbsp;";
        DisplaySelect ($localetypes, "olocaletype");
        echo "</p><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }

    function DisplayLocalePathFormOutput($localepath, $localetype, $selectedlocale) {
        // Display the form requesting locale path

        // Global variables
        global $localetypes;
        global $ScriptName;
        global $olocaletype;

        // Display the form
        echo "<p><h2>I18N Data Format Conversion</h2></p>\n";
        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "<p>Input Storage Mechanism:&nbsp;&nbsp;$localetype</p>";
        if(isset($localepath))
          echo "<p>Input Locale Path:&nbsp;&nbsp;$localepath</p>";
        echo "<p>Selected Locale:&nbsp;&nbsp;$selectedlocale</p>";
        echo "<p>Output Storage Mechanism:&nbsp;&nbsp;$olocaletype</p>";
        echo "Path to the locale files to be written:&nbsp;&nbsp;";
        echo "<input maxlen=255 size=50 name=olocalepath><br><br>\n";
        echo "<input type=\"HIDDEN\" name=localepath value=\"$localepath\">\n";
        echo "<input type=\"HIDDEN\" name=localetype value=\"$localetype\">\n";
        echo "<input type=\"HIDDEN\" name=selectedlocale value=\"$selectedlocale\">\n";
        echo "<input type=\"HIDDEN\" name=olocaletype value=\"$olocaletype\">\n";
        echo "<br><br><input type=\"SUBMIT\" value=\"Convert\">";
        echo "</form>\n";
    }


    function PerformConversion ($localepath, $localetype, $selectedlocale,
                                $olocalepath, $olocaletype) {
        // Perform analysis of the source files and translation data,
        // displaying this analysis.

        // Global variables
        global $localetypes;

        echo "<p><h2>I18N Data Format Conversion</h2></p>\n";
        echo "<p>Input Storage Mechanism:&nbsp;&nbsp;$localetypes[$localetype]<br>\n";
        if(isset($localepath))
          echo "<p>Input Locale Path:&nbsp;&nbsp;$localepath</p>";
        echo "<p>Selected Locale:&nbsp;&nbsp;$selectedlocale</p>";
        echo "<p>Output Storage Mechanism:&nbsp;&nbsp;$localetypes[$olocaletype]<br>\n";
        if(isset($olocalepath))
          echo "<p>Output Locale Path: $olocalepath</p>";

        $available_strings = ReadAllLocaleData ($localepath, $localetype, $selectedlocale);
        echo "<br />Read ", count ($available_strings), " translated strings.<br />\n";
        $count = WriteAllLocaleData ($olocalepath, $olocaletype, $selectedlocale,$available_strings);
        echo "<br />Wrote $count translated strings.<br />\n";
    }


/* -------------------------------------------------------------------------- */
    // Our main code blocks

    if(isset($_POST['localetype']))
      $localetype = $_POST['localetype'];
    if(isset($_POST['localepath']))
      $localepath = $_POST['localepath'];
    if(isset($_POST['selectedlocale']))
      $selectedlocale = $_POST['selectedlocale'];
    if(isset($_POST['olocaletype']))
      $olocaletype = $_POST['olocaletype'];
    if(isset($_POST['olocalepath']))
      $olocalepath = $_POST['olocalepath'];

    include_once "../rfc1766.class.php";
    if(!isset($oRfc1766)){
      $oRfc1766 = new rfc1766_class;
      $_SESSION['oRfc1766'] = $oRfc1766;
    }
    include_once "../i18n.class.php";
    $oI18N = new I18N_class;

    if (isset ($localepath)) {
        if (substr ($localepath, -1) != "/")
            $localepath .= "/";
        if (!is_dir ($localepath))
            die ('<p>You specified the locale path "'.$localepath.'".&nbsp;&nbsp;It does not exist or is otherwise invalid.&nbsp;&nbsp;<a href="'.$ScriptName.'">Please try again.</a></p>'."\n");
    }

    if (isset ($olocalepath)) {
        if (substr ($olocalepath, -1) != "/")
            $olocalepath .= "/";
        if (!is_dir ($olocalepath))
            die ('<p>You specified the locale path "'.$localepath.'".&nbsp;&nbsp;It does not exist or is otherwise invalid.&nbsp;&nbsp;<a href="'.$ScriptName.'">Please try again.</a></p>'."\n");
    }

    if(!isset($localetype)){
        DisplayLocaleTypeForm();
    } elseif (!isset ($localepath) and $localetype != "SQL") {
        DisplayLocalePathForm();
    } elseif (!isset ($selectedlocale)) {
        DisplayLocaleSelectionForm ($localepath, $localetype);
    } elseif (!isset ($olocaletype)) {
        DisplayLocaleTypeFormOutput ($localepath, $localetype,$selectedlocale);
    } elseif (!isset ($olocalepath) && $olocaletype != "SQL") {
        DisplayLocalePathFormOutput ($localepath, $localetype,$selectedlocale);
    } else {
        if($localetype == "SQL" || $olocaletype == "SQL"){
          include_once "../langTable.class.php";
          include_once "../cnx_i18n.inc.php";
          if(!isset($oLang)){
            $oLang = new LangTable_class($host,$user,$password,$db,$table);
            $_SESSION['oLang'] = $oLang;
          }
          $oLang->OpenDb();
          if(!$oLang->TableExists())
            $oLang->CreateTable();
        }
        if(!isset($olocalepath))
          $olocalepath = '/';
        PerformConversion ($localepath, $localetype, $selectedlocale, $olocalepath, $olocaletype);
    }
?>