0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

yes sorry the object is already created in another file. the problem is i separate my html from my php so i cannot use php code in a tpl file
There's nothing in the question about .tpl files. I don't know what that is, I guess it's some kind of template framework? If you're using a framework, you should include those details in the question and add a tag for it, then the people who know about it will see the question.
tpl is an php extention similar to html but without any php code. about the framework i have no idea because all comment as been removed. i have buy that script on codecanyon and the author used a old framework and used to remove all comment from the framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.