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

@@ -3,6 +3,7 @@ import "pkg:/components/Navigation/LongPress.bs"
import "pkg:/components/VideoQueue/VideoQueueUtils.bs"
import "pkg:/components/VideoQueue/VideoQueueViewUtils.bs"
import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Locale.bs"
import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/Types.bs"
@@ -125,7 +126,7 @@ function onKeyEvent(key as string, press as boolean) as boolean
return true
end if
end if
dialog = DialogUtils.ShowDialogButtons("Do you want to exit Playlet?", "Exit", ["Exit", "Cancel"])
dialog = DialogUtils.ShowDialogButtons("Do you want to exit Playlet?", Tr(Locale.Buttons.exit), [Tr(Locale.Buttons.exit), Tr(Locale.Buttons.Cancel)])
dialog.observeField("buttonSelected", FuncName(OnExitDialog), ["buttonSelected"])
return true
end if
@@ -1,6 +1,8 @@
import "pkg:/source/utils/Locale.bs"
namespace DialogUtils
function ShowDialog(message as dynamic, title as string, large = false as boolean) as object
return ShowDialogButtons(message, title, ["Ok"], large)
return ShowDialogButtons(message, title, [Tr(Locale.Buttons.OK)], large)
end function
function ShowDialogButtons(message as dynamic, title as string, buttons as object, large = false as boolean) as object
@@ -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
@@ -1,5 +1,6 @@
import "pkg:/components/Dialog/DialogPalette.bs"
import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Locale.bs"
import "pkg:/source/utils/Types.bs"
function Init()
@@ -70,7 +71,7 @@ function ShowEditKeyboard()
keyboardDialog.title = m.top.displayText
keyboardDialog.message = [m.top.description]
keyboardDialog.text = m.top.value
keyboardDialog.buttons = ["OK", "Cancel"]
keyboardDialog.buttons = [Tr(Locale.Buttons.OK), Tr(Locale.Buttons.Cancel)]
keyboardDialog.observeField("buttonSelected", FuncName(OnKeyboardButton))
m.top.getScene().dialog = keyboardDialog
@@ -1,4 +1,6 @@
import "pkg:/components/VideoFeed/FeedLoadState.bs"
import "pkg:/source/utils/Locale.bs"
import "pkg:/source/utils/LocaleUtils.bs"
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/TimeUtils.bs"
@@ -41,7 +43,7 @@ namespace InvidiousContent
node.lengthText = TimeUtils.GetFormattedTime(item.lengthSeconds)
node.liveNow = VideoIsLive(item)
VideoSetPremiereTimestampText(node, item)
SetIfExists(node, "publishedText", item, "publishedText")
node.publishedText = VideoGetPublishedText(item)
VideoSetThumbnail(node, item, instance)
SetIfExists(node, "title", item, "title")
SetIfExists(node, "timestamp", item, "timestamp")
@@ -172,10 +174,58 @@ namespace InvidiousContent
return videoItem.isUpcoming = true and ValidInt(videoItem.premiereTimestamp) <> 0
end function
function VideoSetPremiereTimestampText(node as object, videoItem as object)
function VideoSetPremiereTimestampText(node as object, videoItem as object) as void
if VideoIsUpcoming(videoItem)
node.isUpcoming = true
node.premiereTimestampText = `Premeres in ${TimeUtils.GetFormattedTimeLeft(videoItem.premiereTimestamp)}`
timestamp = videoItem.premiereTimestamp
if timestamp = invalid
node.premiereTimestampText = "N/A"
return
end if
currentTime = TimeUtils.Now().AsSeconds()
timeLeft = timestamp - currentTime
if timeLeft <= 0
node.premiereTimestampText = Tr(Locale.RowCell.PremieringNow)
return
end if
if timeLeft < 60
if timeLeft = 1
node.premiereTimestampText = Tr(Locale.RowCell.PremieresInOneSecond)
else
node.premiereTimestampText = Tr(Locale.RowCell.PremieresInNSeconds).Replace("^n", timeLeft.toStr())
end if
return
end if
if timeLeft < 3600
minutes = timeLeft \ 60
if minutes = 1
node.premiereTimestampText = Tr(Locale.RowCell.PremieresInOneMinute)
else
node.premiereTimestampText = Tr(Locale.RowCell.PremieresInNMinutes).Replace("^n", minutes.toStr())
end if
return
end if
if timeLeft < 86400
hours = timeLeft \ 3600
if hours = 1
node.premiereTimestampText = Tr(Locale.RowCell.PremieresInOneHour)
else
node.premiereTimestampText = Tr(Locale.RowCell.PremieresInNHours).Replace("^n", hours.toStr())
end if
return
end if
days = timeLeft \ 86400
if days = 1
node.premiereTimestampText = Tr(Locale.RowCell.PremieresInOneDay)
else
node.premiereTimestampText = Tr(Locale.RowCell.PremieresInNDays).Replace("^n", days.toStr())
end if
end if
end function
@@ -184,30 +234,8 @@ namespace InvidiousContent
if viewCount = invalid
return ""
end if
if viewCount < 1000
return `${FormatFloat(viewCount)} views`
end if
viewCount = viewCount / 1000
if viewCount < 1000
return `${FormatFloat(viewCount)}K views`
end if
viewCount = viewCount / 1000
if viewCount < 1000
return `${FormatFloat(viewCount)}M views`
end if
viewCount = viewCount / 1000
return `${FormatFloat(viewCount)}B views`
end function
function FormatFloat(X as float) as float
X = X * 10
X = X + 0.5
X = Fix(X)
X = X / 10
return X
return LocaleUtils.GetFormattedPluralString(viewCount, Locale.RowCell.ZeroViews, Locale.RowCell.OneView, Locale.RowCell.NViews)
end function
function PlaylistSetThumbnail(node as object, nodeField as string, playlistItem as object, instance as dynamic, quality = "medium" as string) as void
@@ -251,40 +279,72 @@ namespace InvidiousContent
function GetVideoCountText(item as object) as string
videoCount = ValidInt(item.videoCount)
if videoCount = 1
return "1 video"
end if
return `${videoCount} videos`
return LocaleUtils.GetPluralString(videoCount, Locale.RowCell.ZeroVideos, Locale.RowCell.OneVideo, Locale.RowCell.NVideos)
end function
function GetChannelSubCountText(channel as object) as string
subCount = ValidInt(channel.subCount)
if subCount = 1
return "1 subscriber"
end if
if subCount < 1000
return `${subCount} subscribers`
end if
subCount = subCount / 1000
if subCount < 1000
return `${FormatFloat(subCount)}K subscribers`
end if
subCount = subCount / 1000
if subCount < 1000
return `${FormatFloat(subCount)}M subscribers`
end if
subCount = subCount / 1000
return `${FormatFloat(subCount)}B subscribers`
return LocaleUtils.GetFormattedPluralString(subCount, Locale.RowCell.ZeroSubscribers, Locale.RowCell.OneSubscriber, Locale.RowCell.NSubscribers)
end function
function PlaylistGetViewCountText(playlistItem as object) as string
return VideoGetViewCountText(playlistItem)
end function
function VideoGetPublishedText(videoItem as object) as string
published = ValidInt(videoItem.published)
if published = 0
return ValidString(videoItem.publishedText)
end if
span = TimeUtils.Now().AsSeconds() - published
if span < 1
return ValidString(videoItem.publishedText)
end if
total_days = span \ 86400
if total_days > 365
years = total_days \ 365
if years = 1
return Tr(Locale.RowCell.OneYearAgo)
else
return Tr(Locale.RowCell.NYearsAgo).Replace("^n", years.toStr())
end if
else if total_days > 30
months = total_days \ 30
if months = 1
return Tr(Locale.RowCell.OneMonthAgo)
else
return Tr(Locale.RowCell.NMonthsAgo).Replace("^n", months.toStr())
end if
else if total_days > 7
weeks = total_days \ 7
if weeks = 1
return Tr(Locale.RowCell.OneWeekAgo)
else
return Tr(Locale.RowCell.NWeeksAgo).Replace("^n", weeks.toStr())
end if
else if total_days > 1
return Tr(Locale.RowCell.NDaysAgo).Replace("^n", total_days.toStr())
else if span > 3600
hours = span \ 3600
if hours = 1
return Tr(Locale.RowCell.OneHourAgo)
else
return Tr(Locale.RowCell.NHoursAgo).Replace("^n", hours.toStr())
end if
else if span > 60
minutes = span \ 60
if minutes = 1
return Tr(Locale.RowCell.OneMinuteAgo)
else
return Tr(Locale.RowCell.NMinutesAgo).Replace("^n", minutes.toStr())
end if
else
return Tr(Locale.RowCell.OneMinuteAgo)
end if
end function
function PlaylistGetUpdatedText(playlistItem as object) as string
updated = playlistItem.updated
if not IsInt(updated)
@@ -295,32 +355,46 @@ namespace InvidiousContent
return ""
end if
count = ""
total_days = span \ 86400
if total_days > 365
years = total_days \ 365
count = years > 1 ? `${years} years` : "1 year"
if years = 1
return Tr(Locale.RowCell.UpdatedOneYearAgo)
else
return Tr(Locale.RowCell.UpdatedNYearsAgo).Replace("^n", years.toStr())
end if
else if total_days > 30
months = total_days \ 30
count = months > 1 ? `${months} months` : "1 month"
if months = 1
return Tr(Locale.RowCell.UpdatedOneMonthAgo)
else
return Tr(Locale.RowCell.UpdatedNMonthsAgo).Replace("^n", months.toStr())
end if
else if total_days > 7
weeks = total_days \ 7
count = weeks > 1 ? `${weeks} weeks` : "1 week"
if weeks = 1
return Tr(Locale.RowCell.UpdatedOneWeekAgo)
else
return Tr(Locale.RowCell.UpdatedNWeeksAgo).Replace("^n", weeks.toStr())
end if
else if total_days > 1
count = `${total_days} days`
return Tr(Locale.RowCell.UpdatedNDaysAgo).Replace("^n", total_days.toStr())
else if span > 3600
hours = span \ 3600
count = hours > 1 ? `${hours} hours` : "1 hour"
if hours = 1
return Tr(Locale.RowCell.UpdatedOneHourAgo)
else
return Tr(Locale.RowCell.UpdatedNHoursAgo).Replace("^n", hours.toStr())
end if
else if span > 60
minutes = span \ 60
count = minutes > 1 ? `${minutes} minutes` : "1 minute"
if minutes = 1
return Tr(Locale.RowCell.UpdatedOneMinuteAgo)
else
return Tr(Locale.RowCell.UpdatedNMinutesAgo).Replace("^n", minutes.toStr())
end if
else
count = span > 1 ? `${span} seconds` : "1 second"
return Tr(Locale.RowCell.UpdatedOneMinuteAgo)
end if
if StringUtils.IsNullOrEmpty(count)
return ""
end if
return `Updated ${count} ago`
end function
end namespace
@@ -1,3 +1,4 @@
import "pkg:/source/utils/Locale.bs"
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/TimeUtils.bs"
import "pkg:/source/utils/Types.bs"
@@ -5,6 +6,8 @@ import "pkg:/source/utils/Types.bs"
function Init()
m.durationRect = m.top.FindNode("durationRect")
m.durationLabel = m.top.FindNode("durationLabel")
m.smallestSystemFont = m.top.FindNode("smallestSystemFont")
m.smallestBoldSystemFont = m.top.FindNode("smallestBoldSystemFont")
end function
function OnContentSet() as void
@@ -32,23 +35,25 @@ function OnContentSet() as void
m.top.thumbnailUri = content.thumbnail
m.top.durationRectVisible = false
m.top.upcomingRectVisible = false
m.top.liveRectVisible = false
if ValidInt(content.lengthSeconds) <> 0
SetDurationText(content.lengthText)
' bs:disable-next-line LINT3023
SetDurationLabel(content.lengthText, "#000000FF", 0.8, m.smallestSystemFont)
m.top.durationRectVisible = true
else
if content.isUpcoming = true
m.top.upcomingRectVisible = true
' bs:disable-next-line LINT3023
SetDurationLabel(Tr(Locale.RowCell.Upcoming), "#000000FF", 0.8, m.smallestBoldSystemFont)
m.top.durationRectVisible = true
else if content.liveNow
m.top.liveRectVisible = true
SetDurationLabel(Tr(Locale.RowCell.Live), "#CC0000FF", 0.9, m.smallestBoldSystemFont)
m.top.durationRectVisible = true
else
m.top.durationRectVisible = false
end if
end if
end function
function SetDurationText(text as string) as void
function SetDurationLabel(text as string, bgColor as string, bgOpacity as float, font as object) as void
m.top.duration = text
label = m.durationLabel
@@ -59,10 +64,13 @@ function SetDurationText(text as string) as void
end if
rectParent = rect.getParent()
label.font = font
size = label.localBoundingRect()
rect.width = size.width + 16
rect.translation = [rectParent.width - rect.width, rect.translation[1]]
rect.color = bgColor
rect.opacity = bgOpacity
end function
@@ -5,13 +5,14 @@
<field id="title" type="string" alias="titleLabel.text" />
<field id="author" type="string" alias="authorLabel.text" />
<field id="viewCountDate" type="string" alias="viewCountDateLabel.text" />
<field id="liveRectVisible" type="boolean" alias="liveRect.visible" />
<field id="upcomingRectVisible" type="boolean" alias="upcomingRect.visible" />
<field id="durationRectVisible" type="boolean" alias="durationRect.visible" />
<field id="duration" type="string" alias="durationLabel.text" />
<field id="thumbnailUri" type="string" alias="thumbnail.uri" />
</interface>
<children>
<Font id="smallestSystemFont" uri="font:SystemFontFile" size="18" />
<Font id="smallestBoldSystemFont" uri="font:BoldSystemFontFile" size="18" />
<LayoutGroup
itemSpacings="[24,5,5]">
<Poster
@@ -35,35 +36,6 @@
font="font:SmallestSystemFont"
translation="[8, 0]" />
</Rectangle>
<Rectangle
id="liveRect"
width="60"
height="25"
color="0xFF0000"
translation="[290, 162]">
<Label
width="60"
height="25"
horizAlign="center"
vertAlign="center"
font="font:SmallestBoldSystemFont"
text="LIVE" />
</Rectangle>
<Rectangle
id="upcomingRect"
width="120"
height="25"
color="0x000000"
opacity="0.8"
translation="[230, 162]">
<Label
width="120"
height="25"
horizAlign="center"
vertAlign="center"
font="font:SmallestBoldSystemFont"
text="UPCOMING" />
</Rectangle>
</Poster>
<Label
id="titleLabel"
@@ -93,7 +93,7 @@ namespace Http
return true
end if
response.Default(200, "Ok")
response.Default(200, "OK")
return true
end function