From 7aee442ae49a7a68821917de9580ce9e62105edf Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sat, 2 Sep 2023 00:46:17 +0200 Subject: [PATCH] Fix: improve first-sentence --- src/UI/i18n/Translation.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/UI/i18n/Translation.ts b/src/UI/i18n/Translation.ts index d55fb9604..7a8eb6be3 100644 --- a/src/UI/i18n/Translation.ts +++ b/src/UI/i18n/Translation.ts @@ -226,16 +226,26 @@ export class Translation extends BaseUIElement { return new Translation(this.translations, this.context) } - FirstSentence() { + /** + * Build a new translation which only contains the first sentence of every language + * A sentence stops at either a dot (`.`) or a HTML-break ('
'). + * The dot or linebreak are _not_ returned. + * + * new Translation({"en": "This is a sentence. This is another sentence"}).FirstSentence().textFor("en") // "This is a sentence" + * new Translation({"en": "This is a sentence
This is another sentence"}).FirstSentence().textFor("en") // "This is a sentence" + * new Translation({"en": "This is a sentence with a bold word. This is another sentence"}).FirstSentence().textFor("en") // "This is a sentence with a bold word" + * @constructor + */ + public FirstSentence(): Translation { const tr = {} for (const lng in this.translations) { if (!this.translations.hasOwnProperty(lng)) { continue } let txt = this.translations[lng] - txt = txt.replace(/[.<].*/, "") + txt = txt.replace(/(\.|).*/, "") txt = Utils.EllipsesAfter(txt, 255) - tr[lng] = txt + tr[lng] = txt.trim() } return new Translation(tr)