Skip to content

Commit

Permalink
+vue
Browse files Browse the repository at this point in the history
  • Loading branch information
eddow committed Jun 5, 2024
1 parent a4ad06a commit b35b6da
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ error(key: string, error: string, spec: object): string
## Integrations

- [SvelteKit](https://github.com/eddow/omni18n-svelte4) - Svelte4
- [Translator](https://github.com/eddow/omni18n-edit/releases) - To edit `FileDB` dictionaries
- [Nuxt](https://github.com/eddow/omni18n-vue) - Vue3
- [Translator](https://github.com/eddow/omni18n-edit/releases) - To edit `FileDB` dictionaries and give an idea of "translation page"

## TODO

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omni18n",
"version": "1.1.10",
"version": "1.1.11",
"exports": {
".": {
"browser": {
Expand Down
4 changes: 2 additions & 2 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class I18nClient implements OmnI18nClient {
if (toLoad.length) this.received(toLoad, await this.condense(this.locales, toLoad))
}

async setLocales(locales: Locale[]) {
setLocales(locales: Locale[], partial?: PartialLoad) {
locales = removeDuplicates(locales)
if (
this.locales.length === locales.length &&
Expand All @@ -143,7 +143,7 @@ export default class I18nClient implements OmnI18nClient {
this.loadedZones = new Set()
this.dictionary = {}
this.internals = {}
await this.loadDefer.defer()
this.loadDefer.defer()
}

modified(entries: Record<TextKey, Translation | undefined>) {
Expand Down
21 changes: 10 additions & 11 deletions src/client/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import {
text
} from './types'

const untranslatable = {
// Must be un-then-able in order to be awaited
then: undefined,
// Vue reference mechanism
__v_isRef: undefined,
__v_raw: undefined
}

function entry(t: Translation, isFallback?: boolean): ClientDictionary {
return { [text]: t, ...(isFallback ? { [fallback]: true } : {}) }
}
Expand Down Expand Up @@ -87,7 +95,9 @@ export function translator(context: TContext): Translator {
}
const primitive = <Translator>new Proxy(translation, {
get(target, key) {
if (key in untranslatable) return untranslatable[key as keyof typeof untranslatable]
switch (key) {
//? case 'toJSON': // Occurs on JSON.stringify
case 'toString':
case Symbol.toStringTag:
case 'valueOf':
Expand All @@ -96,17 +106,6 @@ export function translator(context: TContext): Translator {
return primitive
case 'constructor':
return String
case 'then': // Must be un-then-able in order to be awaited
return new Proxy(
{},
{
get(target, p, receiver) {
const msg = 'Translators must be unthenable. `then` cannot be used as a text key.'
if (p === 'toString') return msg
throw new TranslationError(msg)
}
}
)
case contextKey:
return context
}
Expand Down
9 changes: 5 additions & 4 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ export class TranslationError extends Error {
name = 'TranslationError'
}

export type Translator = ((...args: any[]) => string) & {
[k: TextKey]: Translator
[contextKey]: TContext
}
export type Translator = ((...args: any[]) => string) &
string & {
[k: TextKey]: Translator
[contextKey]: TContext
}

export type Translatable = { [key: TextKey]: Translatable | string }
7 changes: 4 additions & 3 deletions test/static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ describe('parameters', () => {
})

test('change locale', async () => {
const client = new I18nClient(['en-US'], server.condense),
T: Translator = await client.enter()
const client = new I18nClient(['en-US'], server.condense)
let T: Translator = await client.enter()
expect(T.msg.greet()).toBe('Hello here')
await client.setLocales(['fr'])
client.setLocales(['fr'])
T = await client.enter()
expect(T.msg.greet()).toBe('Salut tout le monde')
})
})
Expand Down

0 comments on commit b35b6da

Please sign in to comment.