Skip to content

Commit

Permalink
packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
eddow committed May 15, 2024
1 parent 6b7b2b8 commit cf579c2
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 129 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Generic i18n library managing the fullstack interaction in a CI/CD pace. The dic
- 1.0.x - ~~alpha~~
- 1.1.x - [beta](https://www.youtube.com/watch?v=1gSZfX91zYk)

🎉 1.1.5 🥳 The library finally has well-set entry points and export bundles

[![view on npm](https://badgen.net/npm/v/omni18n)](https://www.npmjs.org/package/omni18n)
[![npm module downloads](https://badgen.net/npm/dt/omni18n)](https://www.npmjs.org/package/omni18n)
[![Github repo dependents](https://badgen.net/github/dependents-repo/emedware/omni18n)](https://github.com/emedware/omni18n/network/dependents?dependent_type=REPOSITORY)
Expand All @@ -29,7 +31,7 @@ The server:
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.

> :warning: The library has 2 entry points: `omni18n/server` and `omni18n/client`. Please only load the latter in the browser. (The `server` contains indeed everything)
> :warning: The library has 2 entry points: `omni18n` and `omni18n/client`. Only load the latter in the browser.
### Server side

Expand Down
26 changes: 23 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@

The first document presents an [overview](../README.md), here is a more detailed description

> :warning: The library has 2 entry points: `omni18n/server` and `omni18n/client`. Please only load the latter in the browser. (The `server` contains indeed everything)
Projects using OmnI18n use it in 4 layers

1. [The `client`](./client.md): The client manages the cache and download along with providing [`Translator`s](./translator.md) that will [interpolate](./interpolation.md)
2. (optional) The HTTP or any other layer. This part is implemented by the user
3. [The `server`](./server.md): The server exposes functions to interact with the languages
4. [The `database`](./db.md): A class implementing some interface that interacts directly with a database

## Bonus - flags
## Entry points

The library has 2x2 entry points:

client/server: The server functionalities are not needed **and** harmful on client-side (try to ask chrome to import `node:fs` ...)

- The complete library `omni18n`
- The client part `omni18n/client`

bundled/source: The sources (TypeScript) are provided so that you can use your favorite bundler/debugger

- The bundled `omni18n`
- The source `omni18n/src`

And of course `omni18n/src/client` for the 2x2...

### umd

On the client side, it is also possible to reference the file `lib/omni18n.js` statically in the HTML code, every functionality will be in the `OmnI18n` global variable.

## Bonus

### Flags

```js
import { localeFlags, flagCodeExceptions }
Expand Down
79 changes: 43 additions & 36 deletions docs/db.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ In mongo or json-oriented DB, a key object could directly give a list of transla
The database interface is the one provided to the server. The first one is fairly simple:

```ts
type RawDictionary = Record<string, [Locale, string]>
export type RawDictionary = Record<TextKey, [Locale, Translation]>

interface DB {
list(locales: Locale[], zone: Zone): Promise<RawDictionary>
Expand All @@ -25,86 +25,91 @@ That `list` function is a glorified `SELECT` who gives all keys given in a zone

The DB role is purely to deal with a database. The [`server`](./server.md) will often mimic functions and their signature (`modify`, `reKey`, ...) and while the `server` role is to propagate information to both the client and the DB, a DB's role is purely the one of an adapter toward a database.

## InteractiveDB
### Glorified strings

A kind of API has been designed for the server to be able to _modify_ the content of the DB.
`Locale`, `Zone`, `TextKey`, `Translation` ... are basically strings.

### Infos
## TranslatableDB

Here, we get already in the realm where we can specify `KeyInfos` and `TextInfos`. The former is given by developers, in english or some common language if text is needed - and appear in the `keys` database - and the `TextInfo`, more often used/edited by the translators and appearing in the `translations` database.
Translation occur with simple "select/upsert" operations. There is _no key management_ here, no structure management, just content edition

If a database implementation is meant to be generic, it should store the `...Infos` as json I guess or something, but an application can specify both these generic arguments _and_ the database adapter to deal with it.
### ...Infos

Here, we get already in the realm where we can specify `KeyInfos` and `TextInfos`.

The former is given by developers, in english or some common language if comments are needed, it might contain the type (text/html/md/...) for the translation interface, &c. - and appear in the `keys` database

The `KeyInfo` might store information like notes from the dev, a flag to know if the text is pure, html, md, ... Whatever concerns development.
The latter more often used/edited by the translators and appearing in the `translations` database. (comment, "Keep the default value"="Do not translate" tag, &c.)

The `Textinfo` might store translation notes I guess, a link to a discussion with chatGPT, I really don't know - in case of doubt, let the default `{}`
If a database implementation is meant to be generic, it should store the `...Infos` as json I guess or something, but an application can specify both these generic arguments _and_ the database adapter to deal with it.

```ts
...Infos extends {} = {}
```

### Specific getters

#### Work list
### Work list

```ts
type WorkDictionaryText<TextInfos> = {
text: string
infos: TextInfos
text?: Translation
infos?: TextInfos
}
type WorkDictionaryEntry<KeyInfos, TextInfos> = {
texts: { [locale: Locale]: WorkDictionaryText<TextInfos> }
zone: Zone
infos: KeyInfos
}
type WorkDictionary = Record<string, WorkDictionaryEntry>

workList(locales: Locale[]): Promise<WorkDictionary>
```

Given a list of locales, find all their translations

> No `zone` fuss, and it's not "the first translation", it's all of them.
> No `zone` fuss: this is read-only at this level, and it's not "the first translation", it's all of them.
This function is indeed used to populate translator's list for working on it ... working list.

#### Get a single key, check whether a translation is specified
### Translate

```ts
get(key: string): Promise<Record<Locale, Translation>>
getZone(key: TextKey, locales?: Locale[]): Promise<Zone>
```
The first one retrieves the list of translations for a key, the second the key's zone IF some of the locales have a translation
### Setters
Sets the text/`TextInfo` for a given text-key/locale pair.

#### Translate
Returns the zone of the text-key if modified, `false` if the text-key was not found

> Write in the texts table, read in the keys table
```ts
modify(
key: string,
key: TextKey,
locale: Locale,
text: string,
text: Translation,
textInfos?: Partial<TextInfos>
): Promise<string | false>
): Promise<Zone | false>
```

Sets the text/`TextInfo` for a given text-key/locale pair.
Returns the zone of the text-key if modified, `false` if the text-key was not found
## EditableDB

#### Key management
Provides edition for the developer. (note: the querying goes through `workList` of `TranslatableDB`)

```ts
key(key: string, zone: string, keyInfos?: Partial<KeyInfos>): Promise<boolean>
reKey(key: string, newKey?: string): Promise<{ zone: string; locales: Locale[] }>
reKey(key: string, newKey?: string): Promise<{ zone: Zone; locales: Locale[] }>
```

`key` just upsert a key and its relative information.

`reKey` renames a key - into oblivion if no `newKey` (in the later case, remove also the translations)
`reKey` renames a key - into oblivion if no `newKey` (in the later case, removes also the translations)

## InteractiveDB

The last one has some little query functions used in interactive mode (ie. when the text changes should be populated to all clients when done)

```ts
get(key: string): Promise<Record<Locale, Translation>>
getZone(key: TextKey, locales?: Locale[]): Promise<Zone>
```

The first one retrieves the list of translations for a key, the second the key's zone IF some of the locales have a translation

## Provided providers

Expand All @@ -121,9 +126,11 @@ This allows:
- All the translations to simply be gathered under a file under source control (backup-able)
- The development activities (adding/removing/removing/rezoning a key) to be made and applied on commit/merge, and the "translation" (text-change) activities to still be available through the UI in real time

> :warning: The file should be in UTF-16 LE in strict `LF` mode
#### Recovering a file to export to a database

An `FileDB.analyze` function is exposed who takes the string to analyze and 2/3 callbacks
A `FileDB.analyze` function is exposed who takes the string to analyze and 2/3 callbacks

- `onKey` called when a new key is discovered
- `onText` called when a translation is discovered
Expand Down Expand Up @@ -156,11 +163,11 @@ A line beginning with no tabs is a key specification
A line beginning with one tab is a locale specification for the key "en cours"

```
[locale]:
[locale]:Some fancy translation
```

```
[locale][{ SomeTextInfos: 'hjson format' }]:
[locale][{ SomeTextInfos: 'hjson format' }]:Some fancy translation
```

##### 2-tabs
Expand Down
40 changes: 8 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 11 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
{
"name": "omni18n",
"version": "1.1.4",
"version": "1.1.5",
"exports": {
"./client": {
"import": "./lib/client.esm.js",
"require": "./lib/client.js",
"types": "./lib/client.d.ts"
"import": "./lib/esm/client.js",
"require": "./lib/cjs/client.js",
"types": "./lib/src/client.d.ts"
},
"./server": {
"import": "./lib/server.esm.js",
"require": "./lib/server.js",
"types": "./lib/index.d.ts"
".": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js",
"types": "./lib/src/index.d.ts"
},
"./src/client": {
"import": "./src/client.ts",
"require": "./src/client.ts",
"types": "./src/client.ts"
},
"./src/server": {
"./src": {
"import": "./src/index.ts",
"require": "./src/index.ts",
"types": "./src/index.ts"
}
},
"description": "",
"types": "./lib/index.d.ts",
"types": "./lib/src/index.d.ts",
"type": "module",
"scripts": {
"test": "jest",
Expand Down Expand Up @@ -60,7 +58,7 @@
"prettier": "^3.2.5",
"rollup": "^4.16.4",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-typescript": "^1.0.1",
"rollup-plugin-shim": "^1.0.0",
"rollup-plugin-typescript2": "^0.36.0",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",
Expand Down
Loading

0 comments on commit cf579c2

Please sign in to comment.