mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-09 19:22:51 +00:00
fix: translator can now handle enum translations
This commit is contained in:
@@ -146,8 +146,8 @@ export class SCThingTranslator {
|
|||||||
* Applies known field value translations of the given SCThings meta class to an instance
|
* Applies known field value translations of the given SCThings meta class to an instance
|
||||||
* Translated values overwrite current values inplace (destructive)
|
* Translated values overwrite current values inplace (destructive)
|
||||||
*
|
*
|
||||||
* @param language The language the thing is translated to
|
* @param instance The thing / object that will be translated
|
||||||
* @param thing The thing that will be translated
|
* @param language The language the thing / object is translated to
|
||||||
* @returns The thing with translated meta field values
|
* @returns The thing with translated meta field values
|
||||||
*/
|
*/
|
||||||
private replaceAvailableMetaFieldValueTranslations(instance: any,
|
private replaceAvailableMetaFieldValueTranslations(instance: any,
|
||||||
@@ -159,13 +159,18 @@ export class SCThingTranslator {
|
|||||||
if (typeof metaClass.fieldValueTranslations[language] !== 'undefined') {
|
if (typeof metaClass.fieldValueTranslations[language] !== 'undefined') {
|
||||||
Object.keys(metaClass.fieldValueTranslations[language])
|
Object.keys(metaClass.fieldValueTranslations[language])
|
||||||
.forEach((key) => {
|
.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)
|
// Assigns known translations of subproperties to property in given language (e.g. categories)
|
||||||
Object.keys((instance as any)[key])
|
Object.keys((instance as any)[key])
|
||||||
.forEach((subKey) => {
|
.forEach((subKey) => {
|
||||||
(instance as any)[key][subKey] =
|
(instance as any)[key][subKey] =
|
||||||
metaClass.fieldValueTranslations[language][key][(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 {
|
} else {
|
||||||
// Assigns property to known translation of fieldValueTranslations in given language
|
// Assigns property to known translation of fieldValueTranslations in given language
|
||||||
(instance as any)[key] = metaClass.fieldValueTranslations[language][key];
|
(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
|
* All the values will be set to the known translations of the property/key name
|
||||||
* @example
|
* @example
|
||||||
* const translatedMetaDish = translator.translatedPropertyNames<SCCourseOfStudies>(SCThingType.CourseOfStudies);
|
* const translatedMetaDish = translator.translatedPropertyNames<SCCourseOfStudies>(SCThingType.CourseOfStudies);
|
||||||
* @param language The language the object is translated to
|
* @param thing The thing whose property names will be translated
|
||||||
* @param thingType Type of the thing
|
* @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
|
* @returns An object with the properties of the SCThingType where the values are the known property tranlations
|
||||||
*/
|
*/
|
||||||
public translatedPropertyNames<T extends SCThing>(thing: T,
|
public translatedPropertyNames<T extends SCThing>(thing: T,
|
||||||
@@ -228,8 +233,8 @@ export class SCThingTranslator {
|
|||||||
* Recursively translates the given object in-place
|
* Recursively translates the given object in-place
|
||||||
* Translated values overwrite current values (destructive)
|
* Translated values overwrite current values (destructive)
|
||||||
*
|
*
|
||||||
* @param language The language the thing is translated to
|
* @param instance The thing / object that will be translated
|
||||||
* @param thing The thing that will be translated
|
* @param language The language the thing / object is translated to
|
||||||
* @returns The thing translated
|
* @returns The thing translated
|
||||||
*/
|
*/
|
||||||
public translateWholeThingDestructively(instance: any,
|
public translateWholeThingDestructively(instance: any,
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {slow, suite, test, timeout} from 'mocha-typescript';
|
|||||||
import {SCThingOriginType, SCThingType} from '../src/things/abstract/thing';
|
import {SCThingOriginType, SCThingType} from '../src/things/abstract/thing';
|
||||||
import {SCBuildingWithoutReferences} from '../src/things/building';
|
import {SCBuildingWithoutReferences} from '../src/things/building';
|
||||||
import {SCDish, SCDishMeta} from '../src/things/dish';
|
import {SCDish, SCDishMeta} from '../src/things/dish';
|
||||||
|
import {SCSetting, SCSettingInputType} from '../src/things/setting';
|
||||||
import {SCThingTranslator} from '../src/translator';
|
import {SCThingTranslator} from '../src/translator';
|
||||||
|
|
||||||
const building: SCBuildingWithoutReferences = {
|
const building: SCBuildingWithoutReferences = {
|
||||||
@@ -86,6 +87,23 @@ const dish: SCDish = {
|
|||||||
uid: '540862f3-ea30-5b8f-8678-56b4dc217140',
|
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');
|
const translator = new SCThingTranslator('de');
|
||||||
// tslint:disable-next-line:no-eval
|
// tslint:disable-next-line:no-eval
|
||||||
const languageNonExistant = eval("'jp'");
|
const languageNonExistant = eval("'jp'");
|
||||||
@@ -95,6 +113,10 @@ const translatorWithFallback = new SCThingTranslator(languageNonExistant);
|
|||||||
// tslint:disable:member-ordering TranslationSpecInplace
|
// tslint:disable:member-ordering TranslationSpecInplace
|
||||||
@suite(timeout(10000), slow(5000))
|
@suite(timeout(10000), slow(5000))
|
||||||
export class TranslationSpecInplace {
|
export class TranslationSpecInplace {
|
||||||
|
@test
|
||||||
|
public directEnumSingleValue () {
|
||||||
|
expect(translator.translate(setting).inputType()).to.equal('einfache Auswahl');
|
||||||
|
}
|
||||||
|
|
||||||
@test
|
@test
|
||||||
public directStringLiteralType() {
|
public directStringLiteralType() {
|
||||||
|
|||||||
Reference in New Issue
Block a user