From df7686457bd6503d4e1a576dd3502a607b828530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Marie=20De=20Mey?= Date: Thu, 30 May 2024 08:32:33 +0300 Subject: [PATCH] +grouped extraction --- README.md | 6 +++--- docs/umd.md | 38 ++++++++++++++++++++++++++++++++++---- src/umd/extractLocales.ts | 32 +++++++++++++++++++++++++++----- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a19a1ce..6ac6d8b 100644 --- a/README.md +++ b/README.md @@ -29,18 +29,18 @@ The server: - exposes a `condense` function that retrieve a condensed (processed) version of the dictionary for a locale (completely json-able). The client part is a [`I18nClient`](./docs/client.md) that will remember a locale and manage the queries to a server and language changes. -This client will produce `Translator`s who are described in typescript by the type `any`, or you can specify yours for your dictionary structure. +This client will produce [`Translator`](./docs/translator.md)s who make some wizardry like a proxy on a function that can be a string to describe text retrieval. ### Server side -Note: For some obscure reasons, the library is completely working on [static applications](./docs/umd.md) +> :information_source: For some obscure reasons, the library is completely working on [static applications](./docs/umd.md) ```ts import { I18nServer, I18nClient } from 'omni18n' const server = new I18nServer(myDBinterface) const client = new I18nClient(['en-US'], server.condense) -const T = await client.enter() // Here is where the actual DB-query occurs +const T = await client.enter() // Here is where the actual DB-query occurs // Will all display the entry `msg.hello` for the `en-US` (or `en`) locale console.log(`${T.msg.hello}, ...`) diff --git a/docs/umd.md b/docs/umd.md index bed40f1..ea5ea5e 100644 --- a/docs/umd.md +++ b/docs/umd.md @@ -38,24 +38,39 @@ Beside, ### Script-less A script-less way to use the library is by providing the arguments (`locales`, `fileNameTemplate`) directly in the script tag as js values + ```html - + ``` `fileNameTemplate` is obsolete if all the needed locales are loaded on a hard-coded way +```html + + + +``` + ### Translation blinking We speak about the "blink" when the page is just loaded and still displayed in its native language for half a second before being translated in the target language. -[*For now*](#todo), the solution needs to specify manually all the locales who shouldn't blink. +[_For now_](#todo), the solution needs to specify manually all the locales who shouldn't blink. + ```html ``` Also, as many mobile webapp tend to let the resource loading at the end of the page, hurrying the translation by inserting a `translatePage` between the page content and the late loads (audio/scripts/...) can show useful. + ```html - + ``` #### TODO @@ -80,7 +95,7 @@ Though, the value can contain an attribute And indeed, several attributes and the content separated by `,`s -A special (non-)attribute is `html`. Normally, the *text* is set. +A special (non-)attribute is `html`. Normally, the _text_ is set. ```html
@@ -104,3 +119,18 @@ Having a div id-ed such and some CSS is enough to have a default language picker Dictionary files are javascript files that have to be generated from a regular `I18nServer`. A [script](../src/umd/extractLocales.ts) is provided to generate them from a [FileDB](./db.md#filedb) in `bin/extractLocales.mjs` and can easily be extended to any other DB source (it interfaces with `I18nServer` only) + +```sh +node extractLocales -i myFile.db en fr hu +``` + +The script accept these arguments: +- without flags - `locales`: The locales to extract (will be the ones used by the application) +- `--input`/`-i` - input (**mandatory**): the input file (a serialized [FileDB](db.md#filedb)) +- `--output`/`-o` - output directory: if different from the directory where the input is +- `--watch`/`-w`: stay active until killed and extract each time the DB file is modified + +Then, 2 possibilities: +- `--pattern`/`-p`: Gives a pattern for each file. This is a filename where `$` is replaced by the locale +- `--grouped`/`-g`: Gives the filename who will contain all the locales +- By default, `pattern` is `$.js` \ No newline at end of file diff --git a/src/umd/extractLocales.ts b/src/umd/extractLocales.ts index 798ca5e..5e86064 100644 --- a/src/umd/extractLocales.ts +++ b/src/umd/extractLocales.ts @@ -1,5 +1,5 @@ import { mkdir, writeFile, watch } from 'fs/promises' -import { dirname, join } from 'path' +import { dirname, join, basename } from 'path' import commandLineArgs from 'command-line-args' import json5 from 'json5' import { FileDB } from 'src/db' @@ -19,6 +19,11 @@ const options = commandLineArgs( alias: 'w', type: Boolean }, + { + name: 'grouped', + alias: 'g', + type: String + }, { name: 'input', alias: 'i', @@ -50,12 +55,29 @@ if (!options.input) fatal('Input must be specified') const fdb = new FileDB(options.input), server = new I18nServer(fdb) -async function exportLocales() { +async function* exported() { for (const locale of options.locales) { - const condensed = await server.condense([locale, ...options.locales]), - output = join(options.output, options.pattern.replace('$', locale)) + const condensed = stringify(await server.condense([locale, ...options.locales])) + yield { + locale, + content: `OmnI18n.preload('${locale}', ${condensed})` + } + } +} + +async function exportLocales() { + if (options.grouped) { + let total = '' + for await (const { content } of exported()) total += content + '\n' + const output = join(options.output, options.grouped) console.log('->', output) - await writeFile(output, `OmnI18n.preload('${locale}', ${stringify(condensed)})`, 'utf8') + await writeFile(output, total, 'utf8') + } else { + for await (const { locale, content } of exported()) { + const output = join(options.output, options.pattern.replace('$', locale)) + console.log('->', output) + await writeFile(output, content, 'utf8') + } } }