I'm trying to translate array of text file using google translate but it doesn't translate, if i translate one file it does
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RavSoft.GoogleTranslator;
using System.Runtime.InteropServices;
using System.IO;
namespace Google_Translate
{
public partial class Form1 : Form
{
DirectoryInfo dir1;
FileInfo[] fi;
string[] splittedFiles;
string splitDirectory;
OpenFileDialog openFileDialog1;
FolderBrowserDialog fb;
Translator t ;
public Form1()
{
InitializeComponent();
splitDirectory = @"d:\testsplit";
openFileDialog1 = new OpenFileDialog();
button1.Enabled = false;
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
t = new Translator();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text == comboBox2.Text)
{
button1.Enabled = false;
MessageBox.Show("Cannot translate to the same language , select a different target or source language");
}
else
{
if (textBox1.Text == "")
{
button1.Enabled = false;
}
else
{
t.SourceLanguage = comboBox1.Text;
t.TargetLanguage = comboBox2.Text;
button1.Enabled = true;
}
}
if (comboBox1.Text == "" || comboBox2.Text == "" || comboBox1.Text == comboBox2.Text)
{
button1.Enabled = false;
}
else
{
if (textBox1.Text == "")
{
button1.Enabled = false;
}
else
{
t.SourceLanguage = comboBox1.Text;
开发者_开发百科 t.TargetLanguage = comboBox2.Text;
button1.Enabled = true;
}
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text == comboBox2.Text)
{
button1.Enabled = false;
MessageBox.Show("Cannot translate to the same language , select a different target or source language");
}
else
{
if (textBox1.Text == "")
{
button1.Enabled = false;
}
else
{
t.SourceLanguage = comboBox1.Text;
t.TargetLanguage = comboBox2.Text;
button1.Enabled = true;
}
}
if (comboBox1.Text == "" || comboBox2.Text == "" || comboBox1.Text == comboBox2.Text)
{
button1.Enabled = false;
}
else
{
if (textBox1.Text == "")
{
button1.Enabled = false;
}
else
{
t.SourceLanguage = comboBox1.Text;
t.TargetLanguage = comboBox2.Text;
button1.Enabled = true;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = t.SourceText;
t.Translate();
textBox2.Text = t.Translation.ToString();
textBox2.SelectionStart = 0;
textBox2.SelectionLength = 0;
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Select a text to translate";
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.FileName = null;
openFileDialog1.Filter = "Text File|*.txt";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
DialogResult result1 = openFileDialog1.ShowDialog();
string file1 = openFileDialog1.FileName;
if (result1 == DialogResult.OK)
{
SplitFiles(file1, 1024 * 32, splitDirectory);
dir1 = new DirectoryInfo(splitDirectory);
fi = dir1.GetFiles("*.txt");
for (int i = 0; i < fi.Length; i++)
{
t.SourceText = fi[i].ToString();
t.SourceLanguage = comboBox1.Text;
t.TargetLanguage = comboBox2.Text;
t.Translate();
}
CombineFiles(@"d:\testsplit\newFileTranslated.txt", splitDirectory); // to change string newlargeFile to a new file name or the same file the user will select if the same name change file name and which directory to
// save the file to using the current format sample in the CombineFile function \\
// to check why its not translating the small files inside the for loop before combining them \\
button1.Enabled = true;
}
if (result1 == DialogResult.Cancel)
{
if (file1 == "")
{
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
button1.Enabled = false;
}
else
{
if (comboBox1.Text == "" || comboBox2.Text == "" || comboBox1.Text == comboBox2.Text)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
}
private bool SplitFiles(string largeFile, int preferredSize, string savePath)
{
bool bRet = false;
if (File.Exists(largeFile))
{
StreamReader sr = null;
StreamWriter sw = null;
try
{
Directory.CreateDirectory(savePath);
sr = new StreamReader(largeFile);
char[] allchars = sr.ReadToEnd().ToCharArray();
sr.Close();
int i = 0;
int position = 0;
while (position < allchars.Length)
{
int l = Math.Min(preferredSize, allchars.Length - position);
sw = new StreamWriter(Path.Combine(savePath, "File" + i.ToString("D4") + ".txt"));
sw.AutoFlush = true;
char[] buffer = new Char[l];
Array.Copy(allchars, position, buffer, 0, l);
sw.Write(buffer);
position += l;
i++;
sw.Close();
bRet = true;
}
}
catch
{
bRet = false;
}
finally
{
sr.Close();
sr = null;
sw.Close();
sw = null;
}
}
return bRet;
}
private bool CombineFiles(string newLargeFile, string savePath)
{
bool bRet = false;
if (Directory.Exists(savePath))
{
StreamReader sr = null;
StreamWriter sw = null;
try
{
List<FileInfo> fList = new List<FileInfo>();
//fList.AddRange(new DirectoryInfo(savePath).GetFiles("*.txt").ToArray());
//fList = fList.OrderBy(a => a.Name).ToList();
fList.AddRange(new DirectoryInfo(savePath).GetFiles("*.txt").Where(a => a.FullName.ToLower().Equals(newLargeFile.ToLower()) == false).ToArray());
fList = fList.OrderBy(a => a.Name).ToList();
sw = new StreamWriter(newLargeFile);
sw.AutoFlush = true;
foreach (FileInfo fi in fList)
{
sr = new StreamReader(fi.FullName);
sw.Write(sr.ReadToEnd());
sr.Close();
}
}
catch
{
bRet = false;
}
finally
{
sr.Close();
sr = null;
sw.Close();
sw = null;
}
}
return bRet;
}
}
}
I changed now thw button2 click event using StreamReader so it will ReadToEnd(); each text file in the split directory.
But it doesn't work it doesn't readl to end the files.
Im getting error on the line: r = new StreamReader(fi[i].ToString());
The error say: FileNotFoundException: Could not find file 'D:\C-Sharp\Google_Translate\Google_Translate\Google_Translate\bin\Debug\File0000.txt'.
And the text files are in d:\testsplit
Cant figure out why its trying to read the files from the C-Sharp...directory instead from d:\testsplit
Here is the code again with the changed fragment:
if (result1 == DialogResult.OK)
{
SplitFiles(file1, 1024 * 32, splitDirectory);
dir1 = new DirectoryInfo(splitDirectory);
fi = dir1.GetFiles("*.txt");
StreamReader r;
for (int i = 0; i < fi.Length; i++)
{
r = new StreamReader(fi[i].ToString());
string y = r.ReadToEnd();
t.SourceLanguage = comboBox1.Text;
t.TargetLanguage = comboBox2.Text;
t.SourceText = y;
t.Translate();
}
CombineFiles(@"d:\testsplit\newFileTranslated.txt", splitDirectory); // to change string newlargeFile to a new file name or the same file the user will select if the same name change file name and which directory to
// save the file to using the current format sample in the CombineFile function \\
// to check why its not translating the small files inside the for loop before combining them \\
button1.Enabled = true;
}
I suppose you need to
change the working directory to the directory that contains the files
Environment.CurrentDirectory = @"d:\testsplit";
or prepend the path to the filenames (make them absolute
@"d:\testsplit\test.txt"
)
精彩评论