Skip to content

Commit 2fa37ff

Browse files
committed
支持自定义输入目录和修改翻译网站
1 parent 8044ff6 commit 2fa37ff

8 files changed

+1071
-172
lines changed

EasyTransaction/BatchTran.cs

+351-17
Large diffs are not rendered by default.

EasyTransaction/EasyTransaction.csproj

+52
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
<AssemblyName>EasyTransaction</AssemblyName>
1212
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<IsWebBootstrapper>false</IsWebBootstrapper>
15+
<PublishUrl>publish\</PublishUrl>
16+
<Install>true</Install>
17+
<InstallFrom>Disk</InstallFrom>
18+
<UpdateEnabled>false</UpdateEnabled>
19+
<UpdateMode>Foreground</UpdateMode>
20+
<UpdateInterval>7</UpdateInterval>
21+
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
22+
<UpdatePeriodically>false</UpdatePeriodically>
23+
<UpdateRequired>false</UpdateRequired>
24+
<MapFileExtensions>true</MapFileExtensions>
25+
<ApplicationRevision>1</ApplicationRevision>
26+
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
27+
<UseApplicationTrust>false</UseApplicationTrust>
28+
<PublishWizardCompleted>true</PublishWizardCompleted>
29+
<BootstrapperEnabled>true</BootstrapperEnabled>
1430
</PropertyGroup>
1531
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1632
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -31,6 +47,21 @@
3147
<ErrorReport>prompt</ErrorReport>
3248
<WarningLevel>4</WarningLevel>
3349
</PropertyGroup>
50+
<PropertyGroup>
51+
<ApplicationIcon>favicon.ico</ApplicationIcon>
52+
</PropertyGroup>
53+
<PropertyGroup>
54+
<ManifestCertificateThumbprint>748CA883CF8CDDCAD0EAA17E94B93E15011EF34F</ManifestCertificateThumbprint>
55+
</PropertyGroup>
56+
<PropertyGroup>
57+
<ManifestKeyFile>EasyTransaction_TemporaryKey.pfx</ManifestKeyFile>
58+
</PropertyGroup>
59+
<PropertyGroup>
60+
<GenerateManifests>true</GenerateManifests>
61+
</PropertyGroup>
62+
<PropertyGroup>
63+
<SignManifests>true</SignManifests>
64+
</PropertyGroup>
3465
<ItemGroup>
3566
<Reference Include="System" />
3667
<Reference Include="System.Core" />
@@ -67,6 +98,7 @@
6798
<AutoGen>True</AutoGen>
6899
<DependentUpon>Resources.resx</DependentUpon>
69100
</Compile>
101+
<None Include="EasyTransaction_TemporaryKey.pfx" />
70102
<None Include="Properties\Settings.settings">
71103
<Generator>SettingsSingleFileGenerator</Generator>
72104
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -77,6 +109,26 @@
77109
<DesignTimeSharedInput>True</DesignTimeSharedInput>
78110
</Compile>
79111
</ItemGroup>
112+
<ItemGroup>
113+
<Content Include="favicon.ico" />
114+
</ItemGroup>
115+
<ItemGroup>
116+
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
117+
<Visible>False</Visible>
118+
<ProductName>Microsoft .NET Framework 4 %28x86 和 x64%29</ProductName>
119+
<Install>true</Install>
120+
</BootstrapperPackage>
121+
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
122+
<Visible>False</Visible>
123+
<ProductName>.NET Framework 3.5 SP1</ProductName>
124+
<Install>false</Install>
125+
</BootstrapperPackage>
126+
<BootstrapperPackage Include="Microsoft.Windows.Installer.4.5">
127+
<Visible>False</Visible>
128+
<ProductName>Windows Installer 4.5</ProductName>
129+
<Install>true</Install>
130+
</BootstrapperPackage>
131+
</ItemGroup>
80132
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
81133
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
82134
Other similar extension points exist, see Microsoft.Common.targets.

EasyTransaction/FrmMain.Designer.cs

+280-106
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EasyTransaction/FrmMain.cs

+181-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Data;
5+
using System.Diagnostics;
56
using System.Drawing;
67
using System.IO;
78
using System.IO.Compression;
@@ -14,27 +15,98 @@
1415
namespace EasyTransaction
1516
{
1617
public partial class FrmMain : Form
17-
{
18-
Uri googleCNURL = new Uri("http://translate.google.cn/");
18+
{
1919
TranslateTool tool;
2020
BatchTran tran;
21+
Stopwatch workTimeWatch;
22+
2123
public FrmMain()
2224
{
2325
InitializeComponent();
24-
tool = new TranslateTool(wb);
25-
tran = new BatchTran(tool);
26+
tool = new TranslateTool(wb, "http://translate.google.cn" );
27+
tran = new BatchTran(tool)
28+
{
29+
FromLanguage = "en",
30+
ToLanguage = "zh-Cn",
31+
};
2632
tran.BatchMessagePushed += Tran_BatchMessagePushed;
33+
tran.ProcessChanged += Tran_ProcessChanged;
34+
tran.Completed += Tran_Completed;
2735
IsReady(false);
36+
txtResult.MaxLength = int.MaxValue;
37+
statusStrip1.Visible = false;
2838

2939
}
3040

41+
private void Tran_Completed(WorkProcessEventArgs args)
42+
{
43+
this.Invoke(new Action(() =>
44+
{
45+
DoWork(false);
46+
}));
47+
}
48+
49+
/// <summary>
50+
/// 翻译进度变化
51+
/// </summary>
52+
/// <param name="args"></param>
53+
private void Tran_ProcessChanged(WorkProcessEventArgs args)
54+
{
55+
if (workTimeWatch == null)
56+
{
57+
workTimeWatch = new Stopwatch();
58+
workTimeWatch.Start();
59+
}
60+
if (args.SumCount == (args.CompletedCount + args.FailCount))
61+
{
62+
workTimeWatch.Stop();
63+
}
64+
else if (args.CompletedCount == 0 && args.FailCount == 0)
65+
{
66+
workTimeWatch.Restart();
67+
}
68+
this.Invoke(new Action(() =>
69+
{
70+
toolProBar.Minimum = 0;
71+
toolProBar.Maximum = args.SumCount;
72+
toolProBar.Value = args.CompletedCount;
73+
lblProcess.Text = string.Format("{0:P0}", args.CompletionRatio);
74+
var info = string.Format("翻译进度:已完成{0:P2},{1:N0}/{2:N0},失败数:{3}.", args.CompletionRatio, args.CompletedCount, args.SumCount, args.FailCount);
75+
if (args.CompletedCount > 0)
76+
{
77+
info += string.Format("速度:{0:N2}ms/行.", workTimeWatch.ElapsedMilliseconds / args.CompletedCount);
78+
}
79+
if (!string.IsNullOrWhiteSpace(args.Message))
80+
{
81+
info += "消息:" + args.Message;
82+
}
83+
toolLableStatus.Text = info;
84+
if (args.CompletedCount == 0)
85+
{
86+
lkSaveFile.Text = "Open:" + Path.GetFileName(tran.GetSaveFileName());
87+
}
88+
}));
89+
}
90+
91+
/// <summary>
92+
/// 翻译出来消息接收
93+
/// </summary>
94+
/// <param name="args"></param>
3195
private void Tran_BatchMessagePushed(MessageEventArgs args)
3296
{
33-
lblBatchResult.Invoke(new Action(() =>
97+
this.Invoke(new Action(() =>
3498
{
35-
lblBatchResult.Text = "翻译进度:" + args.Message;
99+
showLog(args.Message);
36100
}));
37101
}
102+
/// <summary>
103+
/// 显示进度信息
104+
/// </summary>
105+
/// <param name="msg"></param>
106+
private void showLog(string msg)
107+
{
108+
txtResult.AppendText(string.Format("{0:HH:mm:ss}:{1}\r\n", DateTime.Now, msg));
109+
}
38110

39111
private void Form1_Load(object sender, EventArgs e)
40112
{
@@ -43,6 +115,11 @@ private void Form1_Load(object sender, EventArgs e)
43115
private void wb_Navigated(object sender, WebBrowserNavigatedEventArgs e)
44116
{
45117
gpWeb.Text = wb.DocumentTitle;
118+
showLog("页面地址:" + wb.Document.Url.ToString());
119+
if (txtUrl.Focused == false)
120+
{
121+
txtUrl.Text = wb.Document.Url.ToString();
122+
}
46123
}
47124
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
48125
{
@@ -64,7 +141,8 @@ private void IsReady(bool yes)
64141
private void btnRunTest_Click(object sender, EventArgs e)
65142
{
66143
var text = txtTestText.Text.Trim();
67-
txtTestResult.Text = tool.Translate(text);
144+
var result = tool.Translate(text, txtFromLan.Text.Trim(), txtToLan.Text.Trim());
145+
showLog(string.Format("测试结果:\r\n 成功:{0}\r\n,结果:{1}\r\n 错误信息:{2} ", result.IsSuccess, result.Result, result.ErrorMessage));
68146
}
69147

70148
private void btnSelectFile_Click(object sender, EventArgs e)
@@ -81,11 +159,20 @@ private void btnSelectFile_Click(object sender, EventArgs e)
81159

82160
private void btnAction_Click(object sender, EventArgs e)
83161
{
84-
if ((btnAction.Tag as string) == "runing")
162+
DoWork((btnAction.Tag as string) != "runing");
163+
}
164+
165+
private void DoWork(bool needStart)
166+
{
167+
if (!needStart)
85168
{
86169
tran.Stop();
87170
btnAction.Tag = "stoped";
88171
btnAction.Text = "开始";
172+
statusStrip1.Visible = true;
173+
btnSelectFile.Enabled = true;
174+
txtFromLan.Enabled = true;
175+
txtToLan.Enabled = true;
89176
}
90177
else
91178
{
@@ -96,15 +183,101 @@ private void btnAction_Click(object sender, EventArgs e)
96183
}
97184
try
98185
{
186+
tran.FromLanguage = txtFromLan.Text.Trim();
187+
tran.ToLanguage = txtToLan.Text.Trim();
188+
tran.Interval = (int)numTranslateInv.Value;
99189
tran.Start(openFileDialog1.FileName);
100190
btnAction.Tag = "runing";
101191
btnAction.Text = "停止";
192+
statusStrip1.Visible = true;
193+
lblProcess.Text = "0.00%";
194+
btnSelectFile.Enabled = false;
195+
lblProcess.Visible = true;
196+
txtFromLan.Enabled = false;
197+
txtToLan.Enabled = false;
102198
}
103199
catch (Exception ex)
104200
{
105201
MessageBox.Show(ex.Message);
106202
}
107203
}
108204
}
205+
206+
private void numTranslateInv_ValueChanged(object sender, EventArgs e)
207+
{
208+
this.tran.Interval = (int)numTranslateInv.Value;
209+
}
210+
211+
private void lkSaveFile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
212+
{
213+
var filename = tran.GetSaveFileName();
214+
if (string.IsNullOrWhiteSpace(filename))
215+
{
216+
return;
217+
}
218+
Process.Start(filename);
219+
}
220+
221+
222+
private void txtResultSaveDir_Leave(object sender, EventArgs e)
223+
{
224+
if (checkSavePath())
225+
{
226+
tran.SaveDir = txtResultSaveDir.Text.Trim();
227+
}else
228+
{
229+
txtResultSaveDir.Focus();
230+
txtResultSaveDir.SelectAll();
231+
}
232+
}
233+
234+
/// <summary>
235+
/// 检测存储位置是否存在
236+
/// </summary>
237+
private bool checkSavePath()
238+
{
239+
var dir = txtResultSaveDir.Text.Trim();
240+
if (Directory.Exists(dir))
241+
{
242+
return true;
243+
}
244+
if (DialogResult.No == MessageBox.Show("翻译结果存储目录不存在,是否自动创建", "提示", MessageBoxButtons.YesNo))
245+
{
246+
return false;
247+
}
248+
try
249+
{
250+
Directory.CreateDirectory(dir);
251+
}
252+
catch (Exception ex)
253+
{
254+
MessageBox.Show("创建目录失败," + ex.Message + ",可手工新建目录");
255+
return false;
256+
}
257+
258+
return true;
259+
}
260+
261+
private void btnChangeURL_Click(object sender, EventArgs e)
262+
{
263+
var url = txtUrl.Text.Trim();
264+
if (url == "")
265+
{
266+
return;
267+
}
268+
if (!url.StartsWith("https://") && !url.StartsWith("http://"))
269+
{
270+
url = "http://" + url;
271+
}
272+
try
273+
{
274+
var u = new Uri(url);
275+
tool.GoogleTranslateWeb = u;
276+
}
277+
catch (Exception ex)
278+
{
279+
MessageBox.Show(ex.Message);
280+
}
281+
}
109282
}
110283
}

0 commit comments

Comments
 (0)