SQL Server 通过 sp 或函数将文本从英语转换为其他语言 [英] SQL Server Convert text from english to other language via an sp or function

查看:36
本文介绍了SQL Server 通过 sp 或函数将文本从英语转换为其他语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 sql server 中有一个表,其中存储了一些英文文本(在 nvarchar 列中).

我必须创建一个存储过程,我将向该过程传递一种语言(印地语、古吉拉特语、阿拉伯语)作为参数 &它将返回从英语转换为该语言的数据.

我知道最好的方法是将这些语言的数据存储在不同的列中,但我不能这样做&想依赖sql server.

是否有一些实用程序或功能可以帮助我完成此任务.

寻找初学者或想法..

解决方案

Sql-Server 无法从一种语言翻译成另一种语言,但可以使用外部工具,请查看 https://blogs.msdn.microsoft.com/samlester/2013/05/04/language-translation-in-sql-server-using-bing-translator-apis-sql-clr/

或者你可以为谷歌翻译器创建一个包装器:http://www.sqlservercentral.com/Forums/Topic819515-386-1.aspx

使用系统;使用 System.Net;使用 System.Text;使用 System.Text.RegularExpressions;公共静态类翻译器{///<总结>///翻译文本.///</总结>///<param name="input">输入.</param>///<param name="languagePair">语言对.</param>///<returns></returns>public static void Main(string[] args){TranslateText(args[1], args[2]);}///<总结>///使用谷歌翻译翻译文本///</总结>///<param name="input">你要翻译的字符串</param>/// 2 个字母的语言对,以|"分隔.///例如en|da"语言对的意思是从英语翻译成丹麦语</param>///<param name="encoding">编码.</param>///<returns>转换为字符串</returns>公共静态字符串 TranslateText(字符串输入,字符串语言对){string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);字符串结果 = String.Empty;using(WebClient webClient = new WebClient()){webClient.Encoding = System.Text.Encoding.UTF7;结果 = webClient.DownloadString(url);}Match m = Regex.Match(result, "(?<=

)(.*?)(?=

)");if (m.Success) 结果 = m.Value;返回结果;}}

但要小心,如果您每分钟以太多请求访问他们的服务器,Google 会立即阻止您.

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..

解决方案

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.

这篇关于SQL Server 通过 sp 或函数将文本从英语转换为其他语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆