mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
feat: add SCThingTranslator class. move functionality accordingly
This commit is contained in:
committed by
Karl-Philipp Wulfert
parent
797e5ca9de
commit
90e3d22399
@@ -12,10 +12,8 @@
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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';
|
||||
@@ -219,85 +217,9 @@ export class SCThingMeta {
|
||||
*/
|
||||
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',
|
||||
},
|
||||
type: 'Ding',
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Get field translation
|
||||
*
|
||||
* @param {keyof SCTranslations<T extends SCThing>} 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<T extends SCThing>(language: keyof SCTranslations<T>,
|
||||
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<T>} 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<T extends SCThing>(language: keyof SCTranslations<T>,
|
||||
field: SCThingsField,
|
||||
thing: T): string {
|
||||
|
||||
let translations: SCTranslations<SCThingTranslatableProperties>;
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user