Hi, please how we can detect if a latin string is in English or French or not recognized on asp.net MVC razor c# using google translation API ??? Thank u :) ...
You can do it using C# using the code below
static object DetectLanguage(string text)
{
TranslationClient client = TranslationClient.Create();
var detection = client.DetectLanguage(text);
Console.WriteLine("{0}\tConfidence: {1}",
detection.Language, detection.Confidence);
return 0;
}
Note: To run above Code, you should install Google.TranslateApi in your project using Nuget
install-package Google.Cloud.Translation.V2 -pre
but before you run the above code, you need to authorize your credentials from google using the key of the Google OAuth project, using the code below
string credential_path = @"C:\..\key.json";
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);
Note: To get credentials or key, you must have Enabled Google Translation API in your project, created using https://console.developers.google.com with proper billing Methods to use this Api
You can also do it using jQuery Ajax by sending POST
request on Google Translation Enabled API on this URL
https://translation.googleapis.com/language/translate/v2/detect?key=YOUR_API_KEY
With test body as
{
'q': 'Google Translate Rocks',
}
Output would be
{
"data": {
"detections": [
[
{
"confidence": 0.24697750806808472,
"isReliable": false,
"language": "en"
}
]
]
}
}
here are the few links which will help you in achieving above solution
1. https://cloud.google.com/translate/docs/detecting-language#translate-detect-language-csharp (Using C#)
2. https://cloud.google.com/translate/docs/reference/detect#body.QUERY_PARAMETERS (Using Ajax)
3. https://developers.google.com/identity/protocols/application-default-credentials#callingdotnet (Using Credentials for ASP.NET)
I could have shown you proper Code with example if the API would have been free to use, but above details should be enough for reference, let me know how if you find any issue using the above code
Noted. Thank you so much Vikas_jk I will try your specs code and inform you if I have some issues ...
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly