feat: extend property value translation retrival

This commit is contained in:
Rainer Killinger
2020-09-28 15:22:29 +02:00
parent 8641bfc877
commit a246bdea84
2 changed files with 47 additions and 5 deletions

View File

@@ -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<T extends unknown>(type: SCThingType,
field: string,
key?: string,
language?: keyof SCTranslations<T>): 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
*