svnno****@sourc*****
svnno****@sourc*****
2011年 1月 17日 (月) 23:33:57 JST
Revision: 1359 http://sourceforge.jp/projects/tween/svn/view?view=rev&revision=1359 Author: syo68k Date: 2011-01-17 23:33:57 +0900 (Mon, 17 Jan 2011) Log Message: ----------- ・翻訳しようとしたときにNullreferenceExceptionで落ちることがあるバグを修正 ・コード見直しによる翻訳品質の向上 ・UserAgentを送るように修正 Modified Paths: -------------- trunk/Tween/Google.vb trunk/Tween/StatusDictionary.vb trunk/Tween/Tween.vb trunk/Tween/Twitter.vb -------------- next part -------------- Modified: trunk/Tween/Google.vb =================================================================== --- trunk/Tween/Google.vb 2011-01-17 13:27:51 UTC (rev 1358) +++ trunk/Tween/Google.vb 2011-01-17 14:33:57 UTC (rev 1359) @@ -40,21 +40,28 @@ <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 @@ 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)) Modified: trunk/Tween/StatusDictionary.vb =================================================================== --- trunk/Tween/StatusDictionary.vb 2011-01-17 13:27:51 UTC (rev 1358) +++ trunk/Tween/StatusDictionary.vb 2011-01-17 14:33:57 UTC (rev 1359) @@ -62,6 +62,7 @@ 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 @@ _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 Modified: trunk/Tween/Tween.vb =================================================================== --- trunk/Tween/Tween.vb 2011-01-17 13:27:51 UTC (rev 1358) +++ trunk/Tween/Tween.vb 2011-01-17 14:33:57 UTC (rev 1359) @@ -9966,9 +9966,9 @@ 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 Modified: trunk/Tween/Twitter.vb =================================================================== --- trunk/Tween/Twitter.vb 2011-01-17 13:27:51 UTC (rev 1358) +++ trunk/Tween/Twitter.vb 2011-01-17 14:33:57 UTC (rev 1359) @@ -1472,6 +1472,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ If Text Is Nothing Then Return Nothing Dim retStr As String = Text.Replace(">", "<<<<<tweenだいなり>>>>>").Replace("<", "<<<<<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 @@ 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>")