diff --git a/README.md b/README.md index fb3f153..3717278 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ error(key: string, error: string, spec: object): string - [SvelteKit](https://github.com/eddow/omni18n-svelte4) - Svelte4 - [Nuxt](https://github.com/eddow/omni18n-vue) - Vue3 -- [Translator](https://github.com/eddow/omni18n-edit/releases) - To edit `FileDB` dictionaries and give an idea of "translation page" +- [Editor](https://github.com/eddow/omni18n-edit/releases) - To edit `FileDB` dictionaries and give an idea of "translation page" ## TODO diff --git a/docs/umd.md b/docs/umd.md index e685297..aae34d2 100644 --- a/docs/umd.md +++ b/docs/umd.md @@ -4,7 +4,7 @@ Static websites will have to expose static translations files, import a library For now, the file `lib/omni18n.js` is the file to be copied and referred to in the html script tag. -Note: until now (1.1.8), the compressed library is 40k - it might be an overkill as the library is designed for fullstack optimization. The big thing that can still be profitable even in static websites is the [interpolation engine](./interpolation.md). And that it works out of the box. +Note: until now (1.1.8), the compressed library is 40k - it might be an overkill as the library is designed for fullstack optimization. The big thing that can still be profitable even in static websites is the [interpolation engine](./interpolation.md), that it translates on-the-fly (without reloading the page) and that it works out of the box. ## Script diff --git a/package-lock.json b/package-lock.json index 1222ff6..6cb593e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,23 +1,26 @@ { "name": "omni18n", - "version": "1.1.8", + "version": "1.1.11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "omni18n", - "version": "1.1.8", + "version": "1.1.11", "license": "ISC", "dependencies": { - "command-line-args": "^5.2.1", "json5": "^2.2.3" }, + "bin": { + "extractLocales": "bin/extractLocales.js" + }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-terser": "^0.4.4", "@types/command-line-args": "^5.2.3", "@types/jest": "^29.5.12", + "command-line-args": "^5.2.1", "eslint": "^9.1.1", "jest": "^29.7.0", "jsdoc-to-markdown": "^8.0.1", @@ -2456,6 +2459,7 @@ "version": "5.2.1", "resolved": "https://registry.npmjs.org/command-line-args/-/command-line-args-5.2.1.tgz", "integrity": "sha512-H4UfQhZyakIjC74I9d34fGYDwk3XpSr17QhEd0Q3I9Xq1CETHo4Hcuo87WyWHpAF1aSLjLRf5lD9ZGX2qStUvg==", + "dev": true, "dependencies": { "array-back": "^3.1.0", "find-replace": "^3.0.0", @@ -2470,6 +2474,7 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/array-back/-/array-back-3.1.0.tgz", "integrity": "sha512-TkuxA4UCOvxuDK6NZYXCalszEzj+TLszyASooky+i742l9TqsOdYCMJJupxRic61hwquNtppB3hgcuq9SVSH1Q==", + "dev": true, "engines": { "node": ">=6" } @@ -2478,6 +2483,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/typical/-/typical-4.0.0.tgz", "integrity": "sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw==", + "dev": true, "engines": { "node": ">=8" } @@ -3228,6 +3234,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/find-replace/-/find-replace-3.0.0.tgz", "integrity": "sha512-6Tb2myMioCAgv5kfvP5/PkZZ/ntTpVK39fHY7WkWBgvbeE+VHd/tZuZ4mrC+bxh4cfOZeYKVPaJIZtZXV7GNCQ==", + "dev": true, "dependencies": { "array-back": "^3.0.1" }, @@ -3239,6 +3246,7 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/array-back/-/array-back-3.1.0.tgz", "integrity": "sha512-TkuxA4UCOvxuDK6NZYXCalszEzj+TLszyASooky+i742l9TqsOdYCMJJupxRic61hwquNtppB3hgcuq9SVSH1Q==", + "dev": true, "engines": { "node": ">=6" } @@ -4584,7 +4592,8 @@ "node_modules/lodash.camelcase": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", - "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" + "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==", + "dev": true }, "node_modules/lodash.memoize": { "version": "4.1.2", diff --git a/src/db/fileDb.ts b/src/db/fileDb.ts index b57eec2..6b900d4 100644 --- a/src/db/fileDb.ts +++ b/src/db/fileDb.ts @@ -3,8 +3,6 @@ import Defer from '../defer' import { WorkDictionary, type Locale, type TextKey, type Translation, type Zone } from '../types' import MemDB from './memDb' import serialization from './serialization' -import json5 from 'json5' -const { parse, stringify } = json5 export default class FileDB extends MemDB< KeyInfos, diff --git a/src/db/types.ts b/src/db/types.ts index b7f3534..9e15b06 100644 --- a/src/db/types.ts +++ b/src/db/types.ts @@ -8,7 +8,7 @@ export type RawDictionary = Record export interface DB { /** - * Retrieves all the values for a certain zone + * Retrieves all the values for a certain zone and certain locales * @param locales A list of locales to search for * @param zone The zone to search in * @returns A dictionary of key => [locale, text], where locale is the first on the list that has a translation @@ -16,6 +16,30 @@ export interface DB { list(locales: Locale[], zone: Zone): Promise } +/** + * Because a request retrieving "the first locale from a given list" might get really complex with some engines + * So, instead of implementing `DB` directly, `SimplifiedDB` can be extended by providing a `listLocale` function + * + * The list function will simply retrieve all the zone/locale keys/text and sort them programmatically + */ +export abstract class SimplifiedQueryDB implements DB { + /** + * Retrieves all the values for a certain zone and a certain locales + * @param locale The locale to search for + * @param zone The zone to search in + * @returns A dictionary of key => text + */ + abstract listLocale(locales: Locale, zone: Zone): Promise<[TextKey, Translation][]> + async list(locales: Locale[], zone: Zone) { + const rv: RawDictionary = {} + for (const locale of locales.reverse()) + for (const [key, text] of await this.listLocale(locale, zone)) rv[key] = [locale, text] + return rv + } +} + +// TODO? A caching class + export interface TranslatableDB extends DB { /** * Retrieves all the values for certain locales, in order for translators to work on it