mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
feat: extend property value translation retrival
This commit is contained in:
@@ -229,6 +229,38 @@ export class SCThingTranslator {
|
|||||||
return this.getAllMetaFieldTranslations(type, targetLanguage) as T;
|
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
|
* Given a SCThingType this function will translate it
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -269,13 +269,23 @@ export class MetaTranslationSpec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
public retrieveTranslatedThingType() {
|
public retrieveTranslatedPropertyValueType() {
|
||||||
const dishTypeDE = translator.translatedThingType(dish.type);
|
const dishTypeDE = translator.translatedPropertyValue(dish.type, 'type');
|
||||||
const dishTypeEN = translator.translatedThingType(dish.type, 'en');
|
const dishTypeEN = translator.translatedPropertyValue(dish.type, 'type', undefined, 'en');
|
||||||
const dishTypeBASE = translatorWithFallback.translatedThingType(dish.type);
|
const dishTypeBASE = translatorWithFallback.translatedPropertyValue(dish.type, 'type');
|
||||||
expect(dishTypeDE).to.deep.equal(SCDishMeta.getInstance().fieldValueTranslations.de.type);
|
expect(dishTypeDE).to.deep.equal(SCDishMeta.getInstance().fieldValueTranslations.de.type);
|
||||||
expect(dishTypeEN).to.deep.equal(SCDishMeta.getInstance().fieldValueTranslations.en.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<SCDish>(dish.type, 'categories', 'main dish');
|
||||||
|
const dishTypeEN = translator.translatedPropertyValue<SCDish>(dish.type, 'categories', 'main dish', 'en');
|
||||||
|
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]);
|
||||||
|
expect(dishTypeBASE).to.deep.equal(dish.categories[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
|
|||||||
Reference in New Issue
Block a user