This commit is contained in:
sin365 2023-10-16 14:35:56 +08:00
commit 50800699de
8 changed files with 358 additions and 0 deletions

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="SrcData\" />
</ItemGroup>
<ItemGroup>
<None Update="说明.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_LastSelectedProfileId>F:\Sin365\AutoRranslateTextToCsv\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoRranslateTextToCsv", "AutoRranslateTextToCsv.csproj", "{6F081B40-B803-4741-8A05-CB49F9095495}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6F081B40-B803-4741-8A05-CB49F9095495}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F081B40-B803-4741-8A05-CB49F9095495}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F081B40-B803-4741-8A05-CB49F9095495}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F081B40-B803-4741-8A05-CB49F9095495}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E41C24E3-4BE7-4C4F-934E-6DE391955CF7}
EndGlobalSection
EndGlobal

61
FileHelper.cs Normal file
View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AutoRranslateTextToCsv
{
public class FileHelper
{
public static bool LoadFile(string FilePath, out byte[] data)
{
using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
{
try
{
byte[] buffur = new byte[fs.Length];
fs.Read(buffur, 0, (int)fs.Length);
fs.Close();
data = buffur;
return true;
}
catch (Exception ex)
{
data = null;
return false;
}
}
}
public static string[] GetDirFile(string Path)
{
return Directory.GetFiles(Path);
}
public static byte[] String2Byte(string value)
{
return Encoding.Default.GetBytes(value);
}
public static bool SaveFile(string FilePath, byte[] buffer)
{
try
{
//创建一个文件流
using (FileStream wfs = new FileStream(FilePath, FileMode.Create))
{
//将byte数组写入文件中
wfs.Write(buffer, 0, buffer.Length);
//所有流类型都要关闭流,否则会出现内存泄露问题
wfs.Close();
return true;
}
}
catch
{
return false;
}
}
}
}

183
Program.cs Normal file
View File

@ -0,0 +1,183 @@
using System.Text;
namespace AutoRranslateTextToCsv
{
internal class Program
{
static string loc = Path.GetDirectoryName(AppContext.BaseDirectory) + "\\";
const string InDir = "Input";
const string OutDir = "Out";
const string SrcDataFile = "_TextDictionary.csv";
const string Ver = "0.1";
static Dictionary<string, string> mDictSrcData;
static void Main(string[] args)
{
string title = $"AutoRranslateTextToCsv Ver.{Ver} By axibug.com";
Console.Title = title;
Console.WriteLine(title);
if (!Directory.Exists(loc + InDir))
{
Console.WriteLine("Input文件不存在");
Console.ReadLine();
return;
}
if (!Directory.Exists(loc + OutDir))
{
Console.WriteLine("Out文件不存在");
Console.ReadLine();
return;
}
if (!File.Exists(loc + SrcDataFile))
{
Console.WriteLine($"{SrcDataFile}文件不存在");
Console.ReadLine();
return;
}
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Console.WriteLine($"开始加载译文数据源……");
if (!LoadCsv(loc + SrcDataFile, out Dictionary<string, string> _dictSrcData))
{
Console.WriteLine($"译文数据源加载失败");
Console.ReadLine();
return;
}
mDictSrcData = _dictSrcData;
Console.WriteLine($"译文数据源加载成功,共{_dictSrcData.Count}条数据");
string[] files = FileHelper.GetDirFile(loc + InDir);
Console.WriteLine($"Input中共{files.Length}个文件,是否处理? (y/n)");
string yn = Console.ReadLine();
if (yn.ToLower() != "y")
return;
int index = 0;
int errcount = 0;
for (int i = 0; i < files.Length; i++)
{
string FileName = files[i].Substring(files[i].LastIndexOf("\\"));
if (!FileName.ToLower().Contains(".csv"))
{
continue;
}
index++;
Console.WriteLine($">>>>>>>>>>>>>>开始处理 第{index}个文件 {FileName}<<<<<<<<<<<<<<<<<<<");
if (DoReplaceRranslateFile(files[i], out string[] TempArr,out int DoneIndex))
{
string newfileName = FileName;
string outstring = loc + OutDir + "\\" + newfileName;
File.WriteAllLines(outstring, TempArr);
Console.WriteLine($">>>>>>>>>>>>>>成功处理 第{index}个:{outstring} | 其中成功处理文本{DoneIndex}个");
}
else
{
errcount++;
Console.WriteLine($">>>>>>>>>>>>>>处理失败 第{index}个");
}
}
Console.WriteLine($"已处理{files.Length}个文件,其中{errcount}个失败");
Console.ReadLine();
}
public static bool LoadCsv(string path, out Dictionary<string, string> _dictSrcData)
{
_dictSrcData = new Dictionary<string, string>();
using (StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312")))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] values = line.Split(',');
if (values.Length >= 2)
{
string k = null;
string v = null;
k = values[0];
v = values[1];
if (string.IsNullOrEmpty(k) || string.IsNullOrEmpty(v))
continue;
if (string.Equals(k,v))
continue;
_dictSrcData[k] = v;
}
}
}
return true;
}
public static bool DoReplaceRranslateFile(string path,out string[] TempArr,out int DoneNum)
{
DoneNum = 0;
try
{
string[] StrArr = File.ReadAllLines(path, Encoding.GetEncoding("shift-jis"));
TempArr = new string[StrArr.Length];
for (int i = 0; i < StrArr.Length; i++)
{
string line = StrArr[i];
if (i == 0)
{
TempArr[i] = line;
continue;
}
string[] valueArr = StrArr[i].Split("\t");
if (valueArr.Length >= 2 && GetRranslateText(valueArr[2], out string Resultline))
{
string newline = "";
for (int j = 0; j < valueArr.Length; j++)
{
if (j == 2)
newline += EncodingConvert(Encoding.GetEncoding("gb2312"), Encoding.GetEncoding("shift-jis"), Resultline);
else
newline += valueArr[j];
if(j < valueArr.Length -1)
newline += "\t";
DoneNum++;
}
line = newline;
}
TempArr[i] = line;
}
return true;
}
catch(Exception ex)
{
TempArr = null;
return false;
}
}
public static string EncodingConvert(Encoding src, Encoding dst, string text)
{
var bytes = src.GetBytes(text);
bytes = Encoding.Convert(src, dst, bytes);
return dst.GetString(bytes);
}
public static bool GetRranslateText(string line, out string Resultline)
{
if (mDictSrcData.ContainsKey(line))
{
Resultline = mDictSrcData[line];
return true;
}
Resultline = null;
return false;
}
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net7.0\publish\win-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net7.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<History>True|2023-10-16T06:28:28.4099321Z;True|2023-10-16T14:28:04.3437847+08:00;True|2023-10-16T14:26:50.3929900+08:00;False|2023-10-16T14:26:22.6712614+08:00;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>

31
README.md Normal file
View File

@ -0,0 +1,31 @@
译文批量填CSV表转换工具 by 皓月云 axibug.com
服务于ReFrontier魔改版本下 FrontierTextTool 导出工具的译文批量处理
使用方法:
1._TextDictionary.csv
该文件为原文和译文。在本CSV中填入文本。
程序将读取作为字典之用。
*本表固定文字编码为GB2312
2.Input文件夹中放入 需要处理的文件
如指针|日文 的csv文件
mhfdat_01.csv
*使用中不要破坏表列结构,默认即可
*读取本表文字编码固定为shift-jis
3.运行 AutoRranslateTextToCsv.exe
即可输入处理文件到Out文件夹
按照_TextDictionary.csv 替换文本列字段
*输出表文字编码固定为shift-jis