0

I want to add a feature in which my website can translate from English to Hindi..

How it can done and there is any free script ?

2
Contributors
3
Replies
16
Views
4 Years
Discussion Span
Last Post by Szabi Zsoldos
0

This is a function that I've used a while back in a Class.
You have to generate your language files like en.txt or de.txt.

In your PHP code you will call the function and the string $contet->__('translate me');
In your de.txt you will have on each line a separate text.

translate me=übersetzen mich

You will have to set your language based on your user settings.

        $language = $this->selectLanguage();
        $this->language = $language;

Then call the following function with $content->__('translate me'); and you should have your text translated.

    public function findString($str) {
        if (array_key_exists($str, $this->lang[$this->language])) {
            return $this->lang[$this->language][$str];
        }
        return $str;
    }


    /**
    * after the text is found your explode it by =
    * @param string $str
    * @returneaza Returns the exploded string 
    */
    public function splitStrings($str) {
        return explode('=',trim($str));
    }


    /** 
    * Function that translates the text  
    * it the words/translation are found, if not, default language will be the written native language
    * @param string $str
    * @returneaza returns a translaton trough splitString();
    */
    public function __($str) {    
        if (!array_key_exists($this->language, $this->lang)) {
            if (file_exists($this->language.'.txt')) {
                $strings = array_map(array($this,'splitStrings'),file($this->language.'.txt'));
                foreach ($strings as $k => $v) {
                    $this->lang[$this->language][$v[0]] = $v[1];
                }
                return $this->findString($str);
            }
            else {
                return $str;
            }
        }
        else {
            return $this->findString($str);
        }
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.