OSDN Git Service

・翻訳しようとしたときにNullreferenceExceptionで落ちることがあるバグを修正
authorsyo68k <syo68k@users.sourceforge.jp>
Mon, 17 Jan 2011 14:33:57 +0000 (14:33 +0000)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 18 Feb 2012 14:18:29 +0000 (23:18 +0900)
・コード見直しによる翻訳品質の向上
・UserAgentを送るように修正

git-svn-id: http://svn.sourceforge.jp/svnroot/tween/trunk@1359 e39ad16e-3079-482e-bb30-4b4d378143b6

Tween/Google.vb
Tween/StatusDictionary.vb
Tween/Tween.vb
Tween/Twitter.vb

index 66e52fc..6456a94 100644 (file)
@@ -40,21 +40,28 @@ Public Class Google
         <DataMember(Name:="responseStatus")> Public ResponseStatus As HttpStatusCode
     End Class
 
-    Public Function Translate(ByVal isHtml As Boolean, ByVal source As String, ByRef destination As String) As Boolean
+    Public Function Translate(ByVal srclng As String, ByVal dstlng As String, 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))
+        If String.IsNullOrEmpty(srclng) OrElse String.IsNullOrEmpty(dstlng) Then
+            Return False
+        End If
+        headers.Add("User-Agent", "Tween/" + fileVersion)
+        headers.Add("langpair", srclng + "|" + dstlng)
+
+        headers.Add("q", 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)
+
+            If res.ResponseData Is Nothing Then
+                Return False
+            End If
             Dim _body As String = res.ResponseData.TranslatedText
             Dim buf As String = HttpUtility.UrlDecode(_body)
 
@@ -68,8 +75,9 @@ Public Class Google
         Dim http As New HttpVarious()
         Dim apiurl As String = LanguageDetectEndPoint
         Dim headers As New Dictionary(Of String, String)
+        headers.Add("User-Agent", "Tween/" + fileVersion)
         headers.Add("v", "1.0")
-        headers.Add("q", HttpUtility.UrlPathEncode(source))
+        headers.Add("q", source)
         Dim content As String = ""
         If http.GetData(apiurl, headers, content) Then
             Dim serializer As New DataContractJsonSerializer(GetType(LanguageDetectResponse))
index 91afa78..490012b 100644 (file)
@@ -62,6 +62,7 @@ Public NotInheritable Class PostClass
     Private _SearchTabName As String = ""
     Private _IsDeleted As Boolean = False
     Private _InReplyToUserId As Long = 0
+    Private _langauge As String = ""
 
     <FlagsAttribute()> _
     Private Enum Statuses
@@ -386,6 +387,16 @@ Public NotInheritable Class PostClass
             _IsDeleted = value
         End Set
     End Property
+
+    Public Property Language As String
+        Get
+            Return _langauge
+        End Get
+        Set(ByVal value As String)
+            _langauge = value
+        End Set
+    End Property
+
     Public Property FavoritedCount As Integer
 
     Public Function Copy() As PostClass
index 0705a2c..61754e4 100644 (file)
@@ -9966,9 +9966,9 @@ RETRY:
         Dim g As New Google
         Dim buf As String = ""
         If Not Me.ExistCurrentPost 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
+        Dim srclng As String = g.LanguageDetect(_curPost.Data)
+        If srclng <> "ja" AndAlso g.Translate(srclng, "ja", _curPost.Data, buf) Then
+            PostBrowser.DocumentText = createDetailHtml(buf)
         End If
     End Sub
 
index 6b71528..cfc70d4 100644 (file)
@@ -1472,6 +1472,7 @@ Public Class Twitter
             post.Nickname = user.Name
             post.ImageUrl = user.ProfileImageUrl
             post.IsProtect = user.Protected
+            post.Language = user.Lang
 
             'Retweetした人
             post.RetweetedBy = status.User.ScreenName
@@ -1496,6 +1497,7 @@ Public Class Twitter
             post.Nickname = user.Name
             post.ImageUrl = user.ProfileImageUrl
             post.IsProtect = user.Protected
+            post.Language = user.Lang
             post.IsMe = post.Name.ToLower.Equals(_uid)
             If post.IsMe Then _UserIdNo = post.UserId.ToString
         End If
@@ -1832,6 +1834,7 @@ Public Class Twitter
                 post.IsRead = read
                 post.IsReply = post.ReplyToList.Contains(_uid)
                 post.IsExcludeReply = False
+                post.Language = xentryNode.Item("twitter:lang").InnerText
 
                 post.IsOwl = False
                 If post.IsMe AndAlso Not read AndAlso _readOwnPost Then post.IsRead = True
@@ -1938,6 +1941,7 @@ Public Class Twitter
                 post.Nickname = user.Name
                 post.ImageUrl = user.ProfileImageUrl
                 post.IsProtect = user.Protected
+                post.Language = user.Lang
             Catch ex As Exception
                 TraceOut(content)
                 MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)")
@@ -2077,6 +2081,7 @@ Public Class Twitter
                     post.Nickname = user.Name
                     post.ImageUrl = user.ProfileImageUrl
                     post.IsProtect = user.Protected
+                    post.Language = user.Lang
 
                     'Retweetした人
                     post.RetweetedBy = status.User.ScreenName
@@ -2102,6 +2107,7 @@ Public Class Twitter
                     post.Nickname = user.Name
                     post.ImageUrl = user.ProfileImageUrl
                     post.IsProtect = user.Protected
+                    post.Language = user.Lang
                     post.IsMe = post.Name.ToLower.Equals(_uid)
                     If post.IsMe Then _UserIdNo = post.UserId.ToString
                 End If
@@ -2540,11 +2546,6 @@ Public Class Twitter
         If Text Is Nothing Then Return Nothing
         Dim retStr As String = Text.Replace("&gt;", "<<<<<tweenだいなり>>>>>").Replace("&lt;", "<<<<<tweenしょうなり>>>>>")
         'uriの正規表現
-        'Const rgUrl As String = "(?<before>(?:[^\""':!=]|^|\:))" + _
-        '                            "(?<url>(?<protocol>https?://|www\.)" + _
-        '                            "(?<domain>(?:[\.-]|[^\p{P}\s])+\.[a-z]{2,}(?::[0-9]+)?)" + _
-        '                            "(?<path>/[a-z0-9!*'();:&=+$/%#\[\]\-_.,~@^]*[a-z0-9)=#/]?)?" + _
-        '                            "(?<query>\?[a-z0-9!*'();:&=+$/%#\[\]\-_.,~]*[a-z0-9_&=#])?)"
         Const url_valid_general_path_chars As String = "[a-z0-9!*';:=+$/%#\[\]\-_,~]"
         Const url_valid_url_path_ending_chars As String = "[a-z0-9=#/]"
         Const pth As String = "(?<path>/(?:(?:\(" + url_valid_general_path_chars + "+\))" +
@@ -2615,16 +2616,6 @@ Public Class Twitter
                                                   End Function),
                                               RegexOptions.IgnoreCase)
 
-        'Dim mhs As MatchCollection = Regex.Matches(retStr, "(^|[^a-zA-Z0-9/&])[##]([0-9a-zA-Z_]*[a-zA-Z_]+[a-zA-Z_\xc0-\xd6\xd8-\xf6\xf8-\xff]*)")
-        'For Each mt As Match In mhs
-        '    If Not IsNumeric(mt.Result("$2")) Then
-        '        'retStr = retStr.Replace(mt.Result("$1") + mt.Result("$2"), "<a href=""" + _protocol + "twitter.com/search?q=%23" + mt.Result("$2") + """>#" + mt.Result("$2") + "</a>")
-        '        SyncLock LockObj
-        '            _hashList.Add("#" + mt.Result("$2"))
-        '        End SyncLock
-        '    End If
-        'Next
-        'retStr = Regex.Replace(retStr, "(^|[^a-zA-Z0-9/&])([##])([0-9a-zA-Z_]*[a-zA-Z_]+[a-zA-Z0-9_\xc0-\xd6\xd8-\xf6\xf8-\xff]*)", "$1<a href=""" & _protocol & "twitter.com/search?q=%23$3"">$2$3</a>")
 
         retStr = Regex.Replace(retStr, "(^|[^a-zA-Z0-9_/&##@@>=.])(sm|nm)([0-9]{1,10})", "$1<a href=""http://www.nicovideo.jp/watch/$2$3"">$2$3</a>")