Comments (4)
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.
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.
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.
See in docs: README.md#get-detected-language
from google-translate-php.
Related Issues (20)
- setUrl() It is not available in mainland China HOT 1
- Memory Usage HOT 4
- Very slow HOT 1
- Country an cities sometimes are not translated HOT 4
- ErrorException: Maximum execution time of 60 seconds exceeded HOT 4
- Class "Stichoza\GoogleTranslate\GoogleTranslate" not found HOT 1
- How do I swap to using my own paid api key? HOT 1
- Loading Problem. HOT 2
- I use the IP of a VPN server as a proxy, I wonder why it doesn't work when I use 'https' instead? HOT 5
- Can you give me a list of free Proxies that I can use so that translation is not blocked in the future? HOT 1
- Can you fix this bug in your library for me?
- 400 Bad Request Client error: GET HOT 5
- Can you add the link of my package to your package? HOT 1
- Ignore escaped keys on translations HOT 4
- Error: Class "Stichoza\GoogleTranslate\GoogleTranslate" not found in file HOT 6
- Bug report: preserveParameters gives unexpected results when translating to French HOT 1
- cURL error 60: SSL certificate problem HOT 1
- Please assist me HOT 3
- How can I translate my page web HTML into your library? HOT 2
- PHP - extract the children from this <div id="page-container"> then translate it and put them back into this same <div id="page-container">
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from google-translate-php.