Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 304099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:14:56+00:00 2026-05-12T07:14:56+00:00

What’s the best way to detect the language of a string?

  • 0

What’s the best way to detect the language of a string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:14:57+00:00Added an answer on May 12, 2026 at 7:14 am

    If the context of your code have internet access, you can try to use the Google API for language detection.
    http://code.google.com/apis/ajaxlanguage/documentation/

    var text = "¿Dónde está el baño?";
    google.language.detect(text, function(result) {
      if (!result.error) {
        var language = 'unknown';
        for (l in google.language.Languages) {
          if (google.language.Languages[l] == result.language) {
            language = l;
            break;
          }
        }
        var container = document.getElementById("detection");
        container.innerHTML = text + " is: " + language + "";
      }
    });
    

    And, since you are using c#, take a look at this article on how to call the API from c#.

    UPDATE:
    That c# link is gone, here’s a cached copy of the core of it:

    string s = TextBoxTranslateEnglishToHebrew.Text;
    string key = "YOUR GOOGLE AJAX API KEY";
    GoogleLangaugeDetector detector =
       new GoogleLangaugeDetector(s, VERSION.ONE_POINT_ZERO, key);
    
    GoogleTranslator gTranslator = new GoogleTranslator(s, VERSION.ONE_POINT_ZERO,
       detector.LanguageDetected.Equals("iw") ? LANGUAGE.HEBREW : LANGUAGE.ENGLISH,
       detector.LanguageDetected.Equals("iw") ? LANGUAGE.ENGLISH : LANGUAGE.HEBREW,
       key);
    
    TextBoxTranslation.Text = gTranslator.Translation;
    

    Basically, you need to create a URI and send it to Google that looks like:

    http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20worled&langpair=en%7ciw&key=your_google_api_key_goes_here

    This tells the API that you want to translate “hello world” from English to Hebrew, to which Google’s JSON response would look like:

    {"responseData": {"translatedText":"שלום העולם"}, "responseDetails": null, "responseStatus": 200}
    

    I chose to make a base class that represents a typical Google JSON response:

    [Serializable]
    public class JSONResponse
    {
       public string responseDetails = null;
       public string responseStatus = null;
    }
    

    Then, a Translation object that inherits from this class:

    [Serializable]
    public class Translation: JSONResponse
    {
       public TranslationResponseData responseData = 
        new TranslationResponseData();
    }
    

    This Translation class has a TranslationResponseData object that looks like this:

    [Serializable]
    public class TranslationResponseData
    {
       public string translatedText;
    }
    

    Finally, we can make the GoogleTranslator class:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using System.Web;
    using System.Net;
    using System.IO;
    using System.Runtime.Serialization.Json;
    
    namespace GoogleTranslationAPI
    {
    
       public class GoogleTranslator
       {
          private string _q = "";
          private string _v = "";
          private string _key = "";
          private string _langPair = "";
          private string _requestUrl = "";
          private string _translation = "";
    
          public GoogleTranslator(string queryTerm, VERSION version, LANGUAGE languageFrom,
             LANGUAGE languageTo, string key)
          {
             _q = HttpUtility.UrlPathEncode(queryTerm);
             _v = HttpUtility.UrlEncode(EnumStringUtil.GetStringValue(version));
             _langPair =
                HttpUtility.UrlEncode(EnumStringUtil.GetStringValue(languageFrom) +
                "|" + EnumStringUtil.GetStringValue(languageTo));
             _key = HttpUtility.UrlEncode(key);
    
             string encodedRequestUrlFragment =
                string.Format("?v={0}&q={1}&langpair={2}&key={3}",
                _v, _q, _langPair, _key);
    
             _requestUrl = EnumStringUtil.GetStringValue(BASEURL.TRANSLATE) + encodedRequestUrlFragment;
    
             GetTranslation();
          }
    
          public string Translation
          {
             get { return _translation; }
             private set { _translation = value; }
          }
    
          private void GetTranslation()
          {
             try
             {
                WebRequest request = WebRequest.Create(_requestUrl);
                WebResponse response = request.GetResponse();
    
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string json = reader.ReadLine();
                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
                {
                   DataContractJsonSerializer ser =
                      new DataContractJsonSerializer(typeof(Translation));
                   Translation translation = ser.ReadObject(ms) as Translation;
    
                   _translation = translation.responseData.translatedText;
                }
             }
             catch (Exception) { }
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.