124 lines
4.4 KiB
C#
124 lines
4.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Text.RegularExpressions;
|
||
|
||
|
||
// 通过正则提取所有代码中的中文字符串
|
||
// 将中文字符串分行写入到文件
|
||
// 通过有道翻译进行翻译(https://fanyi.youdao.com/index.html#/),可每次复制不多于5000的内容进行翻译
|
||
// 翻译内容保存在translation.xx.txt中,xx即语言选项
|
||
// 通过resxhelper将中文及对应翻译写入到对应资源文件
|
||
|
||
namespace i18n_formatter
|
||
{
|
||
class Program
|
||
{
|
||
static HashSet<string> needI18n = new HashSet<string>();
|
||
static void Main()
|
||
{
|
||
ResxHelper.GenerateResxFileForTranslationsInCurrentDirectory();
|
||
}
|
||
|
||
// call this first
|
||
static void ExtractChineseStrings()
|
||
{
|
||
string directory = "samples";
|
||
|
||
// 遍历给定目录,提取代码文件中所有中文字符串,并保存到HashSet中
|
||
WalkDirectory(new DirectoryInfo(directory));
|
||
|
||
// 将HashSet写入到json文件
|
||
//string json = CreateJsonFromHashSet(needI18n);
|
||
|
||
//File.WriteAllText("i18n.json", json);
|
||
string result = CreateRawTextFromHashSet(needI18n);
|
||
|
||
File.WriteAllText("raw.text", result);
|
||
// 之后要手动进行翻译了
|
||
// 读取翻译结果,将翻译结果写入到对应的资源文件中
|
||
}
|
||
|
||
static string CreateJsonFromHashSet(HashSet<string> set)
|
||
{
|
||
string json = "[";
|
||
foreach (var item in set)
|
||
{
|
||
json += "\"" + item + "\",";
|
||
}
|
||
json = json.TrimEnd(',') + "]";
|
||
return json;
|
||
}
|
||
|
||
static string CreateRawTextFromHashSet(HashSet<string> set)
|
||
{
|
||
string result = "";
|
||
string splitter = "";
|
||
foreach (var item in set)
|
||
{
|
||
result += splitter + item;
|
||
splitter = "\r\n";
|
||
}
|
||
return result;
|
||
}
|
||
|
||
static HashSet<string> ReadHardedcodedStrings(string fileContent, string filePath)
|
||
{
|
||
HashSet<string> result = new HashSet<string>();
|
||
|
||
// Define a regular expression to match string literals
|
||
Regex stringLiteralRegex = new Regex("\"(.*?)\"", RegexOptions.Compiled);
|
||
|
||
// Find all matches of string literals in the file content
|
||
MatchCollection matches = stringLiteralRegex.Matches(fileContent);
|
||
|
||
Console.WriteLine(filePath);
|
||
// Display all the matched string literals
|
||
foreach (Match match in matches)
|
||
{
|
||
if (!CheckChineses(match.Groups[1].Value)) continue;
|
||
needI18n.Add(match.Groups[1].Value);
|
||
result.Add(match.Groups[1].Value);
|
||
//Console.WriteLine(match.Groups[1].Value);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
static bool CheckChineses(string sample)
|
||
{
|
||
string pattern = @"[\u4E00-\u9FFF]"; // The pattern to match Chinese characters
|
||
|
||
// Create a Regex object with the pattern and check if there is a match
|
||
Regex regex = new Regex(pattern);
|
||
Match match = regex.Match(sample);
|
||
return match.Success;
|
||
}
|
||
|
||
static void WalkDirectory(DirectoryInfo directory)
|
||
{
|
||
// Process files in the current directory
|
||
foreach (FileInfo file in directory.GetFiles("*.cs"))
|
||
{
|
||
if (file.Name.ToUpper().Contains(".DESIGNER."))
|
||
continue;
|
||
string fileContent = File.ReadAllText(file.FullName);
|
||
HashSet<string> hardcodedChineseStrings = ReadHardedcodedStrings(fileContent, file.FullName);
|
||
foreach(string hcString in hardcodedChineseStrings)
|
||
{
|
||
Console.WriteLine("replacing" + hcString);
|
||
Console.WriteLine(fileContent.Contains(hcString));
|
||
fileContent = fileContent.Replace("\"" + hcString + "\"", "LocalizationManager.GetString(" + "\"" + hcString + "\"" + ")");
|
||
}
|
||
File.WriteAllText("D:/DevWorkspace/i18n_formatter/bin/Debug/output/"+ file.Name, fileContent);
|
||
}
|
||
|
||
// Recursively process subdirectories
|
||
foreach (DirectoryInfo subdirectory in directory.GetDirectories())
|
||
{
|
||
WalkDirectory(subdirectory);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|