forked from MapComplete/MapComplete
Add typings to translations, move Subs into 'TypedTranslations', cleanup of duplicate parts in translation files, fix #752
This commit is contained in:
parent
f5d5f304ae
commit
e391c1ce20
12 changed files with 64 additions and 318 deletions
|
@ -1,9 +1,6 @@
|
|||
import Locale from "./Locale";
|
||||
import {Utils} from "../../Utils";
|
||||
import BaseUIElement from "../BaseUIElement";
|
||||
import Link from "../Base/Link";
|
||||
import Svg from "../../Svg";
|
||||
import {VariableUiElement} from "../Base/VariableUIElement";
|
||||
import LinkToWeblate from "../Base/LinkToWeblate";
|
||||
|
||||
export class Translation extends BaseUIElement {
|
||||
|
@ -164,25 +161,7 @@ export class Translation extends BaseUIElement {
|
|||
public AllValues(): string[] {
|
||||
return this.SupportedLanguages().map(lng => this.translations[lng]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Substitutes text in a translation.
|
||||
* If a translation is passed, it'll be fused
|
||||
*
|
||||
* // Should replace simple keys
|
||||
* new Translation({"en": "Some text {key}"}).Subs({key: "xyz"}).textFor("en") // => "Some text xyz"
|
||||
*
|
||||
* // Should fuse translations
|
||||
* const subpart = new Translation({"en": "subpart","nl":"onderdeel"})
|
||||
* const tr = new Translation({"en": "Full sentence with {part}", nl: "Volledige zin met {part}"})
|
||||
* const subbed = tr.Subs({part: subpart})
|
||||
* subbed.textFor("en") // => "Full sentence with subpart"
|
||||
* subbed.textFor("nl") // => "Volledige zin met onderdeel"
|
||||
*/
|
||||
public Subs(text: any, context?: string): Translation {
|
||||
return this.OnEveryLanguage((template, lang) => Utils.SubstituteKeys(template, text, lang), context)
|
||||
}
|
||||
|
||||
|
||||
public OnEveryLanguage(f: (s: string, language: string) => string, context?: string): Translation {
|
||||
const newTranslations = {};
|
||||
for (const lang in this.translations) {
|
||||
|
@ -278,5 +257,28 @@ export class Translation extends BaseUIElement {
|
|||
return this.txt
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class TypedTranslation<T> extends Translation {
|
||||
constructor(translations: object, context?: string) {
|
||||
super(translations, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Substitutes text in a translation.
|
||||
* If a translation is passed, it'll be fused
|
||||
*
|
||||
* // Should replace simple keys
|
||||
* new TypedTranslation<object>({"en": "Some text {key}"}).Subs({key: "xyz"}).textFor("en") // => "Some text xyz"
|
||||
*
|
||||
* // Should fuse translations
|
||||
* const subpart = new Translation({"en": "subpart","nl":"onderdeel"})
|
||||
* const tr = new TypedTranslation<object>({"en": "Full sentence with {part}", nl: "Volledige zin met {part}"})
|
||||
* const subbed = tr.Subs({part: subpart})
|
||||
* subbed.textFor("en") // => "Full sentence with subpart"
|
||||
* subbed.textFor("nl") // => "Volledige zin met onderdeel"
|
||||
*/
|
||||
Subs(text: T, context?: string): Translation {
|
||||
return this.OnEveryLanguage((template, lang) => Utils.SubstituteKeys(template, text, lang), context)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
import {FixedUiElement} from "../Base/FixedUiElement";
|
||||
import {Translation} from "./Translation";
|
||||
import {Translation, TypedTranslation} from "./Translation";
|
||||
import BaseUIElement from "../BaseUIElement";
|
||||
import * as known_languages from "../../assets/generated/used_languages.json"
|
||||
import CompiledTranslations from "../../assets/generated/CompiledTranslations";
|
||||
|
@ -22,7 +22,7 @@ export default class Translations {
|
|||
return s;
|
||||
}
|
||||
|
||||
static T(t: string | any, context = undefined): Translation {
|
||||
static T(t: string | any, context = undefined): TypedTranslation<object> {
|
||||
if (t === undefined || t === null) {
|
||||
return undefined;
|
||||
}
|
||||
|
@ -30,17 +30,17 @@ export default class Translations {
|
|||
t = "" + t
|
||||
}
|
||||
if (typeof t === "string") {
|
||||
return new Translation({"*": t}, context);
|
||||
return new TypedTranslation({"*": t}, context);
|
||||
}
|
||||
if (t.render !== undefined) {
|
||||
const msg = "Creating a translation, but this object contains a 'render'-field. Use the translation directly"
|
||||
console.error(msg, t);
|
||||
throw msg
|
||||
}
|
||||
if (t instanceof Translation) {
|
||||
if (t instanceof TypedTranslation) {
|
||||
return t;
|
||||
}
|
||||
return new Translation(t, context);
|
||||
return new TypedTranslation(t, context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue