Martin Dubois
Martin Dubois

Reputation: 47

script not working with my xml to string

first of all sorry for my poor english :) i have a question about this script. i found this script into an old framework and i wanna use it for my friend but i can't make this script working without the whole framework.

string.php

<?php
class StringResource{
    private $string = array();
    public function StringResource($path){
        global $lang;
        if($lang == null){
            $file = $path . 'strings.xml';
            if(file_exists($file))
                $xml = simplexml_load_file($file);
        }else{
            $file = $path . 'strings_'.$lang.'.xml';
            if(file_exists($file)){
                $xml = simplexml_load_file($file);
            }else{
                $file = $path . 'strings.xml';
                if(file_exists($file))
                    $xml = simplexml_load_file($file);
            }
        }

        if($xml){
            foreach($xml->children() as $child) {
                if($child->getName() == 'string'){
                    $attribute = $child->attributes();
                    $key = (String) $attribute['name'];
                    $this->string[$key] = (String) $child;
                }
            }
        }
    }

    public function Get($key){
        return $this->string[$key];
    }
}

i know i could use gettext etc but my friend knowledge is very low so editing one file is the best for him.

strings.xml here a sample of my xml

     <?xml version="1.0" encoding="utf-8"?>
       <strings>
          <string name="menu1">Menu 1 name</string>
          <string name="menu2">menu 2 name</string>
            ....

and when i wanna use them i use {{ST:menu1}} so i don't need to deal with echo. so what is missing for this script to work correctly? is the get key function act like a replace?

Upvotes: 0

Views: 53

Answers (1)

Barmar
Barmar

Reputation: 781004

You create the object with:

$sr = new StringResource("/path/to/directory/");

where the XML data is in /path/to/directory/strings.xml.

Then you can get a string with:

$menu1 = $sr->Get("menu1");

BTW, using the name of the class as the method name is an obsolete way to write a constructor in PHP. The modern way is to name it __construct.

Upvotes: 2

Related Questions