Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 1x 1x 1x 1x 1x 2x 1x 5x 1x 2x 1x 1x 17x 15x 2x 2x 8x 8x 8x 8x 4x 4x 4x 4x 4x 8x 4x 4x 4x 4x | import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic'; import type { ILogger, ILogger_ } from 'src/interfaces/ILogger'; import { Translations } from 'src/translations/Translations'; import type { IPrjSettings } from 'src/types/PrjSettings'; import ILanguageTranslations from './interfaces/ILanguageTranslations'; import ITranslationService, { ITranslationService_, } from './interfaces/ITranslationService'; import { Inject } from '../DependencyInjection/decorators/Inject'; import { RegisterInstance } from '../DependencyInjection/decorators/RegisterInstance'; /** * A service that provides translations for the application. */ @ImplementsStatic<ITranslationService_>() @RegisterInstance('ITranslationService', (x) => new x(Translations)) export class TranslationService implements ITranslationService { static instance: TranslationService | undefined; @Inject('IPrjSettings', (x: IPrjSettings) => x.language) private readonly _language!: IPrjSettings['language']; @Inject( 'ILogger_', (x: ILogger_) => x.getLogger('TranslationService'), false, ) private readonly _logger?: ILogger; private readonly _translations: ILanguageTranslations[]; /** * Gets the singleton instance of the translation service. * @returns The instance of the TranslationService. */ static getInstance(): TranslationService { if (!TranslationService.instance) { throw new Error('TranslationService not initialized'); } return TranslationService.instance; } /** * Gets the singleton instance of the translation service.. * @param translations The translations to use. */ constructor(translations: ILanguageTranslations[]) { if (TranslationService.instance) { return TranslationService.instance; } TranslationService.instance = this; this._translations = translations; } /** * Returns a translation for the given key. * @param key The key to translate. * @returns The translation. * @remarks - If the translation is not found, the key is returned. * - Look at the `translations.json` file to see all available translations. */ public get(key: string): string { const language = this._translations.find( (v) => v.lang === this._language, ); if (language) { if (language.translations.hasOwnProperty(key)) { return language.translations[key] as string; } } this._logger?.warn(`Translation for key ${key} not found`); return key; } /** * Returns all translations for the given key. * @param key The key to translate. * @returns The translations. * @remarks - If the translation is not found, the key is returned. * - Look at the `translations.json` file to see all available translations. */ public getAll(key: string): string[] { const translationStrings: string[] = []; for (const language of this._translations) { if (language.translations.hasOwnProperty(key)) { translationStrings.push(language.translations[key]); } else { this._logger?.warn( `Translation for key ${key} not found in language ${language.lang}`, ); translationStrings.push(key); } } return translationStrings; } } |