├── .gitattributes ├── .gitignore ├── README.md ├── WordListTranslator.sln └── WordListTranslator ├── AdmAccessToken.cs ├── AdmAuthentication.cs ├── App.config ├── Program.cs ├── Properties └── AssemblyInfo.cs ├── Service References └── TranslateService │ ├── Reference.cs │ ├── Reference.svcmap │ ├── SoapService.wsdl │ ├── WordListTranslator.TranslateService.GetTranslationsResponse.datasource │ ├── WordListTranslator.TranslateService.TranslateArray2Response.datasource │ ├── WordListTranslator.TranslateService.TranslateArrayResponse.datasource │ ├── configuration.svcinfo │ ├── configuration91.svcinfo │ ├── soap.wsdl │ ├── soap.xsd │ ├── soap1.xsd │ ├── soap2.xsd │ └── soap3.xsd └── WordListTranslator.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | 11 | [Dd]ebug/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | [Bb]in/ 16 | [Oo]bj/ 17 | 18 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets 19 | !packages/*/build/ 20 | 21 | # MSTest test Results 22 | [Tt]est[Rr]esult*/ 23 | [Bb]uild[Ll]og.* 24 | 25 | *_i.c 26 | *_p.c 27 | *.ilk 28 | *.meta 29 | *.obj 30 | *.pch 31 | *.pdb 32 | *.pgc 33 | *.pgd 34 | *.rsp 35 | *.sbr 36 | *.tlb 37 | *.tli 38 | *.tlh 39 | *.tmp 40 | *.tmp_proj 41 | *.log 42 | *.vspscc 43 | *.vssscc 44 | .builds 45 | *.pidb 46 | *.log 47 | *.scc 48 | 49 | # Visual C++ cache files 50 | ipch/ 51 | *.aps 52 | *.ncb 53 | *.opensdf 54 | *.sdf 55 | *.cachefile 56 | 57 | # Visual Studio profiler 58 | *.psess 59 | *.vsp 60 | *.vspx 61 | 62 | # Guidance Automation Toolkit 63 | *.gpState 64 | 65 | # ReSharper is a .NET coding add-in 66 | _ReSharper*/ 67 | *.[Rr]e[Ss]harper 68 | 69 | # TeamCity is a build add-in 70 | _TeamCity* 71 | 72 | # DotCover is a Code Coverage Tool 73 | *.dotCover 74 | 75 | # NCrunch 76 | *.ncrunch* 77 | .*crunch*.local.xml 78 | 79 | # Installshield output folder 80 | [Ee]xpress/ 81 | 82 | # DocProject is a documentation generator add-in 83 | DocProject/buildhelp/ 84 | DocProject/Help/*.HxT 85 | DocProject/Help/*.HxC 86 | DocProject/Help/*.hhc 87 | DocProject/Help/*.hhk 88 | DocProject/Help/*.hhp 89 | DocProject/Help/Html2 90 | DocProject/Help/html 91 | 92 | # Click-Once directory 93 | publish/ 94 | 95 | # Publish Web Output 96 | *.Publish.xml 97 | 98 | # NuGet Packages Directory 99 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 100 | #packages/ 101 | 102 | # Windows Azure Build Output 103 | csx 104 | *.build.csdef 105 | 106 | # Windows Store app package directory 107 | AppPackages/ 108 | 109 | # Others 110 | sql/ 111 | *.Cache 112 | ClientBin/ 113 | [Ss]tyle[Cc]op.* 114 | ~$* 115 | *~ 116 | *.dbmdl 117 | *.[Pp]ublish.xml 118 | *.pfx 119 | *.publishsettings 120 | 121 | # RIA/Silverlight projects 122 | Generated_Code/ 123 | 124 | # Backup & report files from converting an old project file to a newer 125 | # Visual Studio version. Backup files are not needed, because we have git ;-) 126 | _UpgradeReport_Files/ 127 | Backup*/ 128 | UpgradeLog*.XML 129 | UpgradeLog*.htm 130 | 131 | # SQL Server files 132 | App_Data/*.mdf 133 | App_Data/*.ldf 134 | 135 | 136 | #LightSwitch generated files 137 | GeneratedArtifacts/ 138 | _Pvt_Extensions/ 139 | ModelManifest.xml 140 | 141 | # ========================= 142 | # Windows detritus 143 | # ========================= 144 | 145 | # Windows image file caches 146 | Thumbs.db 147 | ehthumbs.db 148 | 149 | # Folder config file 150 | Desktop.ini 151 | 152 | # Recycle Bin used on file shares 153 | $RECYCLE.BIN/ 154 | 155 | # Mac desktop service store files 156 | .DS_Store 157 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WordListTranslator 2 | ================== 3 | 4 | 一个(使用Bing)可以将大量英文短语批量翻译成中文的工具 5 | 6 | ### 下载 ### 7 | 点这里下载最新版本:https://github.com/kaedei/WordListTranslater/releases 8 | 9 | ### 为什么写它 ### 10 | 一个Web项目要进行国际化,原来的英文版分支出了一个中文版。于是阿三领导扔给了我一个大概有8700行的Excel文件,里面有项目里所有的英文短语、句子,让我把它们翻译成中文(我是项目里唯一的中国程序员)。 11 | 12 | ### 怎么使用 ### 13 | * 1.此工具使用Access数据库,所以你的电脑上要[安装Access 2007的OLEDB驱动](http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabaseEngine.exe),当然自己去改app.config里面的连接字符串也可以。 14 | * 2.然后在Access数据库中新建个表,至少要包含三个列【自增长ID,需要翻译的,翻译之后的】,把需要翻译的英文导入到表中的【需要翻译的】列 15 | * 3.修改代码中的app.config文件,设置各种参数(一看即懂,不再详述),包括连接字符串、表名、列名等 16 | * 4.(可选)代码中使用了Bing Translate API,你可能需要自己填写client id和app key。当然用我的也可以,不过我不保证能一直可用。 17 | 18 | ### 这里有一个更详细的配置方法:http://blog.sina.com.cn/s/blog_58c506600101vneu.html ### 19 | 20 | ### 运行结果 ### 21 | 最终结果会存放在Access数据库里,可以随便拿来处理。 22 | -------------------------------------------------------------------------------- /WordListTranslator.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.40629.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WordListTranslator", "WordListTranslator\WordListTranslator.csproj", "{DE37C1B8-E41C-4912-9AE4-2F22C6BBFD6D}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B0C1E5A9-2767-43E6-84BC-364687060F67}" 9 | ProjectSection(SolutionItems) = preProject 10 | README.md = README.md 11 | EndProjectSection 12 | EndProject 13 | Global 14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 | Debug|Any CPU = Debug|Any CPU 16 | Release|Any CPU = Release|Any CPU 17 | EndGlobalSection 18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 19 | {DE37C1B8-E41C-4912-9AE4-2F22C6BBFD6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 20 | {DE37C1B8-E41C-4912-9AE4-2F22C6BBFD6D}.Debug|Any CPU.Build.0 = Debug|Any CPU 21 | {DE37C1B8-E41C-4912-9AE4-2F22C6BBFD6D}.Release|Any CPU.ActiveCfg = Release|Any CPU 22 | {DE37C1B8-E41C-4912-9AE4-2F22C6BBFD6D}.Release|Any CPU.Build.0 = Release|Any CPU 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | EndGlobal 28 | -------------------------------------------------------------------------------- /WordListTranslator/AdmAccessToken.cs: -------------------------------------------------------------------------------- 1 | #region File Information 2 | // ******************** 3 | // Translator.cs 4 | // Solution: WordListTranslator 5 | // Project: WordListTranslator 6 | // 7 | // Created At: 2014-05-09 10:24 8 | // Created User: Qing Feng Xu 9 | // 10 | // 11 | // 12 | // ******************** 13 | #endregion 14 | 15 | using System.Diagnostics; 16 | using System.Runtime.Serialization; 17 | 18 | namespace WordListTranslator 19 | { 20 | [DataContract] 21 | public class AdmAccessToken 22 | { 23 | [DataMember] 24 | public string access_token { get; set; } 25 | [DataMember] 26 | public string token_type { get; set; } 27 | [DataMember] 28 | public string expires_in { get; set; } 29 | [DataMember] 30 | public string scope { get; set; } 31 | } 32 | } -------------------------------------------------------------------------------- /WordListTranslator/AdmAuthentication.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Net; 4 | using System.Runtime.Serialization.Json; 5 | using System.Text; 6 | using System.Threading; 7 | using System.Web; 8 | 9 | namespace WordListTranslator 10 | { 11 | public class AdmAuthentication 12 | { 13 | public static readonly string DatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; 14 | private string clientId; 15 | private string clientSecret; 16 | private string request; 17 | private AdmAccessToken token; 18 | private Timer accessTokenRenewer; 19 | 20 | //Access token expires every 10 minutes. Renew it every 9 minutes only. 21 | private const int RefreshTokenDuration = 9; 22 | 23 | public AdmAuthentication(string clientId, string clientSecret) 24 | { 25 | this.clientId = clientId; 26 | this.clientSecret = clientSecret; 27 | //If clientid or client secret has special characters, encode before sending request 28 | this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret)); 29 | this.token = HttpPost(DatamarketAccessUri, this.request); 30 | //renew the token every specfied minutes 31 | accessTokenRenewer = new Timer(new TimerCallback(OnTokenExpiredCallback), this, TimeSpan.FromMinutes(RefreshTokenDuration), TimeSpan.FromMilliseconds(-1)); 32 | } 33 | 34 | public AdmAccessToken GetAccessToken() 35 | { 36 | return this.token; 37 | } 38 | 39 | 40 | private void RenewAccessToken() 41 | { 42 | AdmAccessToken newAccessToken = HttpPost(DatamarketAccessUri, this.request); 43 | //swap the new token with old one 44 | //Note: the swap is thread unsafe 45 | this.token = newAccessToken; 46 | Console.WriteLine(string.Format("Renewed token for user: {0} is: {1}", this.clientId, this.token.access_token)); 47 | } 48 | 49 | private void OnTokenExpiredCallback(object stateInfo) 50 | { 51 | try 52 | { 53 | RenewAccessToken(); 54 | } 55 | catch (Exception ex) 56 | { 57 | Console.WriteLine(string.Format("Failed renewing access token. Details: {0}", ex.Message)); 58 | } 59 | finally 60 | { 61 | try 62 | { 63 | accessTokenRenewer.Change(TimeSpan.FromMinutes(RefreshTokenDuration), TimeSpan.FromMilliseconds(-1)); 64 | } 65 | catch (Exception ex) 66 | { 67 | Console.WriteLine(string.Format("Failed to reschedule the timer to renew access token. Details: {0}", ex.Message)); 68 | } 69 | } 70 | } 71 | 72 | 73 | private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails) 74 | { 75 | //Prepare OAuth request 76 | WebRequest webRequest = WebRequest.Create(DatamarketAccessUri); 77 | webRequest.ContentType = "application/x-www-form-urlencoded"; 78 | webRequest.Method = "POST"; 79 | 80 | byte[] bytes = Encoding.ASCII.GetBytes(requestDetails); 81 | webRequest.ContentLength = bytes.Length; 82 | using (Stream outputStream = webRequest.GetRequestStream()) 83 | { 84 | outputStream.Write(bytes, 0, bytes.Length); 85 | } 86 | using (WebResponse webResponse = webRequest.GetResponse()) 87 | { 88 | DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken)); 89 | //Get deserialized object from JSON stream 90 | AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream()); 91 | return token; 92 | } 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /WordListTranslator/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /WordListTranslator/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data.OleDb; 5 | using System.Threading.Tasks; 6 | 7 | namespace WordListTranslator 8 | { 9 | class Program 10 | { 11 | private static volatile int finishedCount = 0; 12 | static void Main(string[] args) 13 | { 14 | var cId = ConfigurationManager.AppSettings["IdColumn"]; 15 | var cFrom = ConfigurationManager.AppSettings["FromColumn"]; 16 | var cTo = ConfigurationManager.AppSettings["ToColumn"]; 17 | var tableName = ConfigurationManager.AppSettings["TableName"]; 18 | 19 | //填充需要翻译的 20 | var inputContent = FillTranslationList(tableName, cId, cFrom, cTo); 21 | 22 | //var listener = new HttpListener(); 23 | //listener.Prefixes.Add("http://127.0.0.1:23333/"); 24 | //listener.Start(); 25 | 26 | var clientId = ConfigurationManager.AppSettings["ClientId"]; 27 | var clientSecret = ConfigurationManager.AppSettings["ClientSecret"]; 28 | var auth = new AdmAuthentication(clientId, clientSecret); 29 | 30 | //翻译 31 | using (var connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString)) 32 | { 33 | connection.Open(); 34 | Parallel.For(0, inputContent.Count, index => 35 | { 36 | TranslateAndSave(inputContent, index, auth, connection, tableName, cTo); 37 | }); 38 | } 39 | 40 | Console.WriteLine("按回车键退出"); 41 | Console.ReadLine(); 42 | } 43 | 44 | private static void TranslateAndSave(List> inputContent, int index, AdmAuthentication auth, OleDbConnection connection, 45 | string tableName, string cTo) 46 | { 47 | var id = inputContent[index].Item1; 48 | var text = inputContent[index].Item2; 49 | if (string.IsNullOrEmpty(text)) 50 | return; 51 | if (ContainsHtmlLabel(text)) 52 | { 53 | Console.WriteLine("({0}/{1}){2}\t{3}", ++finishedCount, inputContent.Count, text, text); 54 | } 55 | else 56 | { 57 | var client = new TranslateService.LanguageServiceClient(); 58 | try 59 | { 60 | var token = auth.GetAccessToken(); 61 | var fromLang = ConfigurationManager.AppSettings["FromLanguage"]; 62 | var toLang = ConfigurationManager.AppSettings["ToLanguage"]; 63 | //How to: https://msdn.microsoft.com/en-us/library/ff512437.aspx 64 | //maybe we can use TranslateArray here to get a better performance 65 | var result = client.Translate("Bearer " + token.access_token, 66 | text, fromLang, toLang, "text/plain", "general", null); 67 | using (var command = connection.CreateCommand()) 68 | { 69 | command.CommandText = string.Format(@"Update {0} Set {1}='{2}' Where ID={3}", 70 | tableName, cTo, result.Replace("'", "''"), id); 71 | command.ExecuteNonQuery(); 72 | } 73 | Console.WriteLine("({0}/{1}){2}\t{3}", ++finishedCount, inputContent.Count, text, result); 74 | } 75 | catch 76 | { 77 | Console.WriteLine("({0}/{1}){2}\t{3}", ++finishedCount, inputContent.Count, text, text); 78 | } 79 | } 80 | } 81 | 82 | /// 83 | /// 获取需要翻译的文本列表 84 | /// 85 | /// ID列名称 86 | /// 需要翻译的列名 87 | /// 数据库中的表名 88 | /// 把翻译后的结果放到哪个列 89 | private static List> FillTranslationList(string tableName, string cId, string cFrom, string cTo) 90 | { 91 | var inputContent = new List>(); 92 | using (var connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString)) 93 | { 94 | connection.Open(); 95 | using (var command = new OleDbCommand(string.Format("Select [{0}],[{1}] From [{2}] Where [{3}] is NULL or [{3}]=''", cId, cFrom, tableName, cTo), 96 | connection)) 97 | { 98 | using (var reader = command.ExecuteReader()) 99 | { 100 | while (reader != null && reader.Read()) 101 | { 102 | try 103 | { 104 | inputContent.Add(new Tuple(reader.GetInt32(0), reader.GetString(1))); 105 | } 106 | catch 107 | { 108 | inputContent.Add(new Tuple(reader.GetInt32(0), "")); 109 | } 110 | } 111 | } 112 | } 113 | } 114 | return inputContent; 115 | } 116 | 117 | private static bool ContainsHtmlLabel(string text) 118 | { 119 | return (text.Contains("") || 120 | text.Contains("") || 121 | text.Contains("") || 122 | text.Contains("") || 123 | text.Contains("class=") || 124 | text.Contains("href=") || 125 | text.Contains("") || 126 | text.Contains("System.") || //.net程序集 127 | text.Contains("