From a246bdea84e0ca390be6ab38723d637626db87d2 Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Mon, 28 Sep 2020 15:22:29 +0200 Subject: [PATCH] feat: extend property value translation retrival --- src/translator.ts | 32 ++++++++++++++++++++++++++++++++ test/translator.spec.ts | 20 +++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/translator.ts b/src/translator.ts index 014aa64e..6c394040 100644 --- a/src/translator.ts +++ b/src/translator.ts @@ -229,6 +229,38 @@ export class SCThingTranslator { return this.getAllMetaFieldTranslations(type, targetLanguage) 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'); + * @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(type: SCThingType, + field: string, + key?: string, + language?: keyof SCTranslations): string | undefined { + const targetLanguage = (typeof language !== 'undefined') ? language : this.language; + const metaClass = this.getMetaClassInstance(type); + + if (typeof metaClass.fieldValueTranslations[targetLanguage] !== 'undefined' && + typeof metaClass.fieldValueTranslations[targetLanguage][field] !== 'undefined') { + if (typeof key === 'string' && + typeof metaClass.fieldValueTranslations[targetLanguage][field][key] !== 'undefined') { + + return metaClass.fieldValueTranslations[targetLanguage][field][key] as string ; + } + + return metaClass.fieldValueTranslations[targetLanguage][field] as string ; + } + + return key; + } + /** * Given a SCThingType this function will translate it * diff --git a/test/translator.spec.ts b/test/translator.spec.ts index cb862cc1..4ae72e0d 100644 --- a/test/translator.spec.ts +++ b/test/translator.spec.ts @@ -269,13 +269,23 @@ export class MetaTranslationSpec { } @test - public retrieveTranslatedThingType() { - const dishTypeDE = translator.translatedThingType(dish.type); - const dishTypeEN = translator.translatedThingType(dish.type, 'en'); - const dishTypeBASE = translatorWithFallback.translatedThingType(dish.type); + public retrieveTranslatedPropertyValueType() { + const dishTypeDE = translator.translatedPropertyValue(dish.type, 'type'); + const dishTypeEN = translator.translatedPropertyValue(dish.type, 'type', undefined, 'en'); + 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.deep.equal(SCDishMeta.getInstance().fieldValueTranslations.en.type); + expect(dishTypeBASE).to.be.undefined; + } + + @test + public retrieveTranslatedPropertyValueNested() { + const dishTypeDE = translator.translatedPropertyValue(dish.type, 'categories', 'main dish'); + const dishTypeEN = translator.translatedPropertyValue(dish.type, 'categories', 'main dish', 'en'); + const dishTypeBASE = translatorWithFallback.translatedPropertyValue(dish.type, 'categories', 'main dish'); + expect(dishTypeDE).to.deep.equal(SCDishMeta.getInstance().fieldValueTranslations.de.categories['main dish']); + expect(dishTypeEN).to.deep.equal(dish.categories[0]); + expect(dishTypeBASE).to.deep.equal(dish.categories[0]); } @test