翻译大量文本数据的最佳方法是什么?

发布于 2024-08-25 13:41:57 字数 1539 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

甜警司 2024-09-01 13:41:57

在将语言翻译与 XMPP 聊天服务器集成时,我必须解决同样的问题。我将有效负载(我需要翻译的文本)划分为完整句子的较小子集。

我不记得确切的数字,但通过 Google 的基于 REST 的翻译网址,我进行了翻译一组完整的句子,总共少于(或等于)1024 个字符,因此大段落将导致多次翻译服务调用。

I had to solve the same problem when integrating language translation with an XMPP chat server. I partitioned my payload (the text I needed to translate) into smaller subsets of complete sentences.

I can’t recall the exact number, but with Google's REST-based translation URL, I translated a set of completed sentences that collectively had a total of less than (or equal to) 1024 characters, so a large paragraph would result in multiple translation service calls.

冷默言语 2024-09-01 13:41:57

将大文本分解为标记化字符串,然后通过循环将每个标记传递给翻译器。将翻译后的输出存储在一个数组中,一旦所有标记都被翻译并存储在数组中,将它们放回一起,您将拥有一个完全翻译的文档。

只是为了证明一点,我把它放在一起:)它的边缘很粗糙,但它可以处理整个大量文本,并且它在翻译准确性方面与 Google 一样好,因为它使用谷歌API。我使用此代码并单击一个按钮处理了 Apple 的整个 2005 年 SEC 10-K 文件(花费了大约 45 分钟)。

结果与一次复制一个句子并将其粘贴到谷歌翻译中所得到的结果基本相同。它并不完美(结尾标点符号不准确,而且我没有逐行写入文本文件),但它确实显示了概念证明。如果您更多地使用正则表达式,它可能会有更好的标点符号。

Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1

    Dim file As New String("Translate Me.txt")
    Dim lineCount As Integer = countLines()

    Private Function countLines()

        If IO.File.Exists(file) Then

            Dim reader As New StreamReader(file)
            Dim lineCount As Integer = Split(reader.ReadToEnd.Trim(), Environment.NewLine).Length
            reader.Close()
            Return lineCount

        Else

            MsgBox(file + " cannot be found anywhere!", 0, "Oops!")

        End If

        Return 1

    End Function

    Private Sub translateText()

        Dim lineLoop As Integer = 0
        Dim currentLine As String
        Dim currentLineSplit() As String
        Dim input1 As New StreamReader(file)
        Dim input2 As New StreamReader(file)
        Dim filePunctuation As Integer = 1
        Dim linePunctuation As Integer = 1

        Dim delimiters(3) As Char
        delimiters(0) = "."
        delimiters(1) = "!"
        delimiters(2) = "?"

        Dim entireFile As String
        entireFile = (input1.ReadToEnd)

        For i = 1 To Len(entireFile)
            If Mid$(entireFile, i, 1) = "." Then filePunctuation += 1
        Next

        For i = 1 To Len(entireFile)
            If Mid$(entireFile, i, 1) = "!" Then filePunctuation += 1
        Next

        For i = 1 To Len(entireFile)
            If Mid$(entireFile, i, 1) = "?" Then filePunctuation += 1
        Next

        Dim sentenceArraySize = filePunctuation + lineCount

        Dim sentenceArrayCount = 0
        Dim sentence(sentenceArraySize) As String
        Dim sentenceLoop As Integer

        While lineLoop < lineCount

            linePunctuation = 1

            currentLine = (input2.ReadLine)

            For i = 1 To Len(currentLine)
                If Mid$(currentLine, i, 1) = "." Then linePunctuation += 1
            Next

            For i = 1 To Len(currentLine)
                If Mid$(currentLine, i, 1) = "!" Then linePunctuation += 1
            Next

            For i = 1 To Len(currentLine)
                If Mid$(currentLine, i, 1) = "?" Then linePunctuation += 1
            Next

            currentLineSplit = currentLine.Split(delimiters)
            sentenceLoop = 0

            While linePunctuation > 0

                Try

                    Dim trans As New Google.API.Translate.TranslateClient("")
                    sentence(sentenceArrayCount) = trans.Translate(currentLineSplit(sentenceLoop), Google.API.Translate.Language.English, Google.API.Translate.Language.German, Google.API.Translate.TranslateFormat.Text)
                    sentenceLoop += 1
                    linePunctuation -= 1
                    sentenceArrayCount += 1

                Catch ex As Exception

                    sentenceLoop += 1
                    linePunctuation -= 1

                End Try

            End While

            lineLoop += 1

        End While

        Dim newFile As New String("Translated Text.txt")
        Dim outputLoopCount As Integer = 0

        Using output As StreamWriter = New StreamWriter(newFile)

            While outputLoopCount < sentenceArraySize

                output.Write(sentence(outputLoopCount) + ". ")

                outputLoopCount += 1

            End While

        End Using

        input1.Close()
        input2.Close()

    End Sub

    Private Sub translateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles translateButton.Click

        translateText()

    End Sub

End Class

Break your big text into tokenized strings, and then pass each token through the translator via a loop. Store the translated output in an array and once all tokens are translated and stored in the array, put them back together and you will have a completely translated document.

Just to prove a point, I threw this together :) It is rough around the edges, but it will handle a whole lot of text and it does just as good as Google for translation accuracy because it uses the Google API. I processed Apple's entire 2005 SEC 10-K filing with this code and the click of one button (took about 45 minutes).

The result was basically identical to what you would get if you copied and pasted one sentence at a time into Google Translate. It isn't perfect (ending punctuation is not accurate and I didn't write to the text file line by line), but it does show a proof of concept. It could have better punctuation if you worked with Regex some more.

Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1

    Dim file As New String("Translate Me.txt")
    Dim lineCount As Integer = countLines()

    Private Function countLines()

        If IO.File.Exists(file) Then

            Dim reader As New StreamReader(file)
            Dim lineCount As Integer = Split(reader.ReadToEnd.Trim(), Environment.NewLine).Length
            reader.Close()
            Return lineCount

        Else

            MsgBox(file + " cannot be found anywhere!", 0, "Oops!")

        End If

        Return 1

    End Function

    Private Sub translateText()

        Dim lineLoop As Integer = 0
        Dim currentLine As String
        Dim currentLineSplit() As String
        Dim input1 As New StreamReader(file)
        Dim input2 As New StreamReader(file)
        Dim filePunctuation As Integer = 1
        Dim linePunctuation As Integer = 1

        Dim delimiters(3) As Char
        delimiters(0) = "."
        delimiters(1) = "!"
        delimiters(2) = "?"

        Dim entireFile As String
        entireFile = (input1.ReadToEnd)

        For i = 1 To Len(entireFile)
            If Mid$(entireFile, i, 1) = "." Then filePunctuation += 1
        Next

        For i = 1 To Len(entireFile)
            If Mid$(entireFile, i, 1) = "!" Then filePunctuation += 1
        Next

        For i = 1 To Len(entireFile)
            If Mid$(entireFile, i, 1) = "?" Then filePunctuation += 1
        Next

        Dim sentenceArraySize = filePunctuation + lineCount

        Dim sentenceArrayCount = 0
        Dim sentence(sentenceArraySize) As String
        Dim sentenceLoop As Integer

        While lineLoop < lineCount

            linePunctuation = 1

            currentLine = (input2.ReadLine)

            For i = 1 To Len(currentLine)
                If Mid$(currentLine, i, 1) = "." Then linePunctuation += 1
            Next

            For i = 1 To Len(currentLine)
                If Mid$(currentLine, i, 1) = "!" Then linePunctuation += 1
            Next

            For i = 1 To Len(currentLine)
                If Mid$(currentLine, i, 1) = "?" Then linePunctuation += 1
            Next

            currentLineSplit = currentLine.Split(delimiters)
            sentenceLoop = 0

            While linePunctuation > 0

                Try

                    Dim trans As New Google.API.Translate.TranslateClient("")
                    sentence(sentenceArrayCount) = trans.Translate(currentLineSplit(sentenceLoop), Google.API.Translate.Language.English, Google.API.Translate.Language.German, Google.API.Translate.TranslateFormat.Text)
                    sentenceLoop += 1
                    linePunctuation -= 1
                    sentenceArrayCount += 1

                Catch ex As Exception

                    sentenceLoop += 1
                    linePunctuation -= 1

                End Try

            End While

            lineLoop += 1

        End While

        Dim newFile As New String("Translated Text.txt")
        Dim outputLoopCount As Integer = 0

        Using output As StreamWriter = New StreamWriter(newFile)

            While outputLoopCount < sentenceArraySize

                output.Write(sentence(outputLoopCount) + ". ")

                outputLoopCount += 1

            End While

        End Using

        input1.Close()
        input2.Close()

    End Sub

    Private Sub translateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles translateButton.Click

        translateText()

    End Sub

End Class
扭转时空 2024-09-01 13:41:57

使用 MyGengo。他们有一个免费的机器翻译 API——我不知道质量如何,但你也可以付费插入人工翻译。

我与他们没有任何关系,也没有使用过它们,但我听说过一些好消息。

Use MyGengo. They have a free API for machine translation - I don't know what the quality is like, but you can also plug in human translation for a fee.

I'm not affiliated with them nor have I used them, but I've heard good things.

累赘 2024-09-01 13:41:57

我们使用了http://www.berlitz.co.uk/translation/

我们会向他们发送一个包含英语内容的数据库文件以及我们所需的语言列表,然后他们会使用各种双语人员来提供翻译。他们还使用配音演员为我们的电话界面提供 WAV 文件。

这显然不如自动翻译快,而且不是免费的,但我认为这种服务是确保您的翻译有意义的唯一方法。

We used http://www.berlitz.co.uk/translation/.

We'd send them a database file with the English content, and a list of the languages we required, and they'd use various bilingual people to provide the translations. They also used voice-actors to provide WAV files for our telephone interface.

This was obviously not as fast as automated translation, and not free, but I think this sort of service is the only way to be sure your translation makes sense.

万人眼中万个我 2024-09-01 13:41:57

Google 提供了一个有用的工具,Google 翻译工具包,它可以让您上传文件并立即将其翻译为 Google 翻译支持的任何语言。
如果您想使用自动翻译,它是免费的,但也可以选择聘请真人为您翻译您的文档。

来自维基百科:

Google 翻译工具包是一款网络应用程序,旨在让译者能够编辑 Google 翻译自动生成的翻译。借助 Google 翻译工具包,译者可以组织自己的工作并使用共享翻译、术语表和翻译记忆库。他们可以上传和翻译 Microsoft Word 文档、OpenOffice.org、RTF、HTML、文本和维基百科文章。

链接

Google provides a useful tool, Google Translator Toolkit, which allows you to upload files and translate them, to whichever language Google Translate supports, at once.
It's free if you want to use the automated translations but there is an option to hire real persons to translate your documents for you.

From Wikipedia:

Google Translator Toolkit is a web application designed to allow translators to edit the translations that Google Translate automatically generates. With the Google Translator Toolkit, translators can organize their work and use shared translations, glossaries and translation memories. They can upload and translate Microsoft Word documents, OpenOffice.org, RTF, HTML, text, and Wikipedia articles.

Link

茶花眉 2024-09-01 13:41:57

有许多不同的机器翻译 API:Google、Microsoft、Yandex、IBM、PROMT、 Systran、百度、YeeCloud、DeepL、SDL 和 SAP。

其中一些支持批量请求(一次翻译一组文本)。我会逐句翻译,并适当处理 403/429 错误(通常用于响应超出配额)。

我可以向您推荐我们最近的评估研究(2017 年 11 月):机器翻译状态

There are a plenty of different machine translation APIs: Google, Microsoft, Yandex, IBM, PROMT, Systran, Baidu, YeeCloud, DeepL, SDL, and SAP.

Some of them support batch requests (translating an array of text at once). I would translate sentence by sentence with proper processing of 403/429 errors (usually used to respond to exceeded quota).

I may refer you to our recent evaluation study (November 2017): State of machine translation

各自安好 2024-09-01 13:41:57

免责声明:虽然我确实发现标记化作为一种​​翻译手段值得怀疑,但稍后将句子分割由 ubiquibacon 说明 可能会产生满足您要求的结果。

我建议可以通过将 30 多行字符串修改减少到他要求的一行正则表达式来改进他的代码 在另一个问题中,但该建议没有得到很好的接受。

以下是在 VB 中使用 Google API for .NET 的实现.NET 和 C#。

文件 Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Google.API.Translate;

namespace TokenizingTranslatorCS
{
    internal class Program
    {
        private static readonly TranslateClient Client =
            new TranslateClient("http://code.google.com/p/google-api-for-dotnet/");

        private static void Main(string[] args)
        {
            Language originalLanguage = Language.English;
            Language targetLanguage = Language.German;

            string filename = args[0];

            StringBuilder output = new StringBuilder();

            string[] input = File.ReadAllLines(filename);

            foreach (string line in input)
            {
                List<string> translatedSentences = new List<string>();
                string[] sentences = Regex.Split(line, "\\b(?<sentence>.*?[\\.!?](?:\\s|$))");
                foreach (string sentence in sentences)
                {
                    string sentenceToTranslate = sentence.Trim();

                    if (!string.IsNullOrEmpty(sentenceToTranslate))
                    {
                        translatedSentences.Add(TranslateSentence(sentence, originalLanguage, targetLanguage));
                    }
                }


                output.AppendLine(string.Format("{0}{1}", string.Join(" ", translatedSentences.ToArray()),
                                                Environment.NewLine));
            }

            Console.WriteLine("Translated:{0}{1}{0}", Environment.NewLine, string.Join(Environment.NewLine, input));
            Console.WriteLine("To:{0}{1}{0}", Environment.NewLine, output);
            Console.WriteLine("{0}Press any key{0}", Environment.NewLine);


            Console.ReadKey();
        }

        private static string TranslateSentence(string sentence, Language originalLanguage, Language targetLanguage)
        {
            string translatedSentence = Client.Translate(sentence, originalLanguage, targetLanguage);
            return translatedSentence;
        }
    }
}

文件 Module1.vb

Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Text
Imports Google.API.Translate


Module Module1

    Private Client As TranslateClient = New TranslateClient("http://code.google.com/p/google-api-for-dotnet/")

    Sub Main(ByVal args As String())

        Dim originalLanguage As Language = Language.English
        Dim targetLanguage As Language = Language.German

        Dim filename As String = args(0)

        Dim output As New StringBuilder

        Dim input As String() = File.ReadAllLines(filename)

        For Each line As String In input
            Dim translatedSentences As New List(Of String)
            Dim sentences As String() = Regex.Split(line, "\b(?<sentence>.*?[\.!?](?:\s|$))")
            For Each sentence As String In sentences

                Dim sentenceToTranslate As String = sentence.Trim

                If Not String.IsNullOrEmpty(sentenceToTranslate) Then

                    translatedSentences.Add(TranslateSentence(sentence, originalLanguage, targetLanguage))

                End If

            Next

            output.AppendLine(String.Format("{0}{1}", String.Join(" ", translatedSentences.ToArray), Environment.NewLine))

        Next

        Console.WriteLine("Translated:{0}{1}{0}", Environment.NewLine, String.Join(Environment.NewLine, input))
        Console.WriteLine("To:{0}{1}{0}", Environment.NewLine, output)
        Console.WriteLine("{0}Press any key{0}", Environment.NewLine)
        Console.ReadKey()


    End Sub

    Private Function TranslateSentence(ByVal sentence As String, ByVal originalLanguage As Language, ByVal targetLanguage As Language) As String

        Dim translatedSentence As String = Client.Translate(sentence, originalLanguage, targetLanguage)
        Return translatedSentence
    End Function

End Module

输入(直接从 ubiquibacon 窃取)

只是为了证明我抛出这个观点
在一起:)周围很粗糙
边缘,但它可以处理很多
文本的效果和它一样好
谷歌翻译准确率
因为它使用了 Google API。我
处理了 Apple 的整个 2005 SEC 10-K
使用此代码提交并单击
一键(大约需要 45 分钟)。
结果基本相同
如果你复制你会得到什么
一次将一句话粘贴到
谷歌翻译。它并不完美
(结尾标点不准确
我没有写入文本文件
逐行),但它确实显示了证据
的概念。它可以有更好的
如果您使用正则表达式,则标点符号
还有一些。

结果(德语打字):

Nur um zu beweisen einen Punkt warf
ich dies zusammen:) Es ist Ecken und
Kanten, aber es wrd eine ganze Menge
文本 umgehen und es tut so gut wie
Google 的 Genauigkeit der
Übersetzungen,weil es die Google-API
评论。 Ich verarbeitet Apple's 的
gesamte 2005 SEC 10-K 归档
代码与调用
品尝(dauerte 约 45 分钟)。达斯
同一性中的战争
zu dem,是 Sie erhalten würden,wenn
Sie kopiert und eingefügt einem Satz
在 einer Zeit 中,在谷歌翻译中。
Es ist nicht perfekt(Endung
互译是不相关的
ich wollte nicht in die Textdatei
Zeile für Zeile) schreiben, aber es
zeigt 概念证明。埃斯哈特
最好的 Satzzeichen,与正则表达式相关
arbeitete einige mehr。

Disclaimer: While I definitely find tokenizing as a means of translation suspect, splitting on sentences as later illustrated by ubiquibacon may produce results that fill your requirements.

I suggested that his code could be improved by reducing the 30+ lines of string munging to the one-line regex he asked for in another question, but the suggestion was not well received.

Here is an implementation using the Google API for .NET in VB.NET and C#.

File Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Google.API.Translate;

namespace TokenizingTranslatorCS
{
    internal class Program
    {
        private static readonly TranslateClient Client =
            new TranslateClient("http://code.google.com/p/google-api-for-dotnet/");

        private static void Main(string[] args)
        {
            Language originalLanguage = Language.English;
            Language targetLanguage = Language.German;

            string filename = args[0];

            StringBuilder output = new StringBuilder();

            string[] input = File.ReadAllLines(filename);

            foreach (string line in input)
            {
                List<string> translatedSentences = new List<string>();
                string[] sentences = Regex.Split(line, "\\b(?<sentence>.*?[\\.!?](?:\\s|$))");
                foreach (string sentence in sentences)
                {
                    string sentenceToTranslate = sentence.Trim();

                    if (!string.IsNullOrEmpty(sentenceToTranslate))
                    {
                        translatedSentences.Add(TranslateSentence(sentence, originalLanguage, targetLanguage));
                    }
                }


                output.AppendLine(string.Format("{0}{1}", string.Join(" ", translatedSentences.ToArray()),
                                                Environment.NewLine));
            }

            Console.WriteLine("Translated:{0}{1}{0}", Environment.NewLine, string.Join(Environment.NewLine, input));
            Console.WriteLine("To:{0}{1}{0}", Environment.NewLine, output);
            Console.WriteLine("{0}Press any key{0}", Environment.NewLine);


            Console.ReadKey();
        }

        private static string TranslateSentence(string sentence, Language originalLanguage, Language targetLanguage)
        {
            string translatedSentence = Client.Translate(sentence, originalLanguage, targetLanguage);
            return translatedSentence;
        }
    }
}

File Module1.vb

Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Text
Imports Google.API.Translate


Module Module1

    Private Client As TranslateClient = New TranslateClient("http://code.google.com/p/google-api-for-dotnet/")

    Sub Main(ByVal args As String())

        Dim originalLanguage As Language = Language.English
        Dim targetLanguage As Language = Language.German

        Dim filename As String = args(0)

        Dim output As New StringBuilder

        Dim input As String() = File.ReadAllLines(filename)

        For Each line As String In input
            Dim translatedSentences As New List(Of String)
            Dim sentences As String() = Regex.Split(line, "\b(?<sentence>.*?[\.!?](?:\s|$))")
            For Each sentence As String In sentences

                Dim sentenceToTranslate As String = sentence.Trim

                If Not String.IsNullOrEmpty(sentenceToTranslate) Then

                    translatedSentences.Add(TranslateSentence(sentence, originalLanguage, targetLanguage))

                End If

            Next

            output.AppendLine(String.Format("{0}{1}", String.Join(" ", translatedSentences.ToArray), Environment.NewLine))

        Next

        Console.WriteLine("Translated:{0}{1}{0}", Environment.NewLine, String.Join(Environment.NewLine, input))
        Console.WriteLine("To:{0}{1}{0}", Environment.NewLine, output)
        Console.WriteLine("{0}Press any key{0}", Environment.NewLine)
        Console.ReadKey()


    End Sub

    Private Function TranslateSentence(ByVal sentence As String, ByVal originalLanguage As Language, ByVal targetLanguage As Language) As String

        Dim translatedSentence As String = Client.Translate(sentence, originalLanguage, targetLanguage)
        Return translatedSentence
    End Function

End Module

Input (stolen directly from ubiquibacon)

Just to prove a point I threw this
together :) It is rough around the
edges, but it will handle a WHOLE lot
of text and it does just as good as
Google for translation accuracy
because it uses the Google API. I
processed Apple's entire 2005 SEC 10-K
filing with this code and the click of
one button (took about 45 minutes).
The result was basically identical to
what you would get if you copied and
pasted one sentence at a time into
Google Translator. It isn't perfect
(ending punctuation is not accurate
and I didn't write to the text file
line by line), but it does show proof
of concept. It could have better
punctuation if you worked with Regex
some more.

Results (to German for typoking):

Nur um zu beweisen einen Punkt warf
ich dies zusammen:) Es ist Ecken und
Kanten, aber es wird eine ganze Menge
Text umgehen und es tut so gut wie
Google für die Genauigkeit der
Übersetzungen, weil es die Google-API
verwendet. Ich verarbeitet Apple's
gesamte 2005 SEC 10-K Filing bei
diesem Code und dem Klicken einer
Taste (dauerte ca. 45 Minuten). Das
Ergebnis war im wesentlichen identisch
zu dem, was Sie erhalten würden, wenn
Sie kopiert und eingefügt einem Satz
in einer Zeit, in Google Translator.
Es ist nicht perfekt (Endung
Interpunktion ist nicht korrekt und
ich wollte nicht in die Textdatei
Zeile für Zeile) schreiben, aber es
zeigt proof of concept. Es hätte
besser Satzzeichen, wenn Sie mit Regex
arbeitete einige mehr.

じ违心 2024-09-01 13:41:57

这非常简单,有几种方法:

  • 使用 API 并以块的形式转换数据(这符合限制)。
  • 编写您自己的简单库来使用 HttpWebRequest 并向其 POST 一些数据。

下面是一个示例(第二个):

方法:

private String TranslateTextEnglishSpanish(String textToTranslate)
{
        HttpWebRequest http = WebRequest.Create("http://translate.google.com/") as HttpWebRequest;
        http.Method = "POST";
        http.ContentType = "application/x-www-form-urlencoded";
        http.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 (.NET CLR 3.5.30729)";
        http.Referer = "http://translate.google.com/";

        byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(String.Format("js=y&prev=_t&hl=en&ie=UTF-8&layout=1&eotf=1&text={0}+&file=&sl=en&tl=es", textToTranslate);

        http.ContentLength = dataBytes.Length;

        using (Stream postStream = http.GetRequestStream())
        {
            postStream.Write(dataBytes, 0, dataBytes.Length);
        }

        HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
        if (httpResponse != null)
        {
            using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
            {
                //* Return translated Text
                return reader.ReadToEnd();
            }
        }

        return "";
}

方法调用:

StringtranslatedText = TranslateTextEnglishSpanish("hello world");

结果:

translatedText == "hola mundo";

您只需要获取所有语言的参数并使用它们即可获得您需要的翻译。

您可以使用 Firefox 的实时 Http Headers 插件 获取数千个值。

It's pretty simple, and there are a few ways:

  • Use the API and translate data in chunks (which matches the limitations).
  • Write your own simple library to use HttpWebRequest and POST some data to it.

Here is an example (of the second one):

Method:

private String TranslateTextEnglishSpanish(String textToTranslate)
{
        HttpWebRequest http = WebRequest.Create("http://translate.google.com/") as HttpWebRequest;
        http.Method = "POST";
        http.ContentType = "application/x-www-form-urlencoded";
        http.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 (.NET CLR 3.5.30729)";
        http.Referer = "http://translate.google.com/";

        byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(String.Format("js=y&prev=_t&hl=en&ie=UTF-8&layout=1&eotf=1&text={0}+&file=&sl=en&tl=es", textToTranslate);

        http.ContentLength = dataBytes.Length;

        using (Stream postStream = http.GetRequestStream())
        {
            postStream.Write(dataBytes, 0, dataBytes.Length);
        }

        HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
        if (httpResponse != null)
        {
            using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
            {
                //* Return translated Text
                return reader.ReadToEnd();
            }
        }

        return "";
}

Method Call:

String translatedText = TranslateTextEnglishSpanish("hello world");

Result:

translatedText == "hola mundo";

You just need to get all languages' parameters and use them in order to get translations you need.

You can get thous values using the Live Http Headers addon for Firefox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文