diff --git a/package.json b/package.json index 2dc1597..7175dba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "omni18n", - "version": "1.0.3", + "version": "1.0.4", "description": "", "main": "dist/index.js", "module": "dist/index.js", diff --git a/src/client/client.ts b/src/client/client.ts index 3473b0e..8dfee8a 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -27,7 +27,6 @@ export default class I18nClient implements OmnI18nClient { private toLoadZones = new Set() private loadDefer = new Defer() - public loaded: Promise = Promise.resolve() public checkOnLoad = new Set() public timeZone?: string @@ -61,13 +60,13 @@ export default class I18nClient implements OmnI18nClient { * @param zones Zones entered * @returns The translator */ - public enter(...zones: string[]) { - zones.push('') + public async enter(...zones: string[]) { + if (!zones.includes('')) zones.push('') const knownZones = this.loadedZones.union(this.toLoadZones), toAdd = zones.filter((zone) => !knownZones.has(zone)) if (toAdd.length) { for (const t of toAdd) this.toLoadZones.add(t) - this.loaded = this.loadDefer.defer(async () => { + await this.loadDefer.defer(async () => { const toLoad = this.toLoadZones this.toLoadZones = new Set() await this.download(Array.from(toLoad)) diff --git a/src/client/helpers.ts b/src/client/helpers.ts index 6e4830e..b81df89 100644 --- a/src/client/helpers.ts +++ b/src/client/helpers.ts @@ -130,6 +130,8 @@ export function translator(context: TContext): Translator { return target case 'constructor': return String + case 'then': // Must be unthenable in order to be awaited + return 'Translators must be unthenable. `then` cannot be used as a text key.' case bulk: return translateBulk(context) } diff --git a/test/dynamic.test.ts b/test/dynamic.test.ts index ceb8856..24aeeb8 100644 --- a/test/dynamic.test.ts +++ b/test/dynamic.test.ts @@ -11,8 +11,8 @@ describe('Dynamic functionality', () => { server = new InteractiveServer( new WaitingDB( new MemDB({ - 'fld.name': { en: 'Name', '.zone': '' }, - 'cmd.customize': { en: 'Customize', 'en-UK': 'Customise', '.zone': '' }, + 'fld.name': { en: 'Name' }, + 'cmd.customize': { en: 'Customize', 'en-UK': 'Customise' }, 'cmd.save': { en: 'Save', '.zone': 'adm' }, 'cmd.modify': { en: 'Modify', '.zone': 'adm' } }) @@ -26,8 +26,7 @@ describe('Dynamic functionality', () => { } ) client = new I18nClient(['en-UK'], server.condense) - T = client.enter() - await client.loaded + T = await client.enter() }) test('regular feedback', async () => { @@ -63,8 +62,7 @@ describe('Dynamic functionality', () => { await server.save() expect(modifications).toEqual([]) expect(T.cmd.save()).toBe('[cmd.save]') - client.enter('adm') - await client.loaded + await client.enter('adm') expect(T.cmd.save()).toBe('Save it') await server.modify('cmd.save', 'en', 'Save') await server.save() diff --git a/test/specifics.test.ts b/test/specifics.test.ts index 6112798..38be2a3 100644 --- a/test/specifics.test.ts +++ b/test/specifics.test.ts @@ -25,7 +25,7 @@ describe('bulk', () => { } beforeAll(async () => { - const { T: lclT, client } = localStack({ + const { Tp } = localStack({ 'sub.obj.v1': { fr: 'fr-v1' }, 'sub.obj.v2': { en: 'en-v2' }, 'sub.obj.v3': { fr: 'fr-v3' }, @@ -34,8 +34,7 @@ describe('bulk', () => { 'struct.obj.sub.v3': { fr: 'fr-v3' }, 'struct.obj.sub': { fr: 'toString' } }) - T = lclT - await client.loaded + T = await Tp }) test('from object', async () => { @@ -65,15 +64,12 @@ describe('specifics', () => { }) test('fallbacks', async () => { misses.mockClear() - const { client, T } = localStack({ + const { Tp } = localStack({ 'fld.name': { en: 'Name' }, 'fld.bday': { en: 'Birthday', fr: 'Anniversaire' }, 'fld.bday.short': { en: 'Bday' } }) - expect('' + T.fld.name).toBe('...') - expect('' + T.fld.inexistent).toBe('...') - await client.loaded - expect(misses).toHaveBeenCalledWith('fld.inexistent') + const T = await Tp misses.mockClear() expect('' + T.fld.name).toBe('Name') expect(misses).toHaveBeenCalledWith('fld.name') diff --git a/test/static.test.ts b/test/static.test.ts index 70daa7c..2e8dd5c 100644 --- a/test/static.test.ts +++ b/test/static.test.ts @@ -77,8 +77,12 @@ beforeAll(async () => { clients.en.enter('adm') clients.be.timeZone = 'Europe/Brussels' clients.en.timeZone = 'Greenwich' - T = Object.fromEntries(Object.entries(clients).map(([key, value]) => [key, value.enter()])) - await Promise.all(Object.values(clients).map((client) => client.loaded)) + T = Object.fromEntries( + await Promise.all( + Object.entries(clients).map(async ([key, value]) => [key, await value.enter()]) + ) + ) + debugger }) describe('basic functionalities', () => { @@ -205,16 +209,14 @@ describe('parameters', () => { expect(T.en.cmd.ban()).toBe('Ban user') expect(T.be.cmd.ban()).toBe('[cmd.ban]') loads = [] - clients.be.enter('adm') - await clients.be.loaded + await clients.be.enter('adm') expect(loads).toEqual([{ locales: ['fr-BE'], zones: ['adm'] }]) expect(T.be.cmd.ban()).toBe("Bannir l'utilisateur") }) test('change locale', async () => { const client = new I18nClient(['en-US'], server.condense), - T: Translator = client.enter() - await client.loaded + T: Translator = await client.enter() expect(T.msg.greet()).toBe('Hello here') await client.setLocale(['fr']) expect(T.msg.greet()).toBe('Salut tout le monde') diff --git a/test/utils.ts b/test/utils.ts index 9be50d0..f6601ce 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -8,6 +8,6 @@ export function localStack( ) { const server = new I18nServer(new WaitingDB(new MemDB(dictionary))), client = new I18nClient(locales, server.condense), - T = client.enter(...zones) - return { server, client, T } + Tp = client.enter(...zones) + return { server, client, Tp } }