Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support array of strings #26

Closed
untoreh opened this issue Jul 27, 2015 · 2 comments
Closed

Support array of strings #26

untoreh opened this issue Jul 27, 2015 · 2 comments

Comments

@untoreh
Copy link
Contributor

untoreh commented Jul 27, 2015

In the url parameters you can specify multiple text strings (or q it's the same) so that instead of splitting a long string into substrings and call each time a translation, call it only once with the array of substrings.

@untoreh
Copy link
Contributor Author

untoreh commented Jul 27, 2015

So in order to support arrays...
getResponse

    if (!is_string($string) && !is_array($string)) {
        throw new InvalidArgumentException("Invalid argument provided");
    }

In this function should be specified also the guzzle aggregator (duplicateAggregator), or maybe in the options passed to the translate class? Right now I am hard coding it into guzzle because I did not find the correct method to edit it from the http client.

then just a couple of functions, the one to detect the languagues and the one to get the correct array format...
instanceTranslate

   $array_flag = is_array($string);
   if($array_flag){
            foreach ($responseArray[0] as $itemArray) {
              foreach($itemArray as $item){
                 if (is_string($item)) {
                    $detectedLanguages[] = $item;
                }
            }
        }
        }else{
            foreach ($responseArray as $item) {
                if (is_string($item)) {
                  $detectedLanguages[] = $item;
                }
            }
        }
        if($array_flag){
            $carry = array();
            foreach($responseArray[0] as $item ){
                $carry[] = $item[0][0][0];
            }
            return $carry;
        }else{
        return array_reduce($responseArray[0], function($carry, $item) {
            $carry .= $item[0];
            return $carry;
        });
        }

that's it

@Stichoza
Copy link
Owner

Stichoza commented Aug 9, 2015

fixed in #27

@Stichoza Stichoza closed this as completed Aug 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants