Skip to content

Commit

Permalink
coverage checking...
Browse files Browse the repository at this point in the history
  • Loading branch information
eddow committed May 14, 2024
1 parent fcdbe98 commit 646f372
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
5 changes: 2 additions & 3 deletions docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Both return a string to display instead of the translated value.
```ts
export interface TContext {
key: string
zones: string[]
client: I18nClient
}
```
Expand Down Expand Up @@ -79,8 +78,8 @@ reports.error = ({ key, client }: TContext, error: string, spec: object) => {
Just override the `missing` and `error` members of `I18nClient` (who call the global `reports` by default)

```ts
missing(key: string, fallback: string | undefined, zones: Zone[]): string
error(key: string, error: string, spec: object, zones: Zone[]): string
missing(key: string, fallback: string | undefined): string
error(key: string, error: string, spec: object): string
```

## SSR: Between clients
Expand Down
5 changes: 5 additions & 0 deletions docs/interpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ In the braces, every character is escaped with the `\` character, even the `\` c
Several values can be given for defaulting:
`"This is a {$1}"` will have to be called with an argument, `"This is a {$1|distraction}"` may be called with an argument.

To play with interpolation giving direct values for test purpose, one can use:
```js
client.interpolate('test', 'qwe {$1} asd {$2} zxc', 'abc', 'xyz')
```

## Arguments

> :information_source: While interpolating, the argument nr 0 is the key, the first argument is the argument nr 1. This is meant to be used by translators - literacy peeps - so of course the first argument has the number "1".
Expand Down
14 changes: 8 additions & 6 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class I18nClient implements OmnI18nClient {
await this.download(Array.from(toLoad))
})
}
return translator({ client: this, zones, key: '' })
return translator({ client: this, key: '' })
}

public getPartialLoad(excludedZones: Zone[] = []): PartialLoad {
Expand Down Expand Up @@ -156,13 +156,15 @@ export default class I18nClient implements OmnI18nClient {
this.onModification?.(Object.keys(entries))
}

interpolate: (context: TContext, text: Translation, args: any[]) => string = interpolate
interpolate(key: TextKey, text: Translation, ...args: any[]) {
return interpolate({ client: this, key }, text, args)
}

missing(key: string, fallback: Translation | undefined, zones: Zone[]): string {
return reports.missing({ key, client: this, zones }, fallback)
missing(key: string, fallback: Translation | undefined): string {
return reports.missing({ key, client: this }, fallback)
}
error(key: string, error: string, spec: object, zones: Zone[]): string {
return reports.error({ key, client: this, zones }, error, spec)
error(key: string, error: string, spec: object): string {
return reports.error({ key, client: this }, error, spec)
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/client/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ function entry(t: Translation, isFallback?: boolean): ClientDictionary {

export function reportMissing(context: TContext, fallback?: Translation) {
const { client, key } = context
return client.missing(key, fallback, context.zones)
return client.missing(key, fallback)
}

export function reportError(context: TContext, error: string, spec: object) {
const { client, key } = context
return client.error(key, error, spec, context.zones)
return client.error(key, error, spec)
}

export const reports = {
Expand Down Expand Up @@ -63,9 +63,9 @@ export function translate(context: TContext, args: any[]): string {
}

return value?.[1]
? client.interpolate(context, reportMissing(context, value[0]), args)
? client.interpolate(key, reportMissing(context, value[0]), ...args)
: value
? client.interpolate(context, value[0], args)
? client.interpolate(key, value[0], ...args)
: reportMissing(context)
}

Expand Down Expand Up @@ -219,9 +219,9 @@ export function bulkDictionary<T extends Translatable = Translatable>(
const subCtx = { ...context, key }
const value = () =>
context.client.interpolate(
subCtx,
key,
obj[fallback] ? reportMissing(subCtx, obj[text]) : obj[text]!,
args
...args
)
if (Object.keys(obj).every((k) => typeof k === 'symbol')) return value()
for (const [k, v] of Object.entries(obj))
Expand Down
5 changes: 4 additions & 1 deletion src/client/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ export function interpolate(context: TContext, text: string, args: any[]): strin
if (processed === null) throw Error(`Unprocessed case: ${proc}`)
apps.push((nextArgs = [processed, ...others]))
}
return apps[0].find((cas) => !!cas)
const rv = apps[0].find((cas) => !!cas)
return !rv || typeof rv === 'string'
? unescape(rv || '')
: reportError(context, 'Object return value', { rv })
}),
parts = text.split(/{.*?}/).map((part) =>
part
Expand Down
7 changes: 3 additions & 4 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ export interface OmnI18nClient {
locales: Locale[]
timeZone?: string
currency?: string
interpolate(context: TContext, text: Translation, args: any[]): string
interpolate(key: TextKey, text: Translation, ...args: any[]): string
onModification?: OnModification
missing(key: string, fallback: Translation | undefined, zones: Zone[]): string
error(key: string, error: string, spec: object, zones: Zone[]): string
missing(key: string, fallback: Translation | undefined): string
error(key: string, error: string, spec: object): string
}

export interface TContext<Client extends OmnI18nClient = OmnI18nClient> {
key: TextKey
zones: Zone[]
client: Client
}

Expand Down
10 changes: 10 additions & 0 deletions test/static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ beforeAll(async () => {
'.zone': ''
},
'specs.ordinal': { '': '{ordinal::$1}' },
'specs.escape': { en: '{$1|dot dot \\: ...}' },
'specs.cases': { fr: '{a: a, b: bé, c: cé, default: pff - $1 :: $1}' },
'emoji.flag': {
en: '🇬🇧',
fr: '🇫🇷',
Expand Down Expand Up @@ -92,6 +94,14 @@ beforeAll(async () => {
})

describe('basic functionalities', () => {
test('escapement', () => {
expect(T.en.specs.escape()).toBe('dot dot : ...')
})
test('cases', () => {
expect(T.be.specs.cases('a')).toBe('a')
expect(T.be.specs.cases('b')).toBe('bé')
expect(T.be.specs.cases('d')).toBe('pff - d')
})
test('several kind of text access', () => {
const fields = T.en.fld
expect('' + fields.name).toBe('Name')
Expand Down

0 comments on commit 646f372

Please sign in to comment.