From fc917c20e7ce9915fe48dc92ce2b1771a49d592a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Sun, 17 Oct 2021 02:24:06 -0300 Subject: [PATCH] feat: Add list method --- README.md | 15 +++++++++++---- package.json | 2 +- src/Sntl.ts | 20 ++++++++++++++++++-- tests/sntl.spec.ts | 18 +++++++++++++++--- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e2c2492..4dc793f 100644 --- a/README.md +++ b/README.md @@ -59,22 +59,29 @@ import { Sntl } from '@secjs/intl' await new Sntl.setDefaultLocale('en-us').load() Sntl.formatMessage('messages.greeting', { name: 'João' }) -// { message: 'Hello!, my name is João!' } +// 'Hello!, my name is João!' // Use forLocale to call for a specific locale in runtime Sntl.forLocale('pt-br').formatMessage('messages.greeting', { name: 'João', -}) // { message: 'Olá!, meu nome é João!' } +}) // 'Olá!, meu nome é João!' Sntl.forLocale('en-us').formatMessage('messages.greeting', { name: 'João', -}) // { message: 'Hello!, my name is João!' } +}) // 'Hello!, my name is João!' // Use changeLocale to change the defaultLocale in runtime Sntl.changeLocale('pt-br').formatMessage('messages.greeting', { name: 'João', }) -// { message: 'Olá!, meu nome é João!' } +// 'Olá!, meu nome é João!' + +Sntl.forLocale('en-us').formatMessage('messages.greeting', { + name: 'João', +}) // 'Hello!, my name is João!' + +// Use list to get all keys inside the json file +Sntl.list('stub', { name: 'João' }) // { test: 'Hello!, my name is João!' } ``` --- diff --git a/package.json b/package.json index 2f8a04d..7302bf9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@secjs/intl", - "version": "1.0.1", + "version": "1.0.2", "description": "", "license": "MIT", "author": "João Lenon", diff --git a/src/Sntl.ts b/src/Sntl.ts index 08cee0d..baa660a 100644 --- a/src/Sntl.ts +++ b/src/Sntl.ts @@ -53,7 +53,23 @@ export class Sntl { return this } - static formatMessage(key: string, fields?: any): { message: string } { + static list(key: string, fields?: any) { + const list = Sntl.locales + .get(Sntl._tempLocale || Sntl._defaultLocale) + .get(key) + + if (fields) { + Object.keys(fields).forEach(k => { + Object.keys(list).forEach(l => { + list[l] = list[l].replace(new RegExp(`{{\\s*${k}\\s*}}`), fields[k]) + }) + }) + } + + return list + } + + static formatMessage(key: string, fields?: any): string { const splitedKey = key.split('.') const keys = Sntl.locales @@ -72,6 +88,6 @@ export class Sntl { Sntl._tempLocale = null - return { message } + return message } } diff --git a/tests/sntl.spec.ts b/tests/sntl.spec.ts index 3f9b57d..c321673 100644 --- a/tests/sntl.spec.ts +++ b/tests/sntl.spec.ts @@ -6,13 +6,13 @@ describe('\n Sntl', () => { }) it('should be able to format messages using default', async () => { - const { message } = Sntl.formatMessage('stub.test', { name: 'João' }) + const message = Sntl.formatMessage('stub.test', { name: 'João' }) expect(message).toBe('Hello my name is João!') }) it('should be able to format messages in pt-br', async () => { - const { message } = Sntl.forLocale('pt-br').formatMessage('stub.test', { + const message = Sntl.forLocale('pt-br').formatMessage('stub.test', { name: 'João', }) @@ -22,7 +22,7 @@ describe('\n Sntl', () => { it('should be able to change default locale in runtime', async () => { Sntl.changeLocale('pt-br') - const { message } = Sntl.formatMessage('stub.test', { + const message = Sntl.formatMessage('stub.test', { name: 'João', }) @@ -32,4 +32,16 @@ describe('\n Sntl', () => { it('should be able to see what is the default locale in Sntl', async () => { expect(Sntl.defaultLocale()).toBe('pt-br') }) + + it('should be able to list all the messages inside the key and pass fields', async () => { + expect(Sntl.list('stub')).toStrictEqual({ + test: 'Olá meu nome é {{ name }}!', + }) + expect(Sntl.list('stub', { name: 'João' })).toStrictEqual({ + test: 'Olá meu nome é João!', + }) + expect(Sntl.forLocale('en-us').list('stub')).toStrictEqual({ + test: 'Hello my name is {{name}}!', + }) + }) })