2024-05-23 15:17:28 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 Mehdi Benadel <https://mehdibenadel.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-06-11 10:28:08 +00:00
|
|
|
import type { TemplateResult } from 'lit'
|
2024-05-24 12:25:08 +00:00
|
|
|
import { PartInfo, directive } from 'lit/directive.js'
|
2024-05-13 14:40:36 +00:00
|
|
|
import { AsyncDirective } from 'lit/async-directive.js'
|
2024-05-23 20:52:39 +00:00
|
|
|
import { RegisterClientHelpers } from '@peertube/peertube-types/client'
|
|
|
|
import { unsafeHTML } from 'lit/directives/unsafe-html.js'
|
|
|
|
import { html } from 'lit'
|
2024-05-24 12:25:08 +00:00
|
|
|
import { registerClientOptionsSubject$ } from '../contexts/peertube'
|
|
|
|
import { Subscription, filter, map } from 'rxjs'
|
2024-05-13 14:40:36 +00:00
|
|
|
|
|
|
|
export class TranslationDirective extends AsyncDirective {
|
2024-05-24 12:25:08 +00:00
|
|
|
private _peertubeHelpers?: RegisterClientHelpers
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private _translatedValue: string = ''
|
|
|
|
private _localizationId: string = ''
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private _allowUnsafeHTML = false
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-24 12:25:08 +00:00
|
|
|
private _subscriptionHandle: Subscription = new Subscription()
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-24 12:25:08 +00:00
|
|
|
constructor (partInfo: PartInfo) {
|
|
|
|
super(partInfo)
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-24 12:25:08 +00:00
|
|
|
this.reconnected()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override disconnected = (): void => {
|
|
|
|
this._subscriptionHandle.unsubscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override reconnected = (): void => {
|
|
|
|
this._subscriptionHandle.unsubscribe()
|
|
|
|
this._subscriptionHandle = registerClientOptionsSubject$
|
|
|
|
.pipe(filter(Boolean))
|
|
|
|
.pipe(map(registerClientOptions => registerClientOptions.peertubeHelpers))
|
|
|
|
.subscribe((registerClientHelpers: RegisterClientHelpers) => {
|
|
|
|
this._peertubeHelpers = registerClientHelpers
|
|
|
|
this._asyncUpdateTranslation().then(() => {}, () => {})
|
|
|
|
})
|
|
|
|
}
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-06-11 10:28:08 +00:00
|
|
|
public override render = (locId: string, allowHTML: boolean = false): TemplateResult | string => {
|
2024-05-23 20:52:39 +00:00
|
|
|
this._localizationId = locId // TODO Check current component for context (to infer the prefix)
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
this._allowUnsafeHTML = allowHTML
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
if (this._translatedValue === '') {
|
|
|
|
this._translatedValue = locId
|
|
|
|
}
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
this._asyncUpdateTranslation().then(() => {}, () => {})
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
return this._internalRender()
|
|
|
|
}
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-06-11 10:28:08 +00:00
|
|
|
private readonly _internalRender = (): TemplateResult | string => {
|
2024-05-23 20:52:39 +00:00
|
|
|
return this._allowUnsafeHTML ? html`${unsafeHTML(this._translatedValue)}` : this._translatedValue
|
|
|
|
}
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _asyncUpdateTranslation = async (): Promise<true> => {
|
2024-06-06 08:26:39 +00:00
|
|
|
if (!this._peertubeHelpers) {
|
|
|
|
console.error('Translation directive: missing peertubeHelpers')
|
|
|
|
}
|
2024-05-23 20:52:39 +00:00
|
|
|
const newValue = await this._peertubeHelpers?.translate(this._localizationId) ?? ''
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
if (newValue !== '' && newValue !== this._translatedValue) {
|
|
|
|
this._translatedValue = newValue
|
|
|
|
this.setValue(this._internalRender())
|
2024-05-13 14:40:36 +00:00
|
|
|
}
|
2024-05-23 20:52:39 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2024-05-13 14:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const ptTr = directive(TranslationDirective)
|