From 150c3d624de8eb3698d7e25e26f4e1db64f2ac51 Mon Sep 17 00:00:00 2001 From: syo68k Date: Mon, 20 Dec 2010 16:20:33 +0000 Subject: [PATCH] =?utf8?q?=E8=A9=B3=E7=B4=B0=E8=A1=A8=E7=A4=BA=E3=81=AE?= =?utf8?q?=E7=99=BA=E8=A8=80=E7=BF=BB=E8=A8=B3=E6=A9=9F=E8=83=BD=E8=BF=BD?= =?utf8?q?=E5=8A=A0=20KnownBug:=E3=83=BB=E6=97=A5=E6=9C=AC=E8=AA=9E?= =?utf8?q?=E3=81=AE=E3=83=84=E3=82=A4=E3=83=BC=E3=83=88=E3=81=AB=E5=AF=BE?= =?utf8?q?=E3=81=97=E3=81=A6=E5=AE=9F=E8=A1=8C=E3=81=99=E3=82=8B=E3=81=A8?= =?utf8?q?=E8=A1=A8=E7=A4=BA=E3=81=8C=E3=81=8A=E3=81=8B=E3=81=97=E3=81=8F?= =?utf8?q?=E3=81=AA=E3=82=8B=20=E3=83=BB=E7=BF=BB=E8=A8=B3=E5=85=88?= =?utf8?q?=E3=81=AE=E8=A8=80=E8=AA=9E=E3=81=8C=E6=97=A5=E6=9C=AC=E8=AA=9E?= =?utf8?q?=E5=9B=BA=E5=AE=9A=E3=81=AB=E3=81=AA=E3=81=A3=E3=81=A6=E3=81=84?= =?utf8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.sourceforge.jp/svnroot/tween/trunk@1233 e39ad16e-3079-482e-bb30-4b4d378143b6 --- Tween/Google.vb | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ Tween/MyCommon.vb | 12 ++++++++ Tween/Tween.Designer.vb | 9 +++++- Tween/Tween.resx | 80 +++++++++++++++++++++++++++-------------------- Tween/Tween.vb | 9 ++++++ Tween/Tween.vbproj | 1 + Tween/Twitter.vb | 11 ------- 7 files changed, 158 insertions(+), 46 deletions(-) create mode 100644 Tween/Google.vb diff --git a/Tween/Google.vb b/Tween/Google.vb new file mode 100644 index 00000000..66e52fc5 --- /dev/null +++ b/Tween/Google.vb @@ -0,0 +1,82 @@ +Imports System.Web +Imports System.Text.RegularExpressions +Imports System.Runtime.Serialization.Json +Imports System.Net +Imports System.Runtime.Serialization + +' http://code.google.com/intl/ja/apis/ajaxlanguage/documentation/#fonje +' デベロッパー ガイド - Google AJAX Language API - Google Code + +Public Class Google + + Private Const TranslateEndPoint As String = "http://ajax.googleapis.com/ajax/services/language/translate" + Private Const LanguageDetectEndPoint As String = "https://ajax.googleapis.com/ajax/services/language/detect" + + _ + Public Class TranslateResponseData + Public TranslatedText As String + End Class + + + _ + Private Class TranslateResponse + Public ResponseData As TranslateResponseData + Public ResponseDetails As String + Public ResponseStatus As HttpStatusCode + End Class + + + _ + Public Class LanguageDetectResponseData + Public Language As String + Public IsReliable As Boolean + Public Confidence As Double + End Class + + _ + Private Class LanguageDetectResponse + Public ResponseData As LanguageDetectResponseData + Public ResponseDetails As String + Public ResponseStatus As HttpStatusCode + End Class + + Public Function Translate(ByVal isHtml As Boolean, ByVal source As String, ByRef destination As String) As Boolean + Dim http As New HttpVarious() + Dim apiurl As String = TranslateEndPoint + Dim headers As New Dictionary(Of String, String) + headers.Add("v", "1.0") + headers.Add("hl", "ja") ' TODO:現在のcultureを反映させる + headers.Add("langpair", "|ja") ' TODO:現在のcultureを反映させる + headers.Add("format", "html") + + headers.Add("q", HttpUtility.UrlPathEncode(source)) + + Dim content As String = "" + If http.GetData(apiurl, headers, content) Then + Dim serializer As New DataContractJsonSerializer(GetType(TranslateResponse)) + Dim res As TranslateResponse = CreateDataFromJson(Of TranslateResponse)(content) + Dim _body As String = res.ResponseData.TranslatedText + Dim buf As String = HttpUtility.UrlDecode(_body) + + destination = String.Copy(buf) + Return True + End If + Return False + End Function + + Public Function LanguageDetect(ByVal source As String) As String + Dim http As New HttpVarious() + Dim apiurl As String = LanguageDetectEndPoint + Dim headers As New Dictionary(Of String, String) + headers.Add("v", "1.0") + headers.Add("q", HttpUtility.UrlPathEncode(source)) + Dim content As String = "" + If http.GetData(apiurl, headers, content) Then + Dim serializer As New DataContractJsonSerializer(GetType(LanguageDetectResponse)) + Dim res As LanguageDetectResponse = CreateDataFromJson(Of LanguageDetectResponse)(content) + Return res.ResponseData.Language + End If + Return "" + End Function + +End Class diff --git a/Tween/MyCommon.vb b/Tween/MyCommon.vb index 823e52d8..66bfb2a1 100644 --- a/Tween/MyCommon.vb +++ b/Tween/MyCommon.vb @@ -27,6 +27,7 @@ Imports System.Security.Principal Imports System.Reflection Imports System.Web Imports System.IO +Imports System.Runtime.Serialization.Json Public Module MyCommon Private ReadOnly LockObj As New Object @@ -590,4 +591,15 @@ retry: TraceOut("Parse Error(DateTimeFormat) : " + input) Return New Date End Function + + Public Function CreateDataFromJson(Of T)(ByVal content As String) As T + Dim data As T + Using stream As New MemoryStream() + Dim buf As Byte() = Encoding.Unicode.GetBytes(content) + stream.Write(Encoding.Unicode.GetBytes(content), offset:=0, count:=buf.Length) + stream.Seek(offset:=0, loc:=SeekOrigin.Begin) + data = DirectCast((New DataContractJsonSerializer(GetType(T))).ReadObject(stream), T) + End Using + Return data + End Function End Module \ No newline at end of file diff --git a/Tween/Tween.Designer.vb b/Tween/Tween.Designer.vb index 41c0c742..62870598 100644 --- a/Tween/Tween.Designer.vb +++ b/Tween/Tween.Designer.vb @@ -113,6 +113,7 @@ Partial Class TweenMain Me.ListManageUserContextToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem() Me.ToolStripSeparator33 = New System.Windows.Forms.ToolStripSeparator() Me.UseHashtagMenuItem = New System.Windows.Forms.ToolStripMenuItem() + Me.TranslationToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem() Me.DateTimeLabel = New System.Windows.Forms.Label() Me.SourceLinkLabel = New System.Windows.Forms.LinkLabel() Me.StatusText = New System.Windows.Forms.TextBox() @@ -771,7 +772,7 @@ Partial Class TweenMain ' 'ContextMenuPostBrowser ' - Me.ContextMenuPostBrowser.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.SelectionSearchContextMenuItem, Me.ToolStripSeparator13, Me.SelectionCopyContextMenuItem, Me.UrlCopyContextMenuItem, Me.SelectionAllContextMenuItem, Me.ToolStripSeparator5, Me.FollowContextMenuItem, Me.RemoveContextMenuItem, Me.FriendshipContextMenuItem, Me.FriendshipAllMenuItem, Me.ToolStripSeparator36, Me.ShowUserStatusContextMenuItem, Me.SearchPostsDetailToolStripMenuItem, Me.SearchAtPostsDetailToolStripMenuItem, Me.ToolStripSeparator32, Me.IdFilterAddMenuItem, Me.ListManageUserContextToolStripMenuItem, Me.ToolStripSeparator33, Me.UseHashtagMenuItem}) + Me.ContextMenuPostBrowser.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.SelectionSearchContextMenuItem, Me.ToolStripSeparator13, Me.SelectionCopyContextMenuItem, Me.UrlCopyContextMenuItem, Me.SelectionAllContextMenuItem, Me.ToolStripSeparator5, Me.FollowContextMenuItem, Me.RemoveContextMenuItem, Me.FriendshipContextMenuItem, Me.FriendshipAllMenuItem, Me.ToolStripSeparator36, Me.ShowUserStatusContextMenuItem, Me.SearchPostsDetailToolStripMenuItem, Me.SearchAtPostsDetailToolStripMenuItem, Me.ToolStripSeparator32, Me.IdFilterAddMenuItem, Me.ListManageUserContextToolStripMenuItem, Me.ToolStripSeparator33, Me.UseHashtagMenuItem, Me.TranslationToolStripMenuItem}) Me.ContextMenuPostBrowser.Name = "ContextMenuStrip4" resources.ApplyResources(Me.ContextMenuPostBrowser, "ContextMenuPostBrowser") ' @@ -896,6 +897,11 @@ Partial Class TweenMain Me.UseHashtagMenuItem.Name = "UseHashtagMenuItem" resources.ApplyResources(Me.UseHashtagMenuItem, "UseHashtagMenuItem") ' + 'TranslationToolStripMenuItem + ' + Me.TranslationToolStripMenuItem.Name = "TranslationToolStripMenuItem" + resources.ApplyResources(Me.TranslationToolStripMenuItem, "TranslationToolStripMenuItem") + ' 'DateTimeLabel ' resources.ApplyResources(Me.DateTimeLabel, "DateTimeLabel") @@ -2137,5 +2143,6 @@ Partial Class TweenMain Friend WithEvents OpenOwnFavedMenuItem As System.Windows.Forms.ToolStripMenuItem Friend WithEvents ToolStripSeparator42 As System.Windows.Forms.ToolStripSeparator Friend WithEvents EventViewerMenuItem As System.Windows.Forms.ToolStripMenuItem + Friend WithEvents TranslationToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem End Class diff --git a/Tween/Tween.resx b/Tween/Tween.resx index 14cef1bc..e7626573 100644 --- a/Tween/Tween.resx +++ b/Tween/Tween.resx @@ -382,7 +382,7 @@ 0, 0, 0, 0 - 570, 248 + 570, 252 0 @@ -406,7 +406,7 @@ 0, 0 - 570, 248 + 570, 252 0 @@ -436,7 +436,7 @@ 0, 0 - 570, 220 + 570, 224 Zoom @@ -640,7 +640,7 @@ Bottom - 0, 220 + 0, 224 3, 3, 3, 3 @@ -670,7 +670,7 @@ 0, 0 - 570, 248 + 570, 252 1 @@ -697,7 +697,7 @@ 0, 0 - 570, 248 + 570, 252 2 @@ -922,16 +922,16 @@ 現在のタブ(&L) - 232, 22 + 262, 22 選択文字列で検索(&S) - 229, 6 + 259, 6 - 232, 22 + 262, 22 選択文字列をコピー(&C) @@ -940,91 +940,97 @@ False - 232, 22 + 262, 22 URLをコピー(&U) - 232, 22 + 262, 22 すべて選択(&A) - 229, 6 + 259, 6 - 232, 22 + 262, 22 フォローする(&F) - 232, 22 + 262, 22 フォロー解除(&N) - 232, 22 + 262, 22 相互フォロー状態表示(&R) - 232, 22 + 262, 22 全ユーザーのフォロー状態(&A) - 229, 6 + 259, 6 - 232, 22 + 262, 22 プロフィール表示(&P) - 232, 22 + 262, 22 このユーザーの発言を検索(&F) - 232, 22 + 262, 22 このユーザーへの@発言を検索(&S) - 229, 6 + 259, 6 - 232, 22 + 262, 22 ID振分ルール作成(&I) - 232, 22 + 262, 22 リスト管理(&L) - 229, 6 + 259, 6 - 232, 22 + 262, 22 ハッシュタグを固定(&H) + + 262, 22 + + + この発言を翻訳 + - 233, 364 + 263, 386 ContextMenuPostBrowser @@ -1039,7 +1045,7 @@ 59, 20 - 508, 40 + 508, 36 6 @@ -1144,7 +1150,7 @@ 2 - 570, 63 + 570, 59 1 @@ -1282,10 +1288,10 @@ 19 - 570, 90 + 570, 86 - 63 + 59 2 @@ -1327,7 +1333,7 @@ 0, 0 - 194, 92 + 194, 88 Zoom @@ -1357,7 +1363,7 @@ 194, 0 - 17, 92 + 17, 88 0 @@ -1387,7 +1393,7 @@ 1 - 570, 90 + 570, 86 355 @@ -1426,7 +1432,7 @@ 574, 348 - 252 + 256 2 @@ -2994,6 +3000,12 @@ System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + TranslationToolStripMenuItem + + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + MenuItemFile diff --git a/Tween/Tween.vb b/Tween/Tween.vb index 6a5c9d09..9f3293b3 100644 --- a/Tween/Tween.vb +++ b/Tween/Tween.vb @@ -10012,4 +10012,13 @@ RETRY: OpenUriAsync("http://twitter.com/" + tw.Username) End Sub + Private Sub TranslationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TranslationToolStripMenuItem.Click + Dim g As New Google + Dim buf As String = "" + If _curPost Is Nothing Then Exit Sub + Dim lng As String = g.LanguageDetect(_curPost.Data) + If lng <> "ja" AndAlso g.Translate(True, PostBrowser.DocumentText, buf) Then + PostBrowser.DocumentText = buf + End If + End Sub End Class diff --git a/Tween/Tween.vbproj b/Tween/Tween.vbproj index 2a528934..5a75f8a6 100644 --- a/Tween/Tween.vbproj +++ b/Tween/Tween.vbproj @@ -162,6 +162,7 @@ Form + HashtagManage.vb diff --git a/Tween/Twitter.vb b/Tween/Twitter.vb index 907a823c..8202b865 100644 --- a/Tween/Twitter.vb +++ b/Tween/Twitter.vb @@ -2954,17 +2954,6 @@ Public Class Twitter RaiseEvent UserStreamEventReceived(evt) End Sub - Private Function CreateDataFromJson(Of T)(ByVal content As String) As T - Dim data As T - Using stream As New MemoryStream() - Dim buf As Byte() = Encoding.Unicode.GetBytes(content) - stream.Write(Encoding.Unicode.GetBytes(content), offset:=0, count:=buf.Length) - stream.Seek(offset:=0, loc:=SeekOrigin.Begin) - data = DirectCast((New DataContractJsonSerializer(GetType(T))).ReadObject(stream), T) - End Using - Return data - End Function - Private Sub userStream_Started() Handles userStream.Started RaiseEvent UserStreamStarted() End Sub -- 2.11.0