UNPKG

ilingo

Version:

This is a lightweight library for translation.

1 lines 13.6 kB
{"version":3,"file":"index.mjs","sources":["../src/constants.ts","../src/utils/identify.ts","../src/utils/language/module.ts","../src/utils/template.ts","../src/module.ts","../src/store/memory.ts"],"sourcesContent":["/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport const LOCALE_DEFAULT = 'en';\nexport const STORE_DEFAULT = Symbol.for('default');\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from 'smob';\nimport type { LinesRecord } from '../types';\n\nexport function isLineRecord(value: unknown) : value is LinesRecord {\n if (!isObject(value)) {\n return false;\n }\n\n const ob = value as Record<string, any>;\n const keys = Object.keys(ob);\n for (let i = 0; i < keys.length; i++) {\n /* istanbul ignore next */\n if (\n typeof ob[keys[i]] !== 'string' &&\n !isLineRecord(ob[keys[i]])\n ) {\n return false;\n }\n }\n\n return true;\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { hasOwnProperty } from 'smob';\nimport codes from './data.json';\n\nexport function isISO639LanguageCode(input: string) {\n return hasOwnProperty(codes, input);\n}\n\nexport function isBCP47LanguageCode(input: string) {\n const hyphenIndex = input.indexOf('-');\n if (hyphenIndex !== -1) {\n input = input.substring(0, hyphenIndex);\n }\n\n return isISO639LanguageCode(input);\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function template(\n str: string,\n data: Record<string, any>,\n regex = /\\{\\{(.+?)\\}\\}/g,\n) : string {\n return Array.from(str.matchAll(regex))\n .reduce((\n acc,\n match,\n ) => {\n if (typeof data[match[1]] !== 'undefined') {\n return acc.replace(match[0], data[match[1]]);\n }\n\n return acc;\n }, str);\n}\n","/*\n * Copyright (c) 2022.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { ConfigInput } from './config';\nimport { LOCALE_DEFAULT } from './constants';\nimport type { IStore } from './store';\nimport type {\n GetContext,\n} from './types';\nimport {\n template,\n} from './utils';\n\nexport class Ilingo {\n public readonly stores : Set<IStore>;\n\n protected locale: string;\n\n // ----------------------------------------------------\n\n constructor(input: ConfigInput = {}) {\n this.locale = input.locale || LOCALE_DEFAULT;\n\n this.stores = new Set<IStore>();\n if (input.store) {\n this.stores.add(input.store);\n }\n }\n\n // ----------------------------------------------------\n\n merge(instance: Ilingo) {\n const ownEntries = Array.from(this.stores.values());\n const foreignEntries = Array.from(instance.stores.values());\n\n let foreignEntriesIndex = -1;\n for (let i = 0; i < foreignEntries.length; i++) {\n foreignEntriesIndex = -1;\n for (let j = 0; j < ownEntries.length; j++) {\n if (ownEntries[j] === foreignEntries[i]) {\n foreignEntriesIndex = j;\n break;\n }\n }\n\n if (foreignEntriesIndex === -1) {\n this.stores.add(foreignEntries[i]);\n }\n }\n }\n\n // ----------------------------------------------------\n\n setLocale(key: string) {\n this.locale = key;\n }\n\n resetLocale() {\n this.locale = LOCALE_DEFAULT;\n }\n\n getLocale() : string {\n return this.locale;\n }\n\n // ----------------------------------------------------\n\n async getLocales() : Promise<string[]> {\n const locales : string[] = [];\n const entries = this.stores.values();\n // eslint-disable-next-line no-constant-condition\n while (true) {\n const store = entries.next();\n if (store.done) {\n break;\n }\n\n locales.push(...await store.value.getLocales());\n }\n return Array.from(new Set(locales));\n }\n\n // ----------------------------------------------------\n\n async get(ctx: GetContext) : Promise<string | undefined> {\n let message : string | undefined;\n const entries = this.stores.values();\n // eslint-disable-next-line no-constant-condition\n while (true) {\n const store = entries.next();\n if (store.done) {\n break;\n }\n\n message = await store.value.get({\n locale: ctx.locale || this.getLocale(),\n group: ctx.group,\n key: ctx.key,\n });\n\n if (message) {\n break;\n }\n }\n\n if (!message) {\n return undefined;\n }\n\n return this.format(message, ctx.data || {});\n }\n\n // ----------------------------------------------------\n\n format(input: string, data: Record<string, any>) {\n return template(input, data || {});\n }\n}\n","/*\n * Copyright (c) 2023.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { getPathValue, setPathValue } from 'pathtrace';\nimport type { LocalesRecord } from '../types';\nimport type {\n IStore,\n MemoryStoreOptions,\n StoreGetContext,\n StoreSetContext,\n} from './types';\n\nexport class MemoryStore implements IStore {\n protected data : LocalesRecord;\n\n constructor(options: MemoryStoreOptions) {\n this.data = options.data;\n }\n\n async get(context: StoreGetContext): Promise<string | undefined> {\n if (\n !this.data[context.locale] ||\n !this.data[context.locale][context.group]\n ) {\n return undefined;\n }\n\n const output = getPathValue(\n this.data[context.locale][context.group],\n context.key,\n );\n\n if (typeof output === 'string') {\n return output;\n }\n\n return undefined;\n }\n\n async set(context: StoreSetContext): Promise<void> {\n this.initLines(context.group, context.locale);\n\n setPathValue(\n this.data[context.locale][context.group],\n context.key,\n context.value,\n );\n }\n\n protected initLines(group: string, locale: string) {\n if (typeof this.data[locale] === 'undefined') {\n this.data[locale] = {};\n }\n\n if (typeof this.data[locale][group] === 'undefined') {\n this.data[locale][group] = {};\n }\n }\n\n async getLocales(): Promise<string[]> {\n return Object.keys(this.data);\n }\n}\n"],"names":["LOCALE_DEFAULT","isLineRecord","value","isObject","ob","keys","Object","i","length","isISO639LanguageCode","input","hasOwnProperty","codes","isBCP47LanguageCode","hyphenIndex","indexOf","substring","template","str","data","regex","Array","from","matchAll","reduce","acc","match","replace","Ilingo","stores","locale","Set","store","add","merge","instance","ownEntries","values","foreignEntries","foreignEntriesIndex","j","setLocale","key","resetLocale","getLocale","getLocales","locales","entries","next","done","push","get","ctx","message","group","undefined","format","MemoryStore","options","context","output","getPathValue","set","initLines","setPathValue"],"mappings":";;;AAAA;;;;;IAOO,M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