diff --git a/package.json b/package.json index 8c4e17a..5d78ecd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@secjs/intl", - "version": "1.0.3", + "version": "1.0.4", "description": "", "license": "MIT", "author": "João Lenon", diff --git a/src/Sntl.ts b/src/Sntl.ts index baa660a..7ea1e1a 100644 --- a/src/Sntl.ts +++ b/src/Sntl.ts @@ -1,5 +1,6 @@ import { parse } from 'path' import { getFolders } from '@secjs/utils' +import { getFoldersSync } from './utils/getFoldersSync' import { NotImplementedException } from '@secjs/exceptions' export class Sntl { @@ -7,6 +8,12 @@ export class Sntl { private static _defaultLocale: string private static locales: Map> = new Map() + constructor() { + Sntl.locales.clear() + Sntl._tempLocale = null + Sntl._defaultLocale = null + } + async load() { const path = `${process.cwd()}/resources/locales` @@ -27,6 +34,26 @@ export class Sntl { return this } + loadSync() { + const path = `${process.cwd()}/resources/locales` + + const { folders } = getFoldersSync(path, true) + + folders.forEach(folder => { + const filesMap = new Map() + + folder.files.forEach(file => { + if (typeof file.value !== 'string') return + + filesMap.set(parse(file.name).name, JSON.parse(file.value)) + }) + + Sntl.locales.set(folder.path, filesMap) + }) + + return this + } + setDefaultLocale(locale: string) { Sntl._defaultLocale = locale diff --git a/src/utils/getFoldersSync.ts b/src/utils/getFoldersSync.ts new file mode 100644 index 0000000..e0f4cf4 --- /dev/null +++ b/src/utils/getFoldersSync.ts @@ -0,0 +1,46 @@ +import { resolve, basename } from 'path' +import { readdirSync, readFileSync } from 'fs' +import { DirectoryContract } from '@secjs/contracts' + +/** + * Return all folders of a directory and files inside + * + * @param dir The directory + * @param withFiles If need to get files inside folders by default false + * @param buffer Return the buffer of the file by default false + * @param fullPath Return the full path of folder or archive by default false + * @return The directory root with sub folders and files when withFiles true + */ +export function getFoldersSync( + dir: string, + withFiles = false, + buffer = false, + fullPath = false, +): DirectoryContract { + const dirents = readdirSync(dir, { withFileTypes: true }) + + const directory = { + path: fullPath ? resolve(dir) : basename(resolve(dir)), + files: [], + folders: [], + } + + for (const dirent of dirents) { + const res = resolve(dir, dirent.name) + + if (dirent.isDirectory()) { + directory.folders.push(getFoldersSync(res, withFiles, buffer, fullPath)) + + continue + } + + if (dirent.isFile() && withFiles) { + directory.files.push({ + name: fullPath ? res : basename(res), + value: buffer ? Buffer.from(res) : readFileSync(res, 'utf-8'), + }) + } + } + + return directory +} diff --git a/tests/sntl.spec.ts b/tests/sntl.spec.ts index c321673..8973538 100644 --- a/tests/sntl.spec.ts +++ b/tests/sntl.spec.ts @@ -44,4 +44,12 @@ describe('\n Sntl', () => { test: 'Hello my name is {{name}}!', }) }) + + it('should be able to reload Sntl locales and load it sync', async () => { + new Sntl().setDefaultLocale('en-us').loadSync() + + const message = Sntl.formatMessage('stub.test', { name: 'João' }) + + expect(message).toBe('Hello my name is João!') + }) })