forked from MapComplete/MapComplete
Fix: improve first-sentence
This commit is contained in:
parent
f207736e40
commit
7aee442ae4
1 changed files with 13 additions and 3 deletions
|
@ -226,16 +226,26 @@ export class Translation extends BaseUIElement {
|
||||||
return new Translation(this.translations, this.context)
|
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 ('<br/>').
|
||||||
|
* 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 <br/> This is another sentence"}).FirstSentence().textFor("en") // "This is a sentence"
|
||||||
|
* new Translation({"en": "This is a sentence with a <b>bold</b> word. This is another sentence"}).FirstSentence().textFor("en") // "This is a sentence with a <b>bold</b> word"
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
public FirstSentence(): Translation {
|
||||||
const tr = {}
|
const tr = {}
|
||||||
for (const lng in this.translations) {
|
for (const lng in this.translations) {
|
||||||
if (!this.translations.hasOwnProperty(lng)) {
|
if (!this.translations.hasOwnProperty(lng)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
let txt = this.translations[lng]
|
let txt = this.translations[lng]
|
||||||
txt = txt.replace(/[.<].*/, "")
|
txt = txt.replace(/(\.|<br\/>).*/, "")
|
||||||
txt = Utils.EllipsesAfter(txt, 255)
|
txt = Utils.EllipsesAfter(txt, 255)
|
||||||
tr[lng] = txt
|
tr[lng] = txt.trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Translation(tr)
|
return new Translation(tr)
|
||||||
|
|
Loading…
Add table
Reference in a new issue