Skip to content

Commit

Permalink
+grouped extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
eddow committed May 30, 2024
1 parent c54699a commit df76864
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}, ...`)
Expand Down
38 changes: 34 additions & 4 deletions docs/umd.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script src="/omni18n.js">["en", "fr"], "/dictionaries/$.js"</script>
<script src="omni18n.js">
["en", "fr"], "/dictionaries/$.js"
</script>
```

`fileNameTemplate` is obsolete if all the needed locales are loaded on a hard-coded way

```html
<script src="omni18n.js">
["en", "fr"]
</script>
<script src="dictionaries/en.js"></script>
<script src="dictionaries/fr.js"></script>
```

### 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
<script src="dictionary_hu.js"></script>
```

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
<script type="application/javascript">OmnI18n.translatePage()</script>
<script type="application/javascript">
OmnI18n.translatePage()
</script>
```

#### TODO
Expand All @@ -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
<div i18n="html: long.termsAndConditions"></div>
Expand All @@ -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`
32 changes: 27 additions & 5 deletions src/umd/extractLocales.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -19,6 +19,11 @@ const options = commandLineArgs(
alias: 'w',
type: Boolean
},
{
name: 'grouped',
alias: 'g',
type: String
},
{
name: 'input',
alias: 'i',
Expand Down Expand Up @@ -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')
}
}
}

Expand Down

0 comments on commit df76864

Please sign in to comment.