Skip to content

Commit

Permalink
export fix + restruct
Browse files Browse the repository at this point in the history
  • Loading branch information
eddow committed May 16, 2024
1 parent d845993 commit 1f65ec8
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 178 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ 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` and `omni18n/client`. Only load the latter in the browser.
### Server side

```ts
Expand Down Expand Up @@ -158,6 +156,7 @@ Or the [object-oriented way](./docs/client.md#oo-reporting) by overriding these
missing(key: string, fallback: Translation | undefined, zones: Zone[]): string
error(key: string, error: string, spec: object, zones: Zone[]): string
```

## Integrations

- [Svelte4](https://github.com/eddow/omni18n-svelte4)
Expand Down
20 changes: 0 additions & 20 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@ Projects using OmnI18n use it in 4 layers
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

## 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
Expand Down
4 changes: 2 additions & 2 deletions docs/interpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The syntax `{other.text.key | arg1 | arg2}` can be used to do such.
The syntax also allow some processing specification, when a processor name (with no `.` in it) is used instead of a first element. The available processors can be extended :

```ts
import { processors, type TContext } from 'omni18n/client';
import { processors, type TContext } from 'omni18n';

Object.assign(processors, {
myProc(this: TContext, arg1: any, ...args: any[]) {
Expand Down Expand Up @@ -96,7 +96,7 @@ example: `{upper | $1}` will render the first argument in upper-case
A list of predefined options can be set in exported variables

```ts
import { formats } from 'omni18n/client'
import { formats } from 'omni18n'

formats.date.year = { year: 'numeric' }
formats.number.arabic = { numberingSystem: 'arab' }
Expand Down
30 changes: 14 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
{
"name": "omni18n",
"version": "1.1.6",
"version": "1.1.7",
"exports": {
"./client": {
"import": "./lib/esm/client.js",
"require": "./lib/cjs/client.js",
"types": "./lib/src/client.d.ts"
},
".": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js",
"types": "./lib/src/index.d.ts"
},
"./src/client": {
"import": "./src/client.ts",
"types": "./src/client.ts"
"browser": {
"import": "./lib/esm/client.js",
"require": "./lib/cjs/client.js",
"types": "./lib/src/client.d.ts"
},
"default": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js",
"types": "./lib/src/index.d.ts"
}
},
"./src": {
"import": "./src/index.ts",
"types": "./src/index.ts"
"./ts": {
"browser": "./src/client.ts",
"default": "./src/index.ts"
}
},
"description": "",
Expand Down
29 changes: 3 additions & 26 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
export {
type CondensedDictionary,
type Condense,
type Locale,
type OnModification,
type TextKey,
type Translation,
type Zone,
type WorkDictionary,
type WorkDictionaryEntry,
type WorkDictionaryText
} from './types'
export {
I18nClient,
type TContext,
getContext,
TranslationError,
type ClientDictionary,
type Translator,
reports,
bulkObject,
bulkDictionary,
formats,
processors
} from './client/index'
export { localeFlags, flagCodeExceptions } from './flags'
export * from './types'
export * from './client/index'
export * from './flags'

declare global {
interface Set<T> {
Expand Down
1 change: 1 addition & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as MemDB, type MemDBDictionary, type MemDBDictionaryEntry } from './memDb'
export { default as FileDB } from './fileDb'
export * from './types'
3 changes: 1 addition & 2 deletions src/db/memDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import {
WorkDictionary,
WorkDictionaryEntry,
WorkDictionaryText,
type InteractiveDB,
type Locale,
type RawDictionary,
type TextKey,
type Translation,
type Zone
} from '../types'
import { type InteractiveDB, type RawDictionary } from './types'

interface SystemEntry<KeyInfos extends {}, TextInfos extends {}> {
'.zone'?: Zone
Expand Down
75 changes: 75 additions & 0 deletions src/db/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Locale, TextKey, Translation, WorkDictionary, Zone } from '../types'

/**
* Dictionary used between the server and the DB
* key => [locale, text]
*/
export type RawDictionary = Record<TextKey, [Locale, Translation]>

export interface DB {
/**
* Retrieves all the values for a certain zone
* @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
*/
list(locales: Locale[], zone: Zone): Promise<RawDictionary>
}

export interface TranslatableDB<TextInfos extends {} = {}> extends DB {
/**
* Retrieves all the values for certain locales, in order for translators to work on it
* @param locales
*/
workList(locales: Locale[]): Promise<WorkDictionary>

/**
* Modifies/add the value for the key [key, locale]
* Note: checks that the key exists
* @param key text key
* @param locale A language and perhaps a country
* @param text A text value
* @returns The zone where the key is stored or false if no change were brought
*/
modify(
key: TextKey,
locale: Locale,
text: Translation,
textInfos?: Partial<TextInfos>
): Promise<void>
}

export interface EditableDB<KeyInfos extends {} = {}, TextInfos extends {} = {}>
extends TranslatableDB<TextInfos> {
/**
* Creates or modifies a key
* @param key The key to manipulate
* @param zone The zone to create the key in, or undefined to delete it
* @returns Wether the zone was changed
*/
key(key: TextKey, zone: Zone, keyInfos?: Partial<KeyInfos>): Promise<boolean>

/**
* Renames a key - or removes it (and its translation) if newKey is undefined
* @param key
* @param newKey
* @returns All the information about that key (actually useful on deletion)
*/
reKey(key: TextKey, newKey?: TextKey): Promise<{ zone: Zone; texts: Record<Locale, Translation> }>
}

export interface InteractiveDB<KeyInfos extends {} = {}, TextInfos extends {} = {}>
extends EditableDB<KeyInfos, TextInfos> {
/**
* Retrieves all the translations given for a certain key
* @param key The key to search for
*/
get(key: TextKey): Promise<Record<Locale, Translation>>

/**
* Checks if a key is specified in a certain locale
* @param key The key to search for
* @param locales The locales to search in
*/
getZone(key: TextKey, locales?: Locale[]): Promise<Zone | false>
}
37 changes: 3 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,3 @@
export {
type CondensedDictionary,
type Condense,
type Locale,
type OnModification,
type TextKey,
type Translation,
type Zone,
type WorkDictionary,
type WorkDictionaryEntry,
type WorkDictionaryText,
I18nClient,
type TContext,
getContext,
TranslationError,
type ClientDictionary,
type Translator,
reports,
bulkObject,
bulkDictionary,
formats,
processors,
localeFlags,
flagCodeExceptions
} from './client'
export { type RawDictionary, type InteractiveDB, type DB } from './types'
export {
I18nServer,
InteractiveServer,
type Modification,
specs2url,
url2specs
} from './server/index'
export { FileDB, MemDB, type MemDBDictionary, type MemDBDictionaryEntry } from './db/index'
export * from './client'
export * from './server'
export * from './db'
2 changes: 1 addition & 1 deletion src/server/interactive.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
WorkDictionary,
type CondensedDictionary,
type InteractiveDB,
type Locale,
type TextKey,
type Translation,
type Zone
} from '../types'
import { type InteractiveDB } from '../db'
import I18nServer, { localeTree } from './server'

const subscriptions = new Map<
Expand Down
3 changes: 1 addition & 2 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DB, RawDictionary } from '../db'
import {
DB,
type CondensedDictionary,
type Locale,
type RawDictionary,
type TextKey,
type Translation,
type Zone
Expand Down
73 changes: 0 additions & 73 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ export type CondensedDictionary = {

export type Condense = (locales: Locale[], zones: Zone[]) => Promise<CondensedDictionary[]>
export type OnModification = (keys?: TextKey[]) => void
/**
* Dictionary used between the server and the DB
* key => [locale, text]
*/
export type RawDictionary = Record<TextKey, [Locale, Translation]>
export type WorkDictionaryText<TextInfos extends {} = {}> = {
text?: Translation
infos?: TextInfos
Expand All @@ -32,71 +27,3 @@ export type WorkDictionaryEntry<KeyInfos extends {} = {}, TextInfos extends {} =
* Dictionary used for translator-related operations
*/
export type WorkDictionary = Record<TextKey, WorkDictionaryEntry>

export interface DB {
/**
* Retrieves all the values for a certain zone
* @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
*/
list(locales: Locale[], zone: Zone): Promise<RawDictionary>
}

export interface TranslatableDB<TextInfos extends {} = {}> extends DB {
/**
* Retrieves all the values for certain locales, in order for translators to work on it
* @param locales
*/
workList(locales: Locale[]): Promise<WorkDictionary>

/**
* Modifies/add the value for the key [key, locale]
* Note: checks that the key exists
* @param key text key
* @param locale A language and perhaps a country
* @param text A text value
* @returns The zone where the key is stored or false if no change were brought
*/
modify(
key: TextKey,
locale: Locale,
text: Translation,
textInfos?: Partial<TextInfos>
): Promise<void>
}

export interface EditableDB<KeyInfos extends {} = {}, TextInfos extends {} = {}>
extends TranslatableDB<TextInfos> {
/**
* Creates or modifies a key
* @param key The key to manipulate
* @param zone The zone to create the key in, or undefined to delete it
* @returns Wether the zone was changed
*/
key(key: TextKey, zone: Zone, keyInfos?: Partial<KeyInfos>): Promise<boolean>

/**
* Renames a key - or removes it (and its translation) if newKey is undefined
* @param key
* @param newKey
* @returns All the information about that key (actually useful on deletion)
*/
reKey(key: TextKey, newKey?: TextKey): Promise<{ zone: Zone; texts: Record<Locale, Translation> }>
}

export interface InteractiveDB<KeyInfos extends {} = {}, TextInfos extends {} = {}>
extends EditableDB<KeyInfos, TextInfos> {
/**
* Retrieves all the translations given for a certain key
* @param key The key to search for
*/
get(key: TextKey): Promise<Record<Locale, Translation>>

/**
* Checks if a key is specified in a certain locale
* @param key The key to search for
* @param locales The locales to search in
*/
getZone(key: TextKey, locales?: Locale[]): Promise<Zone | false>
}

0 comments on commit 1f65ec8

Please sign in to comment.