问题
I have a table in sql server which stores some text in english (in nvarchar column).
I have to create a stored procedure to which I will pass a language (Hindi,Gujarati,Arabic) as a parameter & it will return me data converted to that language from English.
I understand that best way would be to store data in those languages in different columns but I cannot do it & want to rely on sql server.
Is there some utility or function which will help me accomplish this.
Looking For Starters or ideas..
回答1:
Sql-Server cannot translate from one language to another language, but you can use external tools for that, please look at https://blogs.msdn.microsoft.com/samlester/2013/05/04/language-translation-in-sql-server-using-bing-translator-apis-sql-clr/
or you can create a wrapper for google translator: http://www.sqlservercentral.com/Forums/Topic819515-386-1.aspx
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
public static class Translator {
/// <summary>
/// Translates the text.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="languagePair">The language pair.</param>
/// <returns></returns>
public static void Main(string[] args)
{
TranslateText(args[1], args[2]);
}
/// <summary>
/// Translate Text using Google Translate
/// </summary>
/// <param name="input">The string you want translated</param>
/// <param name="languagePair">2 letter Language Pair, delimited by "|".
/// e.g. "en|da" language pair means to translate from English to Danish</param>
/// <param name="encoding">The encoding.</param>
/// <returns>Translated to String</returns>
public static string TranslateText(string input, string languagePair)
{
string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
string result = String.Empty;
using(WebClient webClient = new WebClient())
{
webClient.Encoding = System.Text.Encoding.UTF7;
result = webClient.DownloadString(url);
}
Match m = Regex.Match(result, "(?<=<div id=result_box dir=\"ltr\">)(.*?)(?=</div>)");
if (m.Success) result = m.Value;
return result;
}
}
but be carefull, Google will block you in an instant if you hit their servers with too many requests per minute.
来源:https://stackoverflow.com/questions/36735662/sql-server-convert-text-from-english-to-other-language-via-an-sp-or-function