Skip to content

Commit

Permalink
refactor: get locale data
Browse files Browse the repository at this point in the history
  • Loading branch information
LoeiFy committed Nov 24, 2018
1 parent 4e7f9a1 commit 50ef3f8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
const getLocale = require('./lib/locale')
const parser = require('./lib/parser')
const getLocale = require('./lib/locale')

module.exports = class {
constructor(dir, locale) {
this.dir = dir
this.localeData = locale ? getLocale(dir, locale) : {}
this.currentLocale = locale
}

set locale(locale) {
this.localeData = getLocale(this.dir, locale)
this.currentLocale = locale
}

get locale() {
return this.currentLocale
}

parser(type, ...args) {
const localeData = getLocale(this.dir, this.currentLocale)
return parser(type, localeData, ...args)
}

__(...args) {
return parser('_', this.localeData, ...args)
return this.parser('_', ...args)
}

_n(...args) {
return parser('n', this.localeData, ...args)
return this.parser('n', ...args)
}
}
9 changes: 2 additions & 7 deletions lib/locale.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
const { existsSync, readFileSync } = require('fs')
const { readFileSync } = require('fs')
const { join } = require('path')
const yaml = require('js-yaml')
const logger = require('@acyort/logger')()

module.exports = (dir, locale) => {
const path = join(dir, `${locale}.yml`)

if (!existsSync(path)) {
logger.warn(`Cannot load locale \`${locale}\``)
return {}
}

try {
return yaml.safeLoad(readFileSync(path, 'utf8'))
} catch (e) {
logger.warn(`Cannot load locale \`${locale}\``)
logger.warn(`cannot load locale \`${locale}\``)
return {}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acyort/i18n",
"version": "0.1.1",
"version": "0.1.2",
"description": "Node i18n",
"main": "index.js",
"files": [
Expand Down

0 comments on commit 50ef3f8

Please sign in to comment.