classes/i18n/EditableLocaleFile.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00016 // $Id$
00017 
00018 
00019 import('file.EditableFile');
00020 
00021 class EditableLocaleFile extends LocaleFile {
00022    var $editableFile;
00023 
00024    function EditableLocaleFile($locale, $filename) {
00025       parent::LocaleFile($locale, $filename);
00026       $this->editableFile = new EditableFile($this->filename);
00027    }
00028 
00029    function write() {
00030       $this->editableFile->write();
00031    }
00032 
00033    function &getContents() {
00034       return $this->editableFile->getContents();
00035    }
00036 
00037    function setContents(&$contents) {
00038       $this->editableFile->setContents($contents);
00039    }
00040 
00041    function update($key, $value) {
00042       $matches = null;
00043       $quotedKey = String::regexp_quote($key);
00044       preg_match(
00045          "/<message[\W]+key=\"$quotedKey\">/",
00046          $this->getContents(),
00047          $matches,
00048          PREG_OFFSET_CAPTURE
00049       );
00050       if (!isset($matches[0])) return false;
00051 
00052       $offset = $matches[0][1];
00053       $closeOffset = strpos($this->getContents(), '</message>', $offset);
00054       if ($closeOffset === FALSE) return false;
00055 
00056       $newContents = substr($this->getContents(), 0, $offset);
00057       $newContents .= "<message key=\"$key\">" . $this->editableFile->xmlEscape($value);
00058       $newContents .= substr($this->getContents(), $closeOffset);
00059       $this->setContents($newContents);
00060       return true;
00061    }
00062 
00063    function delete($key) {
00064       $matches = null;
00065       $quotedKey = String::regexp_quote($key);
00066       preg_match(
00067          "/[ \t]*<message[\W]+key=\"$quotedKey\">/",
00068          $this->getContents(),
00069          $matches,
00070          PREG_OFFSET_CAPTURE
00071       );
00072       if (!isset($matches[0])) return false;
00073       $offset = $matches[0][1];
00074 
00075       preg_match("/<\/message>[\W]*[\r]?\n/", $this->getContents(), $matches, PREG_OFFSET_CAPTURE, $offset);
00076       if (!isset($matches[0])) return false;
00077       $closeOffset = $matches[0][1] + strlen($matches[0][0]);
00078 
00079       $newContents = substr($this->getContents(), 0, $offset);
00080       $newContents .= substr($this->getContents(), $closeOffset);
00081       $this->setContents($newContents);
00082       return true;
00083    }
00084 
00085    function insert($key, $value) {
00086       $offset = strrpos($this->getContents(), '</locale>');
00087       if ($offset === false) return false;
00088       $newContents = substr($this->getContents(), 0, $offset);
00089       $newContents .= "\t<message key=\"$key\">" . $this->editableFile->xmlEscape($value) . "</message>\n";
00090       $newContents .= substr($this->getContents(), $offset);
00091       $this->setContents($newContents);
00092    }
00093 }
00094 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7