@c8y/ngx-components
Version:
Angular modules for Cumulocity IoT applications
1 lines • 13.4 kB
Source Map (JSON)
{"version":3,"file":"c8y-ngx-components-translation-editor-data.mjs","sources":["../../translation-editor/data/translation-store.service.ts","../../translation-editor/data/c8y-ngx-components-translation-editor-data.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport {\n ApplicationAvailability,\n ApplicationService,\n ApplicationType,\n IApplication\n} from '@c8y/client';\nimport { AppStateService, ZipService } from '@c8y/ngx-components';\nimport { uniq } from 'lodash-es';\n\nexport interface TranslationLocale {\n label: string;\n locale: string;\n}\n\n@Injectable({\n providedIn: 'root'\n})\nexport class TranslationStoreService {\n protected readonly translationAppName = 'User defined translations';\n protected readonly translationAppContextPath = 'user-defined-translations';\n\n constructor(\n private appService: ApplicationService,\n private appState: AppStateService,\n private zip: ZipService\n ) {}\n\n /**\n * Retrieves a list of available translations for the given locales from all the hosted apps available on the tenant.\n */\n async getAvailableTranslations(locales: string[]) {\n const { data: hostedApps } = await this.appService.list({\n tenant: this.appState.currentTenant.value.name,\n type: 'HOSTED',\n pageSize: 2000\n });\n\n const uniqueContextPaths = uniq(\n hostedApps\n .filter(app => app.contextPath && app.manifest?.webSdkVersion)\n .map(app => app.contextPath)\n );\n\n const translations: { [key: string]: { [locale: string]: string } } = {};\n for (const contextPath of uniqueContextPaths) {\n for (const locale of locales) {\n try {\n const translationsForLanguageAndApp = await this.getFileFromPath(\n `/apps/${contextPath}/${locale}.json`\n );\n for (const key of Object.keys(translationsForLanguageAndApp)) {\n if (!translations[key]) {\n translations[key] = {};\n }\n translations[key][locale] = translationsForLanguageAndApp[key];\n }\n } catch (e) {\n continue;\n }\n }\n }\n\n return translations;\n }\n\n /**\n * Retrieves the translations for the given locale from the translation app.\n */\n async getTranslationsForLocale<T extends string>(locale: T): Promise<{ [key: string]: string }> {\n try {\n const translations = await this.getFileFromTranslationApp(`${locale}.json`);\n if (translations[locale]) {\n return translations[locale];\n }\n } catch (e) {\n // do nothing.\n }\n return {};\n }\n\n /**\n * Retrieves the translations for the given locales from the translation app.\n */\n async loadTranslationsForLocales(locales: string[]): Promise<{\n [key: string]: {\n [key: string]: string;\n };\n }> {\n const translations: {\n [key: string]: { [key: string]: string };\n } = {};\n\n const loadAndAddLocale = async (locale: string) => {\n translations[locale] = await this.getTranslationsForLocale(locale);\n };\n\n await Promise.all(locales.map(loadAndAddLocale));\n\n return translations;\n }\n\n /**\n * Retrieves a combined list of translations for all the provided locales.\n * The locales are combined into a single object per key and an array of these objects is returned.\n */\n async getCombinedListOfTranslationsForPerKey(\n locales: string[]\n ): Promise<{ key: string; [locale: string]: string }[]> {\n const translations = await this.loadTranslationsForLocales(locales);\n const translationMap: { [key: string]: { [locale: string]: string } } = {};\n\n for (const locale of Object.keys(translations)) {\n for (const key of Object.keys(translations[locale])) {\n let translationsForKey = translationMap[key];\n if (!translationsForKey) {\n translationsForKey = {};\n translationMap[key] = translationsForKey;\n }\n\n translationsForKey[locale] = translations[locale][key];\n }\n }\n\n const keys = Object.keys(translationMap).sort((a, b) => a.localeCompare(b));\n\n return keys.map(key => Object.assign({ key }, translationMap[key]));\n }\n\n /**\n * Updates the files of the translation application with the provided translations.\n */\n async updateTranslations(\n translations: { key: string; [locale: string]: string }[]\n ): Promise<void> {\n const filesToUpload = new Array<{ path: string; contents: File }>();\n const translationsPerLocale: { [locale: string]: { [key: string]: string } } = {};\n for (const entry of translations) {\n const key = entry.key;\n for (const locale of Object.keys(entry)) {\n if (locale === 'key') {\n continue;\n }\n if (!translationsPerLocale[locale]) {\n translationsPerLocale[locale] = {};\n }\n\n if (entry[locale]) {\n translationsPerLocale[locale][key] = entry[locale];\n }\n }\n }\n\n const langs = this.appState.state.langs as string[];\n\n for (const locale of langs) {\n const fileName = `${locale}.json`;\n filesToUpload.push({\n path: fileName,\n contents: new File(\n [JSON.stringify({ [locale]: translationsPerLocale[locale] || {} })],\n fileName\n )\n });\n }\n const app = await this.getOrCreateTranslationApp();\n await this.appService.binary(app).updateFiles(filesToUpload);\n }\n\n /**\n * @returns The translation app for the current tenant. If it does not exist, it will be created.\n */\n async getOrCreateTranslationApp(): Promise<IApplication> {\n const { data: apps } = await this.appService.listByName(this.translationAppName);\n const ownApp = apps.find(\n app =>\n app.contextPath === this.translationAppContextPath &&\n app.owner.tenant.id === this.appState.currentTenant.value.name\n );\n if (ownApp) {\n return ownApp;\n }\n\n const { data: app } = await this.appService.create({\n name: this.translationAppName,\n contextPath: this.translationAppContextPath,\n key: `${this.translationAppContextPath}-app-key`,\n type: ApplicationType.HOSTED,\n availability: ApplicationAvailability.MARKET,\n config: {\n icon: {\n class: 'language1'\n }\n },\n noAppSwitcher: true,\n description: 'Providing user defined translations'\n });\n const zip = await this.zip.createZip([]);\n\n const { data: binary } = await this.appService.binary(app).upload(zip, 'translations.zip');\n const { data: updatedApp } = await this.appService.update({\n id: app.id,\n activeVersionId: binary.id as string\n });\n return updatedApp;\n }\n\n private async getFileFromTranslationApp(file: string) {\n return this.getFileFromPath(`/apps/public/${this.translationAppContextPath}/${file}`);\n }\n\n private async getFileFromPath(path: string) {\n const finalPath = `${path}?nocache=${Date.now()}`;\n const response = await fetch(finalPath);\n\n if (response.status !== 200) {\n throw new Error(`Failed to fetch file from ${path}`);\n }\n\n return response.json();\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;MAkBa,uBAAuB,CAAA;AAIlC,IAAA,WAAA,CACU,UAA8B,EAC9B,QAAyB,EACzB,GAAe,EAAA;QAFf,IAAU,CAAA,UAAA,GAAV,UAAU,CAAoB;QAC9B,IAAQ,CAAA,QAAA,GAAR,QAAQ,CAAiB;QACzB,IAAG,CAAA,GAAA,GAAH,GAAG,CAAY;QANN,IAAkB,CAAA,kBAAA,GAAG,2BAA2B,CAAC;QACjD,IAAyB,CAAA,yBAAA,GAAG,2BAA2B,CAAC;KAMvE;AAEJ;;AAEG;IACH,MAAM,wBAAwB,CAAC,OAAiB,EAAA;AAC9C,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