Localization part 2 (#332)

* More localization work

* instance testing translation

* Row cell translations

* safety check
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2024-03-18 22:53:08 -04:00
1 parent 48993dd9f5
commit 9a7db6aab2
27 files changed
+1201 -270

No files matched your search

@@ -35,6 +35,9 @@ function Init()
m.textEditBox.hintText = `e.g. ${Invidious.DEFAULT_INSTANCE}`
SetButtonPositions()
SetPublicInstancesPosition()
m.publicInstances.observeField("width", FuncName(SetPublicInstancesPosition))
end function
function SetButtonPositions()
@@ -45,6 +48,15 @@ function SetButtonPositions()
m.saveButton.translation = [960 / 2 + buttonsWith / 2 - saveButtonWidth, m.saveButton.translation[1]]
end function
function SetPublicInstancesPosition() as void
width = m.publicInstances.width
if width = 0
return
end if
m.publicInstances.translation = [960 / 2 - width / 2, m.publicInstances.translation[1]]
end function
function OnFocusChange() as void
if not m.top.focus
return
@@ -93,7 +105,7 @@ function OnEditButtonSelected()
keyboardDialog.title = "Edit Instance"
keyboardDialog.message = [`Enter the instance url (e.g. ${Invidious.DEFAULT_INSTANCE})`]
keyboardDialog.text = m.top.text
keyboardDialog.buttons = ["OK", "Cancel"]
keyboardDialog.buttons = [Tr(Locale.Buttons.OK), Tr(Locale.Buttons.Cancel)]
keyboardDialog.observeField("buttonSelected", FuncName(OnKeyboardButton))
m.top.getScene().dialog = keyboardDialog
@@ -62,9 +62,7 @@
<InvidiousPublicInstances
id="instances"
translation="[50,190]">
</InvidiousPublicInstances>
translation="[50,190]" />
<ButtonEx
id="testButton"
@@ -53,14 +53,15 @@ function Close()
end function
function RunTestsTask()
m.titleLabel.text = `Testing ${m.top.instance}`
instance = m.top.instance
m.titleLabel.text = Tr(Locale.InvidiousTesting.TestingInstance).Replace("%1", instance)
if m.testingTask <> invalid
m.testingTask.cancel = true
end if
m.testingTask = AsyncTask.Start(Tasks.InvidiousInstanceTestingTask, {
instance: m.top.instance
instance: instance
listNode: m.testList
})
end function
@@ -68,10 +69,21 @@ end function
function ShowTestDetails(event as object) as void
selected = m.testList.itemSelected
itemContent = m.testList.content.GetChild(selected)
if StringUtils.IsNullOrEmpty(itemContent.details)
details = itemContent.details
if StringUtils.IsNullOrEmpty(details)
return
end if
message = `Status: ${itemContent.state}\nTest duration: ${itemContent.duration}ms\nDetails: ${itemContent.details}`
DialogUtils.ShowDialog(message, `Test: ${itemContent.title}`)
state = itemContent.state
displayStates = {
"passed": Locale.InvidiousTesting.Passed
"failed": Locale.InvidiousTesting.Failed
}
message = Tr(Locale.InvidiousTesting.Status) + ": " + Tr(ValidString(displayStates[state])) + `\n`
message += Tr(Locale.InvidiousTesting.TestDuration) + ": " + `${itemContent.duration}ms\n`
message += Tr(Locale.InvidiousTesting.Details) + ": " + details
title = Tr(Locale.InvidiousTesting.Test) + ": " + itemContent.title
DialogUtils.ShowDialog(message, title)
end function
@@ -12,20 +12,20 @@
opacity="0.5">
</Rectangle>
<Rectangle
width="960"
width="1160"
height="600"
translation="[160,60]"
translation="[60,60]"
color="#242424">
<Label
id="titleLabel"
width="960"
width="1160"
horizAlign="center"
text=""
translation="[0,20]" />
<MarkupGrid
id="testList"
itemSize="[860,40]"
itemSize="[1060,40]"
itemSpacing="[0,20]"
numColumns="1"
numRows="7"
@@ -38,7 +38,7 @@
iconVisible="false"
anchor="[0.5,0.5]"
padding="[30,30]"
translation="[480,530]" />
translation="[580,530]" />
</Rectangle>
</children>
</component>
@@ -13,14 +13,16 @@
translation="[5,0]" />
<Label
id="nameLabel"
width="450"
width="660"
translation="[60,10]" />
<Label
id="detailsLabel"
visible="false"
horizAlign="right"
width="300"
width="400"
text="Press 'OK' for details"
translation="[560,10]" />
translation="[660,12]">
<Font role="font" uri="font:SystemFontFile" size="20" />
</Label>
</children>
</component>
@@ -2,6 +2,7 @@ import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/Locale.bs"
import "pkg:/source/utils/StringUtils.bs"
@asynctask
@@ -25,12 +26,12 @@ function PrepareTests() as object
content = CreateObject("roSGNode", "ContentNode")
' TODO:P2 test if video data is reachable (head?)
' TODO:P2 test if video data is reachable in proxy mode (local=true)
content.AppendChild(CreateTestNode("Valid url", IsValidUrl))
content.AppendChild(CreateTestNode("Instance reachable", IsReachable))
content.AppendChild(CreateTestNode("Fetch video details", CanFetchVideoDetails))
content.AppendChild(CreateTestNode("Fetch video comments", CanFetchVideoComments))
content.AppendChild(CreateTestNode("Fetch video captions", CanFetchVideoCaptions))
content.AppendChild(CreateTestNode("Fetch video thumbnails", CanFetchVideoThumbails))
content.AppendChild(CreateTestNode(Tr(Locale.InvidiousTesting.ValidUrl), IsValidUrl))
content.AppendChild(CreateTestNode(Tr(Locale.InvidiousTesting.InstanceReachable), IsReachable))
content.AppendChild(CreateTestNode(Tr(Locale.InvidiousTesting.FetchVideoDetails), CanFetchVideoDetails))
content.AppendChild(CreateTestNode(Tr(Locale.InvidiousTesting.FetchVideoComments), CanFetchVideoComments))
content.AppendChild(CreateTestNode(Tr(Locale.InvidiousTesting.FetchVideoCaptions), CanFetchVideoCaptions))
content.AppendChild(CreateTestNode(Tr(Locale.InvidiousTesting.FetchVideoThumbnails), CanFetchVideoThumbails))
return content
end function
@@ -73,17 +74,17 @@ function IsValidUrl(instance as string, testNode as object, cancellation as obje
try
if not instance.StartsWith("http://") and not instance.StartsWith("https://")
testNode.state = "failed"
testNode.details = "Url must start with http:// or https://"
testNode.details = Tr(Locale.InvidiousTesting.UrlMustStartWith)
return
end if
if instance.EndsWith("/")
testNode.state = "failed"
testNode.details = "Url must not end with /"
testNode.details = Tr(Locale.InvidiousTesting.UrlMustNotEndWith)
return
end if
testNode.state = "passed"
testNode.details = "The url seems to be of a valid format"
testNode.details = Tr(Locale.InvidiousTesting.UrlSeemsValid)
catch e
testNode.state = "failed"
testNode.details = ErrorUtils.Format(e)
@@ -103,33 +104,33 @@ function IsReachable(instance as string, testNode as object, cancellation as obj
json = response.Json()
if json = invalid
testNode.state = "failed"
testNode.details = `Invalid json from ${url}`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFrom).Replace("%1", url)
return
end if
software = json.software
if software = invalid
testNode.state = "failed"
testNode.details = `Invalid json from ${url} (couldn't read $.software)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFromCouldntRead).Replace("%1", url).Replace("%2", "$.software")
return
end if
name = software.name
if not IsString(name)
testNode.state = "failed"
testNode.details = `Invalid json from ${url} (couldn't read $.software.name)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFromCouldntRead).Replace("%1", url).Replace("%2", "$.software.name")
return
end if
version = software.version
if not IsString(version)
testNode.state = "failed"
testNode.details = `Invalid json from ${url} (couldn't read software.version)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFromCouldntRead).Replace("%1", url).Replace("%2", "$.software.version")
return
end if
testNode.state = "passed"
testNode.details = `The instance is reachable and running ${name} version ${version}`
testNode.details = Tr(Locale.InvidiousTesting.TheInstanceIsReachableAndRunning).Replace("%1", name).Replace("%2", version)
catch e
testNode.state = "failed"
testNode.details = ErrorUtils.Format(e)
@@ -152,19 +153,19 @@ function CanFetchVideoDetails(instance as string, testNode as object, cancellati
json = response.Json()
if json = invalid
testNode.state = "failed"
testNode.details = `Invalid json from ${url}`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFrom).Replace("%1", url)
return
end if
title = json.title
if not IsString(title)
testNode.state = "failed"
testNode.details = `Invalid json from ${url} (couldn't read title)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFromCouldntRead).Replace("%1", url).Replace("%2", "$.title")
return
end if
testNode.state = "passed"
testNode.details = `The instance can fetch video details for id "${videoId}" (${title})`
testNode.details = Tr(Locale.InvidiousTesting.TheInstanceCanFetchVideoDetails).Replace("%1", videoId).Replace("%2", title)
catch e
testNode.state = "failed"
testNode.details = ErrorUtils.Format(e)
@@ -186,26 +187,26 @@ function CanFetchVideoComments(instance as string, testNode as object, cancellat
json = response.Json()
if json = invalid
testNode.state = "failed"
testNode.details = `Invalid json from ${url}`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFrom).Replace("%1", url)
return
end if
commentCount = json.commentCount
if not IsInt(commentCount)
testNode.state = "failed"
testNode.details = `Invalid json from ${url} (couldn't read commentCount)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFromCouldntRead).Replace("%1", url).Replace("%2", "$.commentCount")
return
end if
comments = json.comments
if not IsArray(comments)
testNode.state = "failed"
testNode.details = `Invalid json from ${url} (couldn't read comments)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFromCouldntRead).Replace("%1", url).Replace("%2", "$.comments")
return
end if
testNode.state = "passed"
testNode.details = `The instance can fetch video comments for id "${videoId}" (${commentCount} comments)`
testNode.details = Tr(Locale.InvidiousTesting.TheInstanceCanFetchVideoComments).Replace("%1", videoId).Replace("%2", `${commentCount}`)
catch e
testNode.state = "failed"
testNode.details = ErrorUtils.Format(e)
@@ -229,25 +230,25 @@ function CanFetchVideoCaptions(instance as string, testNode as object, cancellat
contentType = headers["Content-Type"]
if StringUtils.IsNullOrEmpty(contentType) or not contentType.StartsWith("text/vtt")
testNode.state = "failed"
testNode.details = `Invalid caption data ${url} (Content-Type is not text/vtt)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidCaptionDataContentType).Replace("%1", url)
return
end if
text = response.Text()
if StringUtils.IsNullOrEmpty(text) or not text.StartsWith("WEBVTT")
testNode.state = "failed"
testNode.details = `Invalid caption data from ${url} (response is not a valid vtt file)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidCaptionDataNotWebVtt).Replace("%1", url)
return
end if
if text.InStr(`All right, so here we are, in front of the\nelephants`) = -1
testNode.state = "failed"
testNode.details = `Did not find expected text in caption data from ${url}`
testNode.details = Tr(Locale.InvidiousTesting.ExpectedTextNotFoundInCaptionData).Replace("%1", url)
return
end if
testNode.state = "passed"
testNode.details = `The instance can fetch video ${label} captions for id "${videoId}"`
testNode.details = Tr(Locale.InvidiousTesting.TheInstanceCanFetchVideoCaptions).Replace("%1", label).Replace("%2", videoId)
catch e
testNode.state = "failed"
testNode.details = ErrorUtils.Format(e)
@@ -269,14 +270,14 @@ function CanFetchVideoThumbails(instance as string, testNode as object, cancella
json = response.Json()
if json = invalid
testNode.state = "failed"
testNode.details = `Invalid json from ${url}`
testNode.details = Tr(Locale.InvidiousTesting.InvalidJsonFrom).Replace("%1", url)
return
end if
videoThumbnails = json.videoThumbnails
if not IsArray(videoThumbnails) or videoThumbnails.Count() = 0
testNode.state = "failed"
testNode.details = `No thumbnails found in json from ${url}`
testNode.details = Tr(Locale.InvidiousTesting.NoThumbnailsFoundInJson).Replace("%1", url)
return
end if
@@ -284,7 +285,7 @@ function CanFetchVideoThumbails(instance as string, testNode as object, cancella
thumbnail = contentNode.thumbnail
if StringUtils.IsNullOrEmpty(thumbnail)
testNode.state = "failed"
testNode.details = `No thumbnail found in json from ${url}`
testNode.details = Tr(Locale.InvidiousTesting.NoThumbnailsFoundInJson).Replace("%1", url)
return
end if
@@ -293,19 +294,19 @@ function CanFetchVideoThumbails(instance as string, testNode as object, cancella
response = HttpClient.Get(thumbnail).NoCache().Cancellation(cancellation).Await()
if not response.IsSuccess()
testNode.state = "failed"
testNode.details = `${thumbnail} is not reachable\n${response.ErrorMessage(false)}`
testNode.details = Tr(Locale.InvidiousTesting.ThumbnailNotReachable).Replace("%1", thumbnail) + `\n` + response.ErrorMessage(false)
return
end if
contentType = response.Headers()["Content-Type"]
if StringUtils.IsNullOrEmpty(contentType) or not contentType.StartsWith("image/")
testNode.state = "failed"
testNode.details = `Invalid thumbnail data ${thumbnail} (Content-Type is not image/*)`
testNode.details = Tr(Locale.InvidiousTesting.InvalidThumbnailData).Replace("%1", thumbnail)
return
end if
testNode.state = "passed"
testNode.details = `The instance can fetch video thumbnail for id "${videoId}" (${thumbnail})`
testNode.details = Tr(Locale.InvidiousTesting.TheInstanceCanFetchVideoThumbnails).Replace("%1", videoId).Replace("%2", thumbnail)
catch e
testNode.state = "failed"
testNode.details = ErrorUtils.Format(e)
@@ -3,7 +3,9 @@ import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Locale.bs"
import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/MathUtils.bs"
function Init()
m.failedLabel = m.top.findNode("failedLabel")
@@ -16,11 +18,58 @@ function Init()
m.busyspinner.poster.height = 100
m.busySpinner.poster.uri = "pkg:/images/spinner.png"
InitHeader()
m.instances.ObserveField("itemSelected", FuncName(OnInstanceSelected))
m.refreshButton.ObserveField("buttonSelected", FuncName(Refresh))
Refresh()
end function
function InitHeader()
fields = ["name", "version", "users", "signup", "location", "health"]
fieldLabels = ["nameLabel", "versionLabel", "usersLabel", "signupLabel", "locationLabel", "healthLabel"]
fieldWidths = {
"name": 240
"version": 220
"users": 80
"signup": 70
"location": 65
"health": 65
}
totalWidth = 0
for i = 0 to fields.Count() - 1
field = fields[i]
label = m.instancesHeader.findNode(fieldLabels[i])
label.text = Tr(field)
rect = label.boundingRect()
rectWidth = rect.width
if i < fields.Count() - 1
rectWidth += 10
end if
width = MathUtils.Max(rectWidth, fieldWidths[field])
fieldWidths[field] = width
label.width = width
totalWidth += width
end for
m.fieldWidths = fieldWidths
m.top.width = totalWidth
end function
function OnWidthChange() as void
width = m.top.width
if width = 0
return
end if
m.instances.itemSize = [width, m.instances.itemSize[1]]
m.busySpinner.translation = [width / 2 - 50, m.busySpinner.translation[1]]
m.failedLabel.width = width
m.refreshButton.translation = [width / 2, m.refreshButton.translation[1]]
end function
function OnFocusChange() as void
if not m.top.focus
return
@@ -45,6 +94,7 @@ function Refresh()
m.invidiousPublicInstancesTask = AsyncTask.Start(Tasks.InvidiousPublicInstancesTask, {
listNode: m.instances
fieldWidths: m.fieldWidths
}, OnInvidiousPublicInstancesTaskResults)
m.busySpinner.visible = true
@@ -1,6 +1,7 @@
<component name="InvidiousPublicInstances" extends="Group" includes="Focus">
<interface>
<field id="instance" type="string" />
<field id="width" type="float" onChange="OnWidthChange" />
</interface>
<children>
<BusySpinner
@@ -11,8 +12,8 @@
id="failedLabel"
text="Failed to load Invidious public instances."
horizAlign="center"
translation="[180,60]"
width="500" />
translation="[0,60]"
width="960" />
<ButtonEx
id="refreshButton"
@@ -22,16 +23,7 @@
padding="[30,30]"
translation="[430,130]" />
<InvidiousPublicInstancesListItem id="instancesHeader">
<InvidiousPublicInstancesListItemContent
role="itemContent"
name="name"
version="version"
users="users"
signup="signup"
location="location"
health="health" />
</InvidiousPublicInstancesListItem>
<InvidiousPublicInstancesListItem id="instancesHeader" />
<MarkupGrid
id="instances"
@@ -1,5 +1,8 @@
function OnContentSet() as void
content = m.top.itemContent
if content = invalid
return
end if
m.top.name = content.name
m.top.version = content.version
@@ -8,4 +11,19 @@ function OnContentSet() as void
m.top.location = content.location
m.top.flag = content.flag
m.top.health = content.health
fieldWidths = content.fieldWidths
labels = {
"name": "nameLabelWidth"
"version": "versionLabelWidth"
"users": "usersLabelWidth"
"signup": "signupLabelWidth"
"location": "locationLabelWidth"
"health": "healthLabelWidth"
}
for each label in labels
m.top[labels[label]] = fieldWidths[label]
end for
end function
@@ -1,6 +1,7 @@
<component name="InvidiousPublicInstancesListItem" extends="Group">
<interface>
<field id="itemContent" type="node" onChange="OnContentSet" />
<field id="name" type="string" alias="nameLabel.text" />
<field id="version" type="string" alias="versionLabel.text" />
<field id="users" type="string" alias="usersLabel.text" />
@@ -8,41 +9,24 @@
<field id="location" type="string" alias="locationLabel.text" />
<field id="flag" type="string" alias="flagEmoji.uri" />
<field id="health" type="string" alias="healthLabel.text" />
<field id="nameLabelWidth" type="float" alias="nameLabel.width" />
<field id="versionLabelWidth" type="float" alias="versionLabel.width" />
<field id="usersLabelWidth" type="float" alias="usersLabel.width" />
<field id="signupLabelWidth" type="float" alias="signupLabel.width" />
<field id="locationLabelWidth" type="float" alias="locationLabel.width" />
<field id="healthLabelWidth" type="float" alias="healthLabel.width" />
</interface>
<children>
<Label
id="nameLabel"
font="font:SmallestSystemFont"
width="240" />
<Label
id="versionLabel"
font="font:SmallestSystemFont"
width="220"
translation="[240,0]" />
<Label
id="usersLabel"
font="font:SmallestSystemFont"
width="100"
translation="[460,0]" />
<Label
id="signupLabel"
font="font:SmallestSystemFont"
width="90"
translation="[550,0]" />
<Label
id="locationLabel"
font="font:SmallestSystemFont"
width="100"
translation="[650,0]" />
<Poster
id="flagEmoji"
width="16"
height="16"
translation="[650,0]" />
<Label
id="healthLabel"
font="font:SmallestSystemFont"
width="60"
translation="[750,0]" />
<LayoutGroup layoutDirection="horiz">
<Label id="nameLabel" font="font:SmallestSystemFont" />
<Label id="versionLabel" font="font:SmallestSystemFont" />
<Label id="usersLabel" font="font:SmallestSystemFont" />
<Label id="signupLabel" font="font:SmallestSystemFont" />
<Label id="locationLabel" font="font:SmallestSystemFont">
<Poster id="flagEmoji" width="16" height="16" />
</Label>
<Label id="healthLabel" font="font:SmallestSystemFont" />
</LayoutGroup>
</children>
</component>
@@ -8,5 +8,6 @@
<field id="flag" type="string" />
<field id="health" type="string" />
<field id="uri" type="string" />
<field id="fieldWidths" type="assocarray" />
</interface>
</component>
@@ -5,6 +5,7 @@ import "pkg:/source/utils/Logging.bs"
@asynctask
function InvidiousPublicInstancesTask(input as object) as object
listNode = input.listNode
fieldWidths = input.fieldWidths
if m.top.cancel
return invalid
@@ -29,7 +30,7 @@ function InvidiousPublicInstancesTask(input as object) as object
content = CreateObject("roSGNode", "ContentNode")
for each instance in instances
item = CreateListItemContent(instance)
item = CreateListItemContent(instance, fieldWidths)
if item = invalid
continue for
end if
@@ -61,7 +62,7 @@ function FilterInstances(instances as object) as object
return filtered
end function
function CreateListItemContent(instance as object) as object
function CreateListItemContent(instance as object, fieldWidths as object) as object
if not IsArray(instance) or instance.Count() < 2
LogWarn("Invalid instance:", instance)
return invalid
@@ -72,12 +73,13 @@ function CreateListItemContent(instance as object) as object
name: instance[0]
version: DrillIntoObject(instance[1], ["stats", "software", "version"], "N/A")
users: DrillIntoObject(instance[1], ["stats", "usage", "users", "total"], "N/A")
signup: DrillIntoObject(instance[1], ["stats", "openRegistrations"], false) ? "Yes" : "No"
signup: Tr(DrillIntoObject(instance[1], ["stats", "openRegistrations"], false) ? "Yes" : "No")
' Leave 5 spaces for the flag, just to simplify the layout
location: ` ${DrillIntoObject(instance[1], ["region"], "N/A")}`
flag: GetFlagUrl(DrillIntoObject(instance[1], ["flag"], ""))
health: DrillIntoObject(instance[1], ["monitor", "30dRatio", "ratio"], "N/A")
uri: instance[1].uri
fieldWidths: fieldWidths
})
return item