/* * Copyright (C) 2018 StApps * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ import {SCThingsField} from './Classes'; import {SCOrganization} from './things/Organization'; import {SCPerson} from './things/Person'; import {isThingWithTranslations} from './types/Guards'; import {SCTranslations} from './types/i18n'; import {SCISO8601Date} from './types/Time'; import {SCUuid} from './types/UUID'; /** * Types a thing can be */ export enum SCThingType { AcademicEvent = 'academic event', Article = 'article', Book = 'book', Building = 'building', Catalog = 'catalog', CourseOfStudies = 'course of studies', DateSeries = 'date series', Diff = 'diff', Dish = 'dish', Favorite = 'favorite', Floor = 'floor', Message = 'message', Offer = 'offer', // Currently not in use Organization = 'organization', Person = 'person', PointOfInterest = 'point of interest', Room = 'room', Semester = 'semester', Setting = 'setting', SportCourse = 'sport course', Ticket = 'ticket', Tour = 'tour', Video = 'video', } /** * A thing */ export interface SCThing { /** * Alternate names of the thing */ alternateNames?: string[]; /** * Description of the thing * * @minLength 1 */ description?: string; /** * Image of the thing */ image?: string; /** * Name of the thing * * @minLength 1 */ name: string; /** * Origin of the thing */ origin: SCThingOrigin; /** * Translations of specific values of the object * * Take precedence over "main" value for selected languages. */ translations?: SCTranslations; /** * Type of the thing */ type: string; /** * Universally unique identifier of the thing */ uid: SCUuid; /** * URL of the thing */ url?: string; } /** * Origin of a thing */ export interface SCThingOrigin { /** * When the thing was indexed last from the origin */ indexed: SCISO8601Date; /** * Maintainer of the origin * * e.g. restaurant of a dish */ maintainer?: SCPerson | SCOrganization; /** * When the thing was modified last in the origin */ modified?: SCISO8601Date; /** * Name of the origin */ name: string; /** * Original ID of the thing in the origin */ originalId?: string; /** * Entity that is responsible for the entity * * e.g. an organizer for an event */ responsibleEntity?: SCPerson | SCOrganization; /** * Main URL of the origin */ url?: string; } /** * Meta information about things */ export class SCThingMeta { /** * Translations of fields */ static fieldTranslations: any = { de: { alternateNames: 'alternative Namen', description: 'Beschreibung', image: 'Bild', name: 'Name', translations: 'Übersetzungen', type: 'Typ', uid: 'Identifikation', url: 'URL', }, en: { alternateNames: 'alternate names', description: 'description', uid: 'identification', }, }; /** * Translations of values of fields */ static fieldValueTranslations: any = { de: { type: { AcademicTerm: 'Studienabschnitt', Article: 'Artikel', Book: 'Buch', Catalog: 'Katalog', Date: 'Termin', Diff: 'Unterschied', Dish: 'Essen', Event: 'Veranstaltung', Favorite: 'Favorit', FloorPlan: 'Etagenplan', Message: 'Nachricht', Offer: 'Angebot', Organization: 'Organisation', Person: 'Person', Place: 'Ort', Setting: 'Einstellung', Thing: 'Ding', Ticket: 'Ticket', Tour: 'Tour', Video: 'Video', }, }, }; /** * Get field translation * * @param {keyof SCTranslations} language Language to get field translation for * @param {keyof T} field Field to get translation for * @returns {string} Translated field or field itself */ static getFieldTranslation(language: keyof SCTranslations, field: SCThingsField): string { if (typeof this.fieldTranslations[language] !== 'undefined' && typeof this.fieldTranslations[language][field] !== 'undefined') { return this.fieldTranslations[language][field]; } return field as string; } /** * Get field value translation * * @param {keyof SCTranslations} language Language to get value translation for * @param {string} field Field to get value translation for * @param {T} thing SCThing to get value translation for * @returns {string} Translated value or value itself */ static getFieldValueTranslation(language: keyof SCTranslations, field: SCThingsField, thing: T): string { let translations: SCTranslations; if (isThingWithTranslations(thing)) { translations = thing.translations; const languageTranslations: SCThingTranslatableProperties | undefined = translations[language]; if (typeof languageTranslations !== 'undefined') { if (typeof (languageTranslations as any)[field] !== 'undefined') { return (languageTranslations as any)[field]; } } } // get translation from meta object if (typeof this.fieldValueTranslations[language] !== 'undefined' && typeof this.fieldValueTranslations[language][field] !== 'undefined' && typeof (thing as any)[field] !== 'undefined' && typeof this.fieldValueTranslations[language][field][(thing as any)[field]]) { return this.fieldValueTranslations[language][field][(thing as any)[field]]; } // fallback to value itself return (thing as any)[field]; } } /** * Translatable properties of things */ export interface SCThingTranslatableProperties { /** * Translation of the description of the thing */ description?: string; /** * Translation of the name of the thing */ name?: string; /** * Origin of the thing */ origin?: { /** * Translation of the name of the origin of the thing */ name: string; }; }