PHP Classes

File: tools/i18n_tools.inc.php

Recommend this page to a friend!
  Classes of Alan H. Lake   I18N class   tools/i18n_tools.inc.php   Download  
File: tools/i18n_tools.inc.php
Role: Auxiliary script
Content type: text/plain
Description: Functions specific to I18N used by other code
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: 18 years ago
Size: 9,491 bytes
 

Contents

Class file image Download
<?php

   
/*
       i18n_tools.inc.php - Common functions used by the I18N tools
       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.2 (September 25, 2005)
    */
   
include_once "../langTable.class.php";

    function
ReadAllLocaleData ($localepath, $localetype, $selectedlocale) {
       
// Reads all locale data for a particular locale and arrayizes it
       
$storagefunction = "ReadAllLocaleData_" . $localetype;
        if(
substr($localepath,-1) != '/')
         
$localepath .= '/';
       
$value = $storagefunction ($localepath, $selectedlocale);

        return
$value;
    }

    function
ReadAllLocaleData_dbm ($localepath, $selectedlocale) {
       
$dbi = dbmopen ("$localepath" . "$selectedlocale" . ".dbm", "r");
       
$key = dbmfirstkey ($dbi);
        while (
$key) {
           
$string[$key] = unserialize (dbmfetch ($dbi, $key));
           
$key = dbmnextkey ($dbi, $key);
        }
       
dbmclose ($dbi);
        while (list (
$entry, $value) = each ($string)){
         
$value = addcslashes($value,'"$
'
);
        }
       
ksort ($string);
       
reset ($string);
        return
$string;
    }

    function
ReadAllLocaleData_onefile ($localepath, $selectedlocale) {
       
$filename = $localepath.$selectedlocale.".php";
        include (
$filename);
        while (list (
$entry, $value) = each ($string)){
         
$value = addcslashes($value,'"$
'
);
        }
        return
$string;
    }

    function
ReadAllLocaleData_severalfiles ($localepath, $selectedlocale) {
       
// Open the specified locale directory
       
$dh = opendir ($localepath);
        while (
$file = readdir ($dh)) {
            if ((
substr ($file, -4) == ".php") &&
                (
substr ($file, 0, strlen ($selectedlocale)) ==
                
"$selectedlocale")) {
                include (
"$localepath"."$file");
                while (list (
$entry, $value) = each ($string)){
                 
$value = addcslashes($value,'"$
'
);
                }
            }
        }
        return
$string;
    }

    function
ReadAllLocaleData_SQL ($localepath, $selectedlocale) {
     
$oLang = $_SESSION['oLang'];

      if(!isset(
$oLang))
        die(
'ReadAllLocaleData_SQL: $oLang is not set.');
     
$string = $oLang->ReadLocaleData($selectedlocale);
      return
$string;
    }

    function
BuildXLCallList ($filename, $required_string) {
       
// Opens a file and extracts a list of all XL() calls

        // Die if the requested source file does not exist
       
if (!file_exists ($filename))
            die (
"<p>The source file you specified ($filename) does not " .
                
"exist.\n");

       
// Open the source file and read its contents
       
$file = fopen ("$filename", "r");
       
$content = fread ($file, filesize ("$filename"));
       
fclose ($file);

       
// Look for calls to function XL
       
$count = preg_match_all ("/(XL\s*\(\")(\S+)(\",)/", $content, $matches);

       
// Stuff the matches into our array
       
while (list ($junkindex, $string) = each ($matches[2])) {
           
$required_string[$string] = 1;
        }

        return
$required_string;
    }


    function
BuildLocaleList ($localepath, $localetype) {
       
// Global variables
       
global $localetypes;
        if (!empty (
$localetypes[$localetype])) {
           
$storagefunction = "BuildLocaleList_" . $localetype;
            return
$storagefunction ($localepath);
        } else {
            die (
"Invalid locale type specified ($localetype).\n");
        }
    }

    function
BuildLocaleList_dbm ($localepath) {
       
// Read the directory list of the specified localepath,
        // return array of filename/locale pairs for all dbm files

        // Open the specified locale directory
       
$dh = opendir ($localepath);
        while (
$file = readdir ($dh)) {
            if (
substr ($file, -4) == ".dbm")
               
$result[substr ($file, 0, -4)] = substr ($file, 0, -4);
        }

        return
$result;
    }

    function
BuildLocaleList_onefile ($localepath) {
       
// Read the directory list of the specified localepath,
        // return array of filename/locale pairs for all monolithic files

        // Open the specified locale directory
       
$dh = opendir ($localepath);
        while (
$file = readdir ($dh)) {
            if (
substr ($file, -4) == ".php")
               
$result[substr ($file, 0, -4)] = substr ($file, 0, -4);
        }
        return
$result;
    }

    function
BuildLocaleList_severalfiles ($localepath) {
       
// Read the directory list of the specified localepath,
        // return array of filename/locale pairs for all monolithic files

        // Open the specified locale directory
       
$dh = opendir ($localepath);
        while (
$file = readdir ($dh)) {
            if ((
substr ($file, -4) == ".php") &&
                (
substr ($file, -6, 1) == "."))
               
$result[substr ($file, 0, -6)] = substr ($file, 0, -6);
        }
        return
$result;
    }

    function
BuildLocaleList_SQL ($localepath) {
       
// $localpath will be empty for this option
        // return array of locale code/name pairs
     
global $oLang;

      if(!isset(
$oLang))
        die(
'BuildLocaleList_SQL: $oLang is not set.');
     
$locales = $oLang->GetLocales();
      return
$locales;
    }


    function
WriteAllLocaleData ($olocalepath, $olocaletype, $selectedlocale,
                                
$available_strings) {
       
$storagefunction = "WriteAllLocaleData_" . $olocaletype;
        if(
substr($olocalepath,-1) != '/')
         
$olocalepath .= '/';
       
$value = $storagefunction ($olocalepath, $selectedlocale, $available_strings);
        return
$value;
    }


    function
WriteAllLocaleData_dbm ($localepath, $selectedlocale, $data) {
       
$dbi = dbmopen ("$localepath" . "$selectedlocale" . ".dbm", "c");
       
$counter = 0;
        while (list (
$entry, $value) = each ($data)) {
           
dbminsert ($dbi, $entry, urlencode (serialize ($value)));
           
$counter++;
        }
       
dbmclose ($dbi);
        return
$counter;
    }


    function
WriteAllLocaleData_onefile ($localepath,$selectedlocale,$data) {
       
ksort ($data);
       
reset ($data);
       
$fp = fopen ($localepath . $selectedlocale . ".php", "w");
       
$counter = 0;

       
fputs ($fp, "<?php\n");

        while (list (
$entry, $value) = each ($data)) {
           
$value = str_replace ("\$", "\\\$", $value);
           
$value = str_replace ("\"", "\\\"", $value);
           
$string = "\$string[$entry] = \"$value\";\n";
           
fputs ($fp, $string);
           
$counter++;
        }
       
fputs ($fp, "?>\n");
       
fclose ($fp);
        echo(
"Don't forget to check ownership and permissions on this newly created file.<br />");
        return
$counter;
    }

    function
WriteAllLocaleData_severalfiles ($localepath,$selectedlocale,$data) {
       
ksort ($data);
       
reset ($data);
       
$counter = 0;

        while (list (
$entry, $value) = each ($data)) {
           
$newletter = strtolower (substr ($entry, 0, 1));
            if (
$newletter != $oldletter) {
                if (
$fp) {
                   
fputs ($fp, "?>\n");
                   
fclose ($fp);
                }
               
$file = $localepath.$selectedlocale.'.'.$newletter.'.php';
                if(!
$fp = fopen ($file, "w"))
                  die(
$file." couldn't be created. Check your directory permissions.");
               
fputs ($fp, "<?php\n");
            }

           
$value = str_replace ("\$", "\\\$", $value);
           
$value = str_replace ("\"", "\\\"", $value);
           
$string = "\$string[$entry] = \"$value\";\n";
           
fputs ($fp, $string);

           
$oldletter = $newletter;
           
$counter++;
        }
       
fputs ($fp, "?>\n");
       
fclose ($fp);
        echo(
"Don't forget to check ownership and permissions on these newly created files.<br />");
        return
$counter;
    }

    function
WriteAllLocaleData_SQL ($localepath,$selectedlocale,$data) {
        global
$oLang;

       
ksort ($data);
       
reset ($data);
       
$counter = 0;

        if(!isset(
$oLang))
          die(
'WriteAllLocaleData_SQL: $oLang is not set.');
        while (list (
$entry, $value) = each ($data)) {
         
$value = str_replace ("\$", "\\\$", $value);
         
$value = str_replace ("\"", "\\\"", $value);
          if(
strlen($value) > 65535){
           
$len = strlen($value);
            die(
'The length of '.$value.' is '.$len.'. The maximum length is 65535.<br />');
          }
         
$oLang->WriteLocaleData($selectedlocale,$entry,$value);
         
$counter++;
        }
        return
$counter;
    }
?>