Git Product home page Git Product logo

Comments (4)

Stichoza avatar Stichoza commented on July 22, 2024

Yeah, that's a good idea. Maybe I (or any of you, cool contributors) will implement a method that will return last detected language. Of course if the detected language is stated somewhere in the response from Google Translate.

from google-translate-php.

quanglam2807 avatar quanglam2807 commented on July 22, 2024

Hi guys, I have found the solution to retrieve all the information from Google Translate included dictionary, detected language,... But I wrote all the code in Javascript, so if anyone can turn it into PHP, it will be great!
First, we need to turn Google JSON to standard JSON

function standardlizeJSON(str) {
    var i = 0;
    var newstr = '';
    while (i < str.length) {
        if ((str[i] == ',') || (str[i] == '[')) {
            switch (str[i + 1]) {
                case ',':
                case ']':
                    newstr += str[i] + 'null';
                    i++;
                    break;
                case '"':
                    var tmp = str.substr(i + 2);
                    var j = tmp.indexOf('"');
                    while ((tmp[j - 1] == '\\') && (tmp[j-2] != '\\')) {
                        j = j + 1 + tmp.substr(j + 1).indexOf('"');
                    }
                    j = i + 2 + j;
                    newstr += str.substring(i, j + 1);
                    i = j + 1;

                    break;
                default:
                    newstr += str[i];
                    i++;
            }
        }
        else {
            newstr += str[i];
            i++;
        }
    }

    return newstr;
}

Then, get the information from JSON

// send request to Google with inputLang (can be "auto"), outputLang, inputText
var standardJSON = standardlizeJSON(googleJSON);
var result = JSON.parse(standardJSON);

var outputText = "";
var inputRoman = "";
var outputRoman = "";
result[0].forEach(function (part) {
    if (part[0]) outputText += part[0];
    if (!part[0]) {
        if (part[2]) outputRoman += part[2];
        if (part[3]) inputRoman += part[3];
    }
});

var inputLang = result[2];
if (inputLang == 'zh-CN') inputLang = 'zh';
var inputDict;
if ((result[11]) || (result[12]) || (result[13]) || (result[14]))
    inputDict = [result[11], result[12], result[13], result[14]];

var inputDict = inputDict;
var outputDict = result[1];

var suggestedinputLang;
if (result[8]) {
    suggestedinputLang = result[8][0][0];
}
if (suggestedinputLang == 'zh-CN') suggestedinputLang = 'zh';

var suggestedinputText;
if (result[7]) {
    suggestedinputText = result[7][1];
}


return {
    inputLang: inputLang,
    suggestedinputLang: suggestedinputLang,
    inputText: inputText,
    suggestedinputText: suggestedinputText,
    inputRoman: inputRoman,
    inputDict: inputDict ? inputDict : {},

    outputLang: outputLang,
    outputText: outputText,
    outputRoman: outputRoman,
    outputDict: outputDict ? outputDict : {}
}

inputLang will return detected language code instead of "auto"

from google-translate-php.

Stichoza avatar Stichoza commented on July 22, 2024

I have figured out how to get recognized language and it way simpler than the code @nhutquang97 provided.

I'm just wondering if there should be a separate method to get that language information. Or let's say pass another parameter to translate() by reference and store recognized language there?

from google-translate-php.

Stichoza avatar Stichoza commented on July 22, 2024

See in docs: README.md#get-detected-language

from google-translate-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.