背景: 我們項目一開始的所有提示都是中文,後來要做國際化。發現項目中的帶雙引號的中文居然有 2.3 w 多條!!!簡直讓人欲哭無淚... 如果使用人工改的話,首先不說正確率了。光是效率都是難難難。所以發揮了自己的才能寫了一個自動化工具。 思路: 首選讀取項目文件夾下的所有文件路徑 篩選路徑文件尾碼. ...
背景:
我們項目一開始的所有提示都是中文,後來要做國際化。發現項目中的帶雙引號的中文居然有 2.3 w 多條!!!簡直讓人欲哭無淚...
如果使用人工改的話,首先不說正確率了。光是效率都是難難難。所以發揮了自己的才能寫了一個自動化工具。
思路:
- 首選讀取項目文件夾下的所有文件路徑
- 篩選路徑文件尾碼.只對.cs或自定義的文件操作讀取
- 使用正則表達式進行查詢並處理為數組
- 調用免費的百度翻譯api進行任意類型的翻譯
- 使用replace進行替換字元串,並保存
準備工作:
1,百度智能雲領取免費的api介面
2,創建應用
3,調試api
代碼實現:
以上準備工作就緒後,代碼就比較容易實現了,完整控制台代碼如下:
Program.cs
using Batch_translation; using System.Text.RegularExpressions; Console.WriteLine("請按下任意鍵進行批量翻譯!"); Console.ReadLine(); string strFolderPath = "you FolderPath";//你的項目文件夾路徑 string[] files = Directory.GetFiles(strFolderPath, "*", SearchOption.AllDirectories); // 獲取文件夾下所有文件路徑,包括子文件夾 string[] subFolders = Directory.GetDirectories(strFolderPath, "*", SearchOption.AllDirectories); // 獲取所有子文件夾路徑 foreach (var filePath in files) { //業務邏輯 if (filePath.EndsWith(".cs")) { BatchTran(filePath);//獲取文件內容併進行轉換操作 } } foreach (var folder in subFolders) { foreach (var filePath in Directory.GetFiles(folder)) { //業務邏輯 if (filePath.EndsWith(".cs")) { BatchTran(filePath);//獲取文件內容併進行轉換操作 } } } Console.WriteLine("轉化完成"); void BatchTran(string filePath) { /* 核心邏輯: 首選讀取項目文件夾下的所有文件路徑 篩選路徑文件尾碼.只對.cs或自定義的文件操作讀取 使用正則表達式進行查詢並處理為數組 調用免費的百度翻譯api進行任意類型的翻譯 使用replace進行替換字元串,並保存 */ BaiDuApi baiDuApi = new BaiDuApi("you apikey", "you secretKey");//百度api 初始化 string content = File.ReadAllText(filePath);//讀取文本 string pattern = @"("".*[\u4E00-\u9FA5]+.*[\u4E00-\u9FA5]+.*"")";//定義所需要的正則表達式(ps:此正則表示查詢英文雙引號下的中文) Regex myRegex = new Regex(pattern); MatchCollection collection = myRegex.Matches(content); foreach (Match match in collection) { var oldStr = ""; oldStr = match.Value;//.Substring(0, match.Value.Length - 1) var newStr = baiDuApi.Translate(oldStr); if (!string.IsNullOrEmpty(newStr)) content = content.Replace(oldStr, newStr); } if (collection.Count > 0) // 將修改後的內容寫入到文件中 File.WriteAllText(filePath, content); }
BaiDuApi.cs
using System; using System.IO; using Newtonsoft.Json; using RestSharp; namespace Batch_translation { public class BaiDuApi { private readonly string apiKey; private readonly string secretKey; public BaiDuApi(string apiKey, string secretKey) { this.apiKey = apiKey; this.secretKey = secretKey; } public string Translate(string sourceText) { var token = GetAccessToken(apiKey, secretKey); if (token == "") return ""; var client = new RestClient($"https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token={token}"); var request = new RestRequest("", Method.Post); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Accept", "application/json"); // 定義一個匿名類型對象 var data = new { from = "zh", to = "en", q = sourceText }; // 將對象序列化成 JSON 字元串 string body = JsonConvert.SerializeObject(data); request.AddParameter("application/json", body, ParameterType.RequestBody); var response = client.Execute(request); if (response.Content == null) { return ""; } var dataResult = JsonConvert.DeserializeObject<BaiDuMode>(response.Content); if (dataResult.result != null) { if (dataResult.result.trans_result != null && dataResult.result.trans_result.Count > 0) { return dataResult.result.trans_result[0].dst; } } return sourceText; } /** * 使用 AK,SK 生成鑒權簽名(Access Token) * @return 鑒權簽名信息(Access Token) */ static string GetAccessToken(string API_KEY, string SECRET_KEY) { var client = new RestClient($"https://aip.baidubce.com/oauth/2.0/token"); var request = new RestRequest("", Method.Post); request.AddParameter("grant_type", "client_credentials"); request.AddParameter("client_id", API_KEY); request.AddParameter("client_secret", SECRET_KEY); var response = client.Execute(request); Console.WriteLine(response.Content); if (response.Content == null) return ""; var result = JsonConvert.DeserializeObject<dynamic>(response.Content); return result.access_token.ToString(); } } }
結語:
在每次編碼中成長,提升自己的思想。這隻是一個很簡陋的代碼工具,如有好的建議也可以評論區討論。感謝觀看。
從前慢,車馬慢。 一生只愛一個人。