2024-05-23 15:17:28 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 Mehdi Benadel <https://mehdibenadel.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-05-23 12:41:11 +00:00
|
|
|
import { PartInfo, directive } from 'lit/directive.js'
|
2024-05-13 14:40:36 +00:00
|
|
|
import { AsyncDirective } from 'lit/async-directive.js'
|
|
|
|
import { RegisterClientHelpers } from '@peertube/peertube-types/client';
|
2024-05-23 00:26:38 +00:00
|
|
|
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
|
|
|
|
import { html } from 'lit';
|
2024-05-13 14:40:36 +00:00
|
|
|
|
|
|
|
export class TranslationDirective extends AsyncDirective {
|
|
|
|
|
|
|
|
private _peertubeHelpers?: RegisterClientHelpers
|
|
|
|
|
|
|
|
private _translatedValue : string = ''
|
|
|
|
private _localizationId : string = ''
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
private _allowUnsafeHTML = false
|
|
|
|
|
2024-05-13 14:40:36 +00:00
|
|
|
constructor(partInfo: PartInfo) {
|
|
|
|
super(partInfo);
|
|
|
|
|
|
|
|
//_peertubeOptionsPromise.then((options) => this._peertubeHelpers = options.peertubeHelpers)
|
|
|
|
}
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
// update = (part: ElementPart) => {
|
|
|
|
// if (part) console.log(`Element : ${part?.element?.getAttributeNames?.().join(' ')}`);
|
|
|
|
// return this.render(this._localizationId);
|
|
|
|
// }
|
|
|
|
|
|
|
|
override render = (locId: string, allowHTML: boolean = false) => {
|
2024-05-13 14:40:36 +00:00
|
|
|
this._localizationId = locId // TODO Check current component for context (to infer the prefix)
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
this._allowUnsafeHTML = allowHTML
|
|
|
|
|
2024-05-13 14:40:36 +00:00
|
|
|
if (this._translatedValue === '') {
|
|
|
|
this._translatedValue = locId
|
|
|
|
}
|
|
|
|
|
2024-05-23 12:41:11 +00:00
|
|
|
this._asyncUpdateTranslation().then(() => {}, () => {})
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
return this._internalRender()
|
|
|
|
}
|
|
|
|
|
|
|
|
_internalRender = () => {
|
|
|
|
return this._allowUnsafeHTML ? html`${unsafeHTML(this._translatedValue)}` : this._translatedValue
|
2024-05-13 14:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_asyncUpdateTranslation = async () => {
|
|
|
|
let newValue = await this._peertubeHelpers?.translate(this._localizationId) ?? ''
|
|
|
|
|
|
|
|
if (newValue !== '' && newValue !== this._translatedValue) {
|
|
|
|
this._translatedValue = newValue
|
2024-05-23 00:26:38 +00:00
|
|
|
this.setValue(this._internalRender())
|
2024-05-13 14:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ptTr = directive(TranslationDirective)
|
|
|
|
|