Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过接入MCIM API以支持简介翻译 #242

Merged
merged 13 commits into from
Feb 13, 2025
32 changes: 31 additions & 1 deletion Plain Craft Launcher 2/Modules/Minecraft/ModComp.vb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Public Module ModComp
Imports System.Threading.Tasks

Public Module ModComp

Public Enum CompType
''' <summary>
Expand Down Expand Up @@ -211,6 +213,34 @@
Return If(DatabaseEntry Is Nothing OrElse DatabaseEntry.ChineseName = "", RawName, DatabaseEntry.ChineseName)
End Get
End Property
''' <summary>
''' 中文描述。若为 Nothing 则没有。
''' </summary>
Public ReadOnly Property ChineseDescription As Task(Of String)
Get
Return GetChineseDescriptionAsync()
End Get
End Property

Private Async Function GetChineseDescriptionAsync() As Task(Of String)
Dim from = If(FromCurseForge, "curseforge", "modrinth")
Dim para = If(FromCurseForge, "modId", "project_id")
Dim result As String = Nothing

Try
Dim jsonObject = Await Task.Run(Function() NetGetCodeByRequestOnce($"https://mod.mcimirror.top/translate/{from}?{para}={Id}", Encode:=Encoding.UTF8, IsJson:=True))
If jsonObject.ContainsKey("translated") Then
result = jsonObject("translated").ToString()
Else
Hint($"{TranslatedName} 的简介暂无译文!", HintType.Critical)
End If
Catch ex As Exception
Log(ex, "获取中文描述时出现错误!")
Hint($"获取译文时出现错误,信息:{ex.Message}", HintType.Critical)
End Try

Return result
End Function

'实例化

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<local:MyButton x:Name="BtnIntroWiki" MinWidth="140" Text="转到 MC 百科" Padding="13,10" Margin="0,8,20,0" HorizontalAlignment="Left" />
<local:MyButton x:Name="BtnIntroCopy" MinWidth="140" Text="复制名称" Padding="13,10" Margin="0,8,20,0" HorizontalAlignment="Left" />
<local:MyButton x:Name="BtnIntroLinkCopy" MinWidth="140" Text="复制链接" Padding="13,10" Margin="0,8,20,0" HorizontalAlignment="Left" />
<local:MyButton x:Name="BtnTranslate" MinWidth="140" Text="翻译简介" Padding="13,10" Margin="0,8,20,0" HorizontalAlignment="Left" />
<local:MyButton x:Name="BtnFavorites" MinWidth="140" Text="收藏" Padding="13,10" Margin="0,8,20,0" HorizontalAlignment="Left" />
</WrapPanel>
</StackPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@
End Function

#End Region

Private IsFirstInit As Boolean = True
Public Sub Init() Handles Me.PageEnter
AniControlEnabled += 1
Expand All @@ -238,7 +237,6 @@
Else
PageLoaderRestart(IsForceRestart:=True)
End If

'放置当前工程
If CompItem IsNot Nothing Then PanIntro.Children.Remove(CompItem)
CompItem = Project.ToCompItem(True, True)
Expand Down Expand Up @@ -466,5 +464,11 @@
CompClipboard.CurrentText = Project.Website
ClipboardSet(Project.Website)
End Sub

'翻译简介
Private Async Sub BtnTranslate_Click(sender As Object, e As RoutedEventArgs) Handles BtnTranslate.Click
Hint($"正在获取 {Project.TranslatedName} 的简介译文……")
Dim ChineseDescription = Await Project.ChineseDescription
If ChineseDescription Is Nothing Then Return
MyMsgBox($"原文:{Project.Description}{Environment.NewLine}译文:{ChineseDescription}")
End Sub
End Class