Bit of a newbie..

I'm using a great little routine that translates between language pairs:
http://code.google.com/p/gtranslate-api-php/

To use it:

require("GTranslate.php");
$gt = new Gtranslate;
$gt->it_to_en("Ciao mondo");

The program I'm developing has the language pair in a string, so I want to use the program with the language pair as a parameter, eg:

$trans = translate("it_to_en","Ciao mondo");

But I don't really know how to change the class. Any pointers?

Many thanks

Russ

    Try this (works for me):

    require 'GTranslate.php';
    $gt = new Gtranslate();
    $lang_pair = 'it_to_en';
    echo $gt->$lang_pair('Ciao mondo');
    

      Other possibilities:

      I'd extend rather than change the GTranslate class. You could do this:

      require_once 'GTranslate.php';
      
      class Trans extends GTranslate
      {
          public function translate($pair, $txt)
          {
              return $this->$pair($txt);
          }
      }
      
      $gt = new Trans();
      $trans = $gt->translate('it_to_en', 'Ciao mondo');
      echo $trans;

      Or, you could change the translate function. Something like this will work:

      function translate($pair, $txt)
      {
          $gt = new GTranslate();
          return $gt->$pair($txt);
      }
      
      require_once 'GTranslate.php';
      $trans = translate('it_to_en', 'Ciao mondo');
      echo $trans;

        if you're going to use class extending why not go the full out.

        Create abstract classes or interfaces.

        interface Translation {
           public function translate($txt);
        }
        
        class it_to_en implements Translation {
        
        public function translate($txt) {
          // do translate
          return $value;
        }
        }
        
        function translation($type, $txt) {
           require_once($type . '.php');
           $trans = new $type;
           return $trans->translate($txt);
        
        }
        

        its not complete or elegant as it would need a bit more work and error checking, but it would allow for you to throw in new language types without reworking the code.

        It should be noted, I went for interfaces because there is only one function identified and is not going to be the same. The nice thing about interfaces is it will require all classes that implement it to have the same defined functions.

          Yeah, that's good, but "full-out" could include a boatload of other functionalities (including error-checking and other "more work") but since this is a newbie forum and OP admits to being a bit of a one, I thought (rightly or wrongly) that addressing the specific problem would be the best approach for starters. But, at any rate, you're welcome to take it from here.

            I'm sorry I have a bad tendency, especially while doing other things, to skim across non-code. I did not mean to offend.

              Fantastic! Thank you for the quick reply and for the options, I didn't know you could do that.

                Write a Reply...