refactor: overhaul translator as mentioned in #118

This commit is contained in:
Rainer Killinger
2021-01-05 15:22:31 +01:00
parent cbcf9c9adb
commit d5f3d71a41
5 changed files with 1030 additions and 696 deletions

5
.mailmap Normal file
View File

@@ -0,0 +1,5 @@
Rainer Killinger <mail-openstapps@killinger.co> Rainer Killinger <git@killinger.co>
Rainer Killinger <mail-openstapps@killinger.co> Rainer Killinger <killinge@hrz.uni-frankfurt.de>
Rainer Killinger <mail-openstapps@killinger.co> Rainer Killinger <killinger@hrz.uni-frankfurt.de>
Wieland Schöbl <wulkanat@gmail.com> wulkanat@gmail.com <wulkanat@gmail.com>
Wieland Schöbl <wulkanat@gmail.com> Wieland Schöbl <wieland.schoebl@campus.tu-berlin.de>

1411
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -32,14 +32,15 @@
"contributors": [
"Andreas Lehmann",
"Anselm Stordeur <anselmstordeur@gmail.com>",
"Axel Nieder-Vahrenholz",
"Benjamin Jöckel",
"Imran Hossain",
"Frank Nagel",
"Jovan Krunić <jovan.krunic@gmail.com>",
"Michel Jonathan Schmitz",
"Rainer Killinger <mail-openstapps@killinger.co>",
"Roman Klopsch",
"Sebastian Lange",
"Wieland Schöbl",
"Roman Klopsch"
"Wieland Schöbl"
],
"dependencies": {
"@openstapps/core-tools": "0.16.0",
@@ -48,6 +49,7 @@
"@types/json-schema": "7.0.6",
"@types/node": "10.17.44",
"fast-clone": "1.5.13",
"fast-deep-equal": "3.1.3",
"http-status-codes": "2.1.4",
"json-patch": "0.7.0",
"json-schema": "0.2.5",

View File

@@ -13,8 +13,9 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import clone = require('fast-clone');
import equal = require('fast-deep-equal/es6');
import {Defined, TSOCType} from 'ts-optchain';
import {SCTranslations} from './general/i18n';
import {SCLanguageCode} from './general/i18n';
import {isThing} from './guards';
import {SCClasses} from './meta';
import {SCThing, SCThingType} from './things/abstract/thing';
@@ -27,13 +28,32 @@ const standardCacheSize = 200;
*/
export class SCThingTranslator {
/**
* Getter for language property
*/
get language(): SCLanguageCode {
return this._language;
}
/**
* Setter for language property. Also flushes translation cache
*
* @param language The language the translator instance will use from now on
*/
set language(language: SCLanguageCode) {
if (language !== this._language) {
this.cache.flush();
}
this._language = language;
}
/**
* Property representing the translators target language
*/
private _language: keyof SCTranslations<SCThing>;
private _language: SCLanguageCode;
/**
* Property representing the translators base language
* This means every translation is given for this language
* LRU cache containing already translated SCThings
*/
private readonly cache: LRUCache<SCThing>;
@@ -42,36 +62,23 @@ export class SCThingTranslator {
*/
private readonly metaClasses: typeof SCClasses;
/**
* LRU cache containing SCThings translations have been provided for
*/
private readonly sourceCache: LRUCache<SCThing>;
/**
* @example
* // returns translator instance for german
* new SCThingTranslator('de');
*/
constructor(language: keyof SCTranslations<SCThing>, cacheCapacity: number = standardCacheSize) {
constructor(language: SCLanguageCode, cacheCapacity: number = standardCacheSize) {
this.cache = new LRUCache(cacheCapacity);
this.sourceCache = new LRUCache(cacheCapacity);
this._language = language;
this.metaClasses = SCClasses;
}
/**
* Getter for language property
*/
get language(): keyof SCTranslations<SCThing> {
return this._language;
}
/**
* Setter for language property. Also flushes translation cache
*
* @param language The language the translator instance will use from now on
*/
set language(language: keyof SCTranslations<SCThing>) {
if (language !== this._language) {
this.cache.flush();
}
this._language = language;
}
/**
* Get field value translation recursively
*
@@ -101,8 +108,8 @@ export class SCThingTranslator {
* @param language The language the thing property values are translated to
* @returns The thing with all known meta values translated
*/
private getAllMetaFieldTranslations<T extends SCThing>(thingType: SCThingType,
language: keyof SCTranslations<T>): object | undefined {
private getAllMetaFieldTranslations(thingType: SCThingType,
language: SCLanguageCode): object | undefined {
const fieldTranslations = {};
const metaClass = this.getMetaClassInstance(thingType);
if (typeof metaClass === 'undefined') {
@@ -151,7 +158,7 @@ export class SCThingTranslator {
* @returns The thing with translated meta field values
*/
private replaceAvailableMetaFieldValueTranslations(instance: any,
language: keyof SCTranslations<any>): any {
language: SCLanguageCode): any {
const metaClass = this.getMetaClassInstance(instance.type);
if (typeof metaClass === 'undefined') {
return instance;
@@ -181,6 +188,60 @@ export class SCThingTranslator {
return instance;
}
/**
* Recursively translates the given object in-place
* Translated values overwrite current values (destructive)
*
* @param instance The thing / object that will be translated
* @returns The thing translated
*/
private translateThingInPlaceDestructively<T>(instance: T): T {
const targetLanguage = this.language;
let nextInstance = instance as any;
// Recursively call this function on all nested SCThings, arrays and objects
Object.keys(nextInstance)
.forEach((key) => {
if (
isThing(nextInstance[key]) ||
nextInstance[key] instanceof Array ||
nextInstance[key] instanceof Object) {
nextInstance[key] = this.translateThingInPlaceDestructively(nextInstance[key]);
}
});
// Spread variable translations given by the connector into thing
if (typeof nextInstance.translations?.[targetLanguage] !== 'undefined') {
nextInstance = {...nextInstance, ...nextInstance.translations![targetLanguage]} as T;
}
// Spread known translations from meta classes into (partly) translated thing
this.replaceAvailableMetaFieldValueTranslations(nextInstance, targetLanguage);
return nextInstance;
}
/**
* Recursively translates the given object in-place
* Translated values overwrite current values (destructive)
*
* @param instance The thing / object that will be translated
* @returns The thing translated
*/
public translate<T extends SCThing>(thing: T): T {
if (equal(this.sourceCache.get(thing), thing)) {
const cachedInstance = this.cache.get(thing);
if (typeof cachedInstance !== 'undefined') {
return cachedInstance as T;
}
}
const translatedInstance = this.translateThingInPlaceDestructively(clone(thing));
delete translatedInstance.translations;
this.cache.putObject(translatedInstance);
this.sourceCache.putObject(thing);
return translatedInstance as T;
}
/**
* Get field value translation recursively
* @example
@@ -191,21 +252,24 @@ export class SCThingTranslator {
* dishTranslatedAccess.offers[0].inPlace.categories[1]();
* // undoing the TSOCType<T>
* const dishAsBefore: SCDish = dishTranslatedAccess()!;
* @param data Top level object that gets passed through the recursion
* @param thing Top level object that gets passed through the recursion
* @returns an TSOCType<T> object allowing for access to translations or a translated value(s)
*/
public translate<T extends SCThing>(data: T): TSOCType<T> {
public translatedAccess<T extends SCThing>(thing: T): TSOCType<T> {
return new Proxy(
((defaultValue?: Defined<T>) => (data == null ? defaultValue : data)) as TSOCType<T>,
((defaultValue?: Defined<T>) => (thing == null ? defaultValue : thing)) as TSOCType<T>,
{
get: (target, key) => {
const obj: any = target();
const objTranslatedFromCache = this.cache.get(data);
if (typeof objTranslatedFromCache !== 'undefined') {
return this.deeptranslate((objTranslatedFromCache as any)[key]);
if (equal(this.sourceCache.get(thing), thing)) {
const objTranslatedFromCache = this.cache.get(thing);
if (typeof objTranslatedFromCache !== 'undefined') {
return this.deeptranslate((objTranslatedFromCache as any)[key]);
}
}
const objTranslated = this.translateWholeThingDestructively(clone(obj));
this.cache.putObject(objTranslated);
this.sourceCache.putObject(thing);
return this.deeptranslate(objTranslated[key]);
},
@@ -219,69 +283,31 @@ export class SCThingTranslator {
* @example
* const translatedMetaDish = translator.translatedPropertyNames<SCCourseOfStudies>(SCThingType.CourseOfStudies);
* @param type The type whose property names will be translated
* @param language The language all property names will be translated to
* @returns An object with the properties of the SCThingType where the values are the known property tranlations
*/
public translatedPropertyNames<T extends SCThing>(type: SCThingType,
language?: keyof SCTranslations<T>): T | undefined {
return this.getAllMetaFieldTranslations(type, language ?? this.language) as T;
public translatedPropertyNames<T extends SCThing>(type: SCThingType): T | undefined {
return this.getAllMetaFieldTranslations(type, this.language) as T;
}
/**
* Given a SCThingType and a corresponding property name it returns the known property value translation
* Access pattern to the meta object containing the translation can be thought of as type.field[key] with key being optional
* @example
* const translatedMetaDish = translator.translatedPropertyNames(SCThingType.Dish, 'categories', 'main dish');
* const singleValueTranslation = translator.translatedPropertyValue(SCThingType.Dish, 'categories', 'main dish');
* @param type The type for whose property values a translation is required
* @param field The property for which a translation is required
* @param key If specified tries to access the field with this key
* @param language The language all property names will be translated to
* @returns Known translation for the property
*/
public translatedPropertyValue<T extends unknown>(type: SCThingType,
field: string,
key?: string,
language?: keyof SCTranslations<T>): string | undefined {
const fieldTranslation = this.getMetaClassInstance(type).fieldValueTranslations[language ?? this.language]?.[field];
public translatedPropertyValue(type: SCThingType,
field: string,
key?: string): string | undefined {
const fieldValueTranslations = this.getMetaClassInstance(type).fieldValueTranslations[this.language] ??
this.getMetaClassInstance(type).fieldValueTranslations.en;
const fieldTranslation = fieldValueTranslations?.[field];
return fieldTranslation?.[key ?? ''] ?? key ?? fieldTranslation;
return fieldTranslation?.[key ?? ''] ?? key ?? fieldTranslation;
}
/**
* Recursively translates the given object in-place
* Translated values overwrite current values (destructive)
*
* @param instance The thing / object that will be translated
* @param language The language the thing / object is translated to
* @returns The thing translated
*/
public translateWholeThingDestructively(instance: any,
language?: keyof SCTranslations<any>): any {
const targetLanguage = (typeof language !== 'undefined') ? language : this.language;
let nextInstance = instance;
// Recursively call this function on all nested SCThings, arrays and objects
Object.keys(nextInstance)
.forEach((key) => {
if (
isThing((nextInstance as any)[key]) ||
nextInstance[key] instanceof Array ||
nextInstance[key] instanceof Object) {
nextInstance[key] = this.translateWholeThingDestructively(nextInstance[key], targetLanguage);
}
});
// Spread variable translations given by the connector into thing
if (typeof nextInstance.translations !== 'undefined') {
if (typeof nextInstance.translations![targetLanguage] !== 'undefined') {
nextInstance = {...nextInstance, ...nextInstance.translations![targetLanguage]} as typeof instance;
}
}
// Spread known translations from meta classes into (partly) translated thing
this.replaceAvailableMetaFieldValueTranslations(nextInstance, targetLanguage);
return nextInstance;
}
}
/**

View File

@@ -15,7 +15,7 @@
import {expect} from 'chai';
import clone = require('fast-clone');
import {slow, suite, test, timeout} from '@testdeck/mocha';
import {SCThingOriginType, SCThingType} from '../src/things/abstract/thing';
import {SCThingOriginType, SCThingType, SCThingRemoteOrigin} from '../src/things/abstract/thing';
import {SCBuildingWithoutReferences} from '../src/things/building';
import {SCDish, SCDishMeta} from '../src/things/dish';
import {SCSetting, SCSettingInputType} from '../src/things/setting';
@@ -105,142 +105,182 @@ const setting: SCSetting = {
};
const translator = new SCThingTranslator('de');
// tslint:disable-next-line:no-eval
const languageNonExistant = eval("'jp'");
const translatorEN = new SCThingTranslator('en');
// this will simulate a translator always utilizing the base language translations
const translatorWithFallback = new SCThingTranslator(languageNonExistant);
const translatorWithFallback = new SCThingTranslator('tt');
const translatedThingDE = translator.translate(dish);
const translatedThingFallback = translatorWithFallback.translate(dish);
// tslint:disable:max-line-length member-ordering newline-per-chained-call prefer-function-over-method completed-docs TranslationSpecInplace
@suite(timeout(10000), slow(5000))
export class TranslationSpecInplace {
@test
public directEnumSingleValue () {
expect(translator.translate(setting).inputType()).to.equal('einfache Auswahl');
public directEnumSingleValue() {
expect(translator.translatedAccess(setting).inputType()).to.equal('einfache Auswahl');
}
@test
public directStringLiteralType() {
expect(translator.translate(dish).type()).to.equal('Essen');
expect(translator.translatedAccess(dish).type()).to.equal('Essen');
expect(translatedThingDE.type).to.equal('Essen');
}
@test
public directStringProperty() {
expect(translator.translate(dish).name()).to.equal('de-dish-name');
expect(translator.translatedAccess(dish).name()).to.equal('de-dish-name');
expect(translatedThingDE.name).to.equal('de-dish-name');
}
@test
public directArrayOfString() {
expect(translator.translate(dish).characteristics()).to.deep
expect(translator.translatedAccess(dish).characteristics()).to.deep
.equal([{name: 'de-characteristic0'}, {name: 'de-characteristic1'}]);
expect(translatedThingDE.characteristics).to.deep
.equal([{name: 'de-characteristic0'}, {name: 'de-characteristic1'}]);
}
@test
public directArrayOfStringSubscript() {
expect(translator.translate(dish).characteristics[1]()).to.deep.equal({name: 'de-characteristic1'});
expect(translator.translatedAccess(dish).characteristics[1]()).to.deep.equal({name: 'de-characteristic1'});
expect(translatedThingDE.characteristics![1]).to.deep.equal({name: 'de-characteristic1'});
}
@test
public directMetaArrayOfString() {
expect(translator.translate(dish).categories()).to.deep.equal(['Hauptgericht', 'Nachtisch']);
expect(translator.translatedAccess(dish).categories()).to.deep.equal(['Hauptgericht', 'Nachtisch']);
expect(translatedThingDE.categories).to.deep.equal(['Hauptgericht', 'Nachtisch']);
}
@test
public directMetaArrayOfStringSubscript() {
expect(translator.translate(dish).categories[1]()).to.equal('Nachtisch');
expect(translator.translatedAccess(dish).categories[1]()).to.equal('Nachtisch');
expect(translatedThingDE.categories[1]).to.equal('Nachtisch');
}
@test
public nestedStringLiteralType() {
expect(translator.translate(dish).offers[0].inPlace.type()).to.equal('Gebäude');
expect(translator.translatedAccess(dish).offers[0].inPlace.type()).to.equal('Gebäude');
expect(translatedThingDE.offers![0].inPlace!.type).to.equal('Gebäude');
}
@test
public nestedStringProperty() {
expect(translator.translate(dish).offers[0].inPlace.name()).to.equal('de-space-name');
expect(translator.translatedAccess(dish).offers[0].inPlace.name()).to.equal('de-space-name');
expect(translatedThingDE.offers![0].inPlace!.name).to.equal('de-space-name');
}
@test
public nestedMetaArrayOfString() {
expect(translator.translate(dish).offers[0].inPlace.categories()).to.deep.equal(['Büro', 'Bildung']);
expect(translator.translatedAccess(dish).offers[0].inPlace.categories()).to.deep.equal(['Büro', 'Bildung']);
expect(translatedThingDE.offers![0].inPlace!.categories).to.deep.equal(['Büro', 'Bildung']);
}
@test
public nestedMetaArrayOfStringSubscript() {
expect(translator.translate(dish).offers[0].inPlace.categories[1]()).to.equal('Bildung');
expect(translator.translatedAccess(dish).offers[0].inPlace.categories[1]()).to.equal('Bildung');
expect(translatedThingDE.offers![0].inPlace!.categories[1]).to.equal('Bildung');
}
@test
public directStringLiteralTypeFallback() {
expect(translatorWithFallback.translate(dish).type()).to.equal('dish');
expect(translatorWithFallback.translatedAccess(dish).type()).to.equal('dish');
expect(translatedThingFallback.type).to.equal('dish');
}
@test
public directStringPropertyFallback() {
expect(translatorWithFallback.translate(dish).name()).to.equal('base-dish-name');
expect(translatorWithFallback.translatedAccess(dish).name()).to.equal('base-dish-name');
expect(translatedThingFallback.name).to.equal('base-dish-name');
}
@test
public directArrayOfStringSubscriptFallback() {
expect(translatorWithFallback.translate(dish).characteristics[1]())
expect(translatorWithFallback.translatedAccess(dish).characteristics[1]())
.to.deep.equal({name: 'base-characteristic1'});
expect(translatedThingFallback.characteristics![1])
.to.deep.equal({name: 'base-characteristic1'});
}
@test
public directMetaArrayOfStringFallback() {
expect(translatorWithFallback.translate(dish).categories()).to.deep.equal(['main dish', 'dessert']);
expect(translatorWithFallback.translatedAccess(dish).categories()).to.deep.equal(['main dish', 'dessert']);
expect(translatedThingFallback.categories).to.deep.equal(['main dish', 'dessert']);
}
@test
public directMetaArrayOfStringSubscriptFallback() {
expect(translatorWithFallback.translate(dish).categories[1]()).to.equal('dessert');
expect(translatorWithFallback.translatedAccess(dish).categories[1]()).to.equal('dessert');
expect(translatedThingFallback.categories[1]).to.equal('dessert');
}
@test
public nestedStringLiteralTypeFallback() {
expect(translatorWithFallback.translate(dish).offers[0].inPlace.type()).to.equal('building');
expect(translatorWithFallback.translatedAccess(dish).offers[0].inPlace.type()).to.equal('building');
expect(translatedThingFallback.offers![0].inPlace!.type).to.equal('building');
}
@test
public nestedStringPropertyFallback() {
expect(translatorWithFallback.translate(dish).offers[0].inPlace.name()).to.equal('base-space-name');
expect(translatorWithFallback.translatedAccess(dish).offers[0].inPlace.name()).to.equal('base-space-name');
expect(translatedThingFallback.offers![0].inPlace!.name).to.equal('base-space-name');
}
@test
public nestedMetaArrayOfStringFallback() {
expect(translatorWithFallback.translate(dish).offers[0].inPlace.categories())
expect(translatorWithFallback.translatedAccess(dish).offers[0].inPlace.categories())
.to.deep.equal(['office', 'education']);
expect(translatedThingFallback.offers![0].inPlace!.categories)
.to.deep.equal(['office', 'education']);
}
@test
public nestedMetaArrayOfStringSubscriptFallback() {
expect(translatorWithFallback.translate(dish).offers[0].inPlace.categories[1]()).to.equal('education');
expect(translatorWithFallback.translatedAccess(dish).offers[0].inPlace.categories[1]()).to.equal('education');
expect(translatedThingFallback.offers![0].inPlace!.categories[1]).to.equal('education');
}
@test
public directStringLiteralTypeUndefined() {
// tslint:disable-next-line:no-eval
const undefinedThing = eval('(x) => undefined;');
expect(translator.translate(undefinedThing())('defaultValue')).to.equal('defaultValue');
expect(translator.translate(dish).name('defaultValue')).to.not.equal('defaultValue');
expect(translator.translatedAccess(undefinedThing())('defaultValue')).to.equal('defaultValue');
expect(translator.translatedAccess(dish).name('defaultValue')).to.not.equal('defaultValue');
}
@test
public nestedMetaArrayOfStringSubscriptUndefined() {
// tslint:disable-next-line: no-eval
const workingTranslation = eval('translator.translate(dish).offers[0].inPlace.categories[1](\'printer\');');
const workingTranslation = eval('translator.translatedAccess(dish).offers[0].inPlace.categories[1](\'printer\');');
// tslint:disable-next-line: no-eval
const defaultValueTranslation = eval('translator.translate(dish).offers[0].inPlace.categories[1234](\'printer\');');
const defaultValueTranslation = eval('translator.translatedAccess(dish).offers[0].inPlace.categories[1234](\'printer\');');
expect(defaultValueTranslation).to.equal('printer');
expect(workingTranslation).to.not.equal('printer');
}
@test
public reaccessWithChangedSourceOmitsLRUCache() {
const translatorDE = new SCThingTranslator('de');
const dishCopy = clone(dish);
const translatedDish = translatorDE.translatedAccess(dish);
const distructivelyTranslatedDish = translatorDE.translate(dish);
(dishCopy.origin as SCThingRemoteOrigin).name = 'tranlator.spec';
expect(translatorDE.translatedAccess(dishCopy)).not.to.deep.equal(translatedDish);
expect(translatorDE.translate(dishCopy)).not.to.equal(distructivelyTranslatedDish);
}
@test
public changingTranslatorLanguageFlushesItsLRUCache() {
const translatorDE = new SCThingTranslator('de');
expect(translatorDE.translate(dish).name()).to.equal('de-dish-name');
expect(translatorDE.translatedAccess(dish).name()).to.equal('de-dish-name');
expect(translatorDE.translate(dish).name).to.equal('de-dish-name');
translatorDE.language = 'en';
expect(translatorDE.translate(dish).name()).to.equal('base-dish-name');
expect(translatorDE.translatedAccess(dish).name()).to.equal('base-dish-name');
expect(translatorDE.translate(dish).name).to.equal('base-dish-name');
}
@test
@@ -250,7 +290,7 @@ export class TranslationSpecInplace {
for (let i = 0; i < 201; i++) {
const anotherDish = Object.assign({}, dish);
anotherDish.uid = String(i);
expect(translatorDE.translate(anotherDish).name()).to.equal('de-dish-name');
expect(translatorDE.translatedAccess(anotherDish).name()).to.equal('de-dish-name');
}
}
}
@@ -262,7 +302,7 @@ export class MetaTranslationSpec {
@test
public consistencyWithMetaClass() {
const dishMetaTranslationsDE = translator.translatedPropertyNames(dish.type);
const dishMetaTranslationsEN = translator.translatedPropertyNames(dish.type, 'en');
const dishMetaTranslationsEN = translatorEN.translatedPropertyNames(dish.type);
expect(dishMetaTranslationsEN).to.not.deep.equal(dishMetaTranslationsDE);
expect(dishMetaTranslationsDE).to.deep.equal(SCDishMeta.getInstance().fieldTranslations.de);
expect(dishMetaTranslationsEN).to.deep.equal(SCDishMeta.getInstance().fieldTranslations.en);
@@ -271,17 +311,17 @@ export class MetaTranslationSpec {
@test
public retrieveTranslatedPropertyValueType() {
const dishTypeDE = translator.translatedPropertyValue(dish.type, 'type');
const dishTypeEN = translator.translatedPropertyValue(dish.type, 'type', undefined, 'en');
const dishTypeEN = translatorEN.translatedPropertyValue(dish.type, 'type', undefined);
const dishTypeBASE = translatorWithFallback.translatedPropertyValue(dish.type, 'type');
expect(dishTypeDE).to.deep.equal(SCDishMeta.getInstance().fieldValueTranslations.de.type);
expect(dishTypeEN).to.deep.equal(SCDishMeta.getInstance().fieldValueTranslations.en.type);
expect(dishTypeBASE).to.be.undefined;
expect(dishTypeBASE).to.deep.equal(SCDishMeta.getInstance().fieldValueTranslations.en.type);
}
@test
public retrieveTranslatedPropertyValueNested() {
const dishTypeDE = translator.translatedPropertyValue<SCDish>(dish.type, 'categories', 'main dish');
const dishTypeEN = translator.translatedPropertyValue<SCDish>(dish.type, 'categories', 'main dish', 'en');
const dishTypeDE = translator.translatedPropertyValue(dish.type, 'categories', 'main dish');
const dishTypeEN = translatorEN.translatedPropertyValue(dish.type, 'categories', 'main dish');
const dishTypeBASE = translatorWithFallback.translatedPropertyValue(dish.type, 'categories', 'main dish');
expect(dishTypeDE).to.deep.equal(SCDishMeta.getInstance<SCDishMeta>().fieldValueTranslations.de.categories['main dish']);
expect(dishTypeEN).to.deep.equal(dish.categories[0]);