ilingo
Version:
This is a lightweight library for translation.
1 lines • 13.4 kB
Source Map (JSON)
{"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 { Store } from './store';\nimport type {\n GetContext,\n} from './types';\nimport {\n template,\n} from './utils';\n\nexport class Ilingo {\n public readonly stores : Set<Store>;\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<Store>();\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 MemoryStoreOptions,\n Store,\n StoreGetContext,\n StoreSetContext,\n} from './types';\n\nexport class MemoryStore implements Store {\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","merge","instance","ownEntries","stores","values","foreignEntries","foreignEntriesIndex","j","add","setLocale","key","locale","resetLocale","getLocale","getLocales","locales","entries","store","next","done","push","Set","get","ctx","message","group","undefined","format","constructor","MemoryStore","context","output","getPathValue","set","initLines","setPathValue","options"],"mappings":";;;AAAA;;;;;IA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