From abda5cf0caead37f085431e1e5a9771b79272ec6 Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Mon, 11 Nov 2019 14:46:26 +0100 Subject: [PATCH] fix: translator can now handle enum translations --- src/translator.ts | 19 ++++++++++++------- test/translator.spec.ts | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/translator.ts b/src/translator.ts index b810a0cd..07b9996e 100644 --- a/src/translator.ts +++ b/src/translator.ts @@ -146,8 +146,8 @@ export class SCThingTranslator { * Applies known field value translations of the given SCThings meta class to an instance * Translated values overwrite current values inplace (destructive) * - * @param language The language the thing is translated to - * @param thing The thing that will be translated + * @param instance The thing / object that will be translated + * @param language The language the thing / object is translated to * @returns The thing with translated meta field values */ private replaceAvailableMetaFieldValueTranslations(instance: any, @@ -159,13 +159,18 @@ export class SCThingTranslator { if (typeof metaClass.fieldValueTranslations[language] !== 'undefined') { Object.keys(metaClass.fieldValueTranslations[language]) .forEach((key) => { - if (metaClass.fieldValueTranslations[language][key] instanceof Object) { + if (metaClass.fieldValueTranslations[language][key] instanceof Object + && (instance as any)[key] instanceof Object) { // Assigns known translations of subproperties to property in given language (e.g. categories) Object.keys((instance as any)[key]) .forEach((subKey) => { (instance as any)[key][subKey] = metaClass.fieldValueTranslations[language][key][(instance as any)[key][subKey]]; }); + } else if (metaClass.fieldValueTranslations[language][key] instanceof Object + && typeof (instance as any)[key] === 'string') { + // Assigns known translations of enum to property in given language (e.g. SCSettingInputType) + (instance as any)[key] = metaClass.fieldValueTranslations[language][key][(instance as any)[key]]; } else { // Assigns property to known translation of fieldValueTranslations in given language (instance as any)[key] = metaClass.fieldValueTranslations[language][key]; @@ -213,8 +218,8 @@ export class SCThingTranslator { * All the values will be set to the known translations of the property/key name * @example * const translatedMetaDish = translator.translatedPropertyNames(SCThingType.CourseOfStudies); - * @param language The language the object is translated to - * @param thingType Type of the thing + * @param thing The thing 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(thing: T, @@ -228,8 +233,8 @@ export class SCThingTranslator { * Recursively translates the given object in-place * Translated values overwrite current values (destructive) * - * @param language The language the thing is translated to - * @param thing The thing that will be translated + * @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, diff --git a/test/translator.spec.ts b/test/translator.spec.ts index f42935f5..4c6d34b0 100644 --- a/test/translator.spec.ts +++ b/test/translator.spec.ts @@ -18,6 +18,7 @@ import {slow, suite, test, timeout} from 'mocha-typescript'; import {SCThingOriginType, SCThingType} 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'; import {SCThingTranslator} from '../src/translator'; const building: SCBuildingWithoutReferences = { @@ -86,6 +87,23 @@ const dish: SCDish = { uid: '540862f3-ea30-5b8f-8678-56b4dc217140', }; +const setting: SCSetting = { + categories: ['profile'], + defaultValue: 'student', + description: 'base-description', + inputType: SCSettingInputType.SingleChoice, + name: 'group', + order: 1, + origin: { + indexed: '2018-11-11T14:30:00Z', + name: 'Dummy', + type: SCThingOriginType.Remote, + }, + type: SCThingType.Setting, + uid: '2c97aa36-4aa2-43de-bc5d-a2b2cb3a530e', + values: ['student', 'employee', true, 42], +}; + const translator = new SCThingTranslator('de'); // tslint:disable-next-line:no-eval const languageNonExistant = eval("'jp'"); @@ -95,6 +113,10 @@ const translatorWithFallback = new SCThingTranslator(languageNonExistant); // tslint:disable:member-ordering TranslationSpecInplace @suite(timeout(10000), slow(5000)) export class TranslationSpecInplace { + @test + public directEnumSingleValue () { + expect(translator.translate(setting).inputType()).to.equal('einfache Auswahl'); + } @test public directStringLiteralType() {