Skip to content

Commit

Permalink
feat: Add list method
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Oct 17, 2021
1 parent 1f623e4 commit fc917c2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!' }
```

---
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": "@secjs/intl",
"version": "1.0.1",
"version": "1.0.2",
"description": "",
"license": "MIT",
"author": "João Lenon",
Expand Down
20 changes: 18 additions & 2 deletions src/Sntl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -72,6 +88,6 @@ export class Sntl {

Sntl._tempLocale = null

return { message }
return message
}
}
18 changes: 15 additions & 3 deletions tests/sntl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})

Expand All @@ -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',
})

Expand All @@ -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}}!',
})
})
})

0 comments on commit fc917c2

Please sign in to comment.