-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (59 loc) · 1.71 KB
/
Copy pathindex.js
File metadata and controls
70 lines (59 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs')
const path = require('path');
var locale = new Intl.DateTimeFormat().resolvedOptions().locale.replace(`-`, `_`)
var directory = `locales`
var file = `main`
const setLocale = ( l ) => locale = l
const setDirectory = ( d ) => directory = d
const setFile = ( f ) => file = f
const load = ( my_locale ) =>
{
try
{
let dir = path.dirname(require.main.filename);
dir = `${dir}/${getDirectory()}`
if ( fs.existsSync(dir) )
{
dir = `${dir}/${my_locale || getLocale()}`
if ( fs.existsSync(dir) )
{
dir = `${dir}/${getFile()}.json`
if ( fs.existsSync(dir) )
{
return require(dir)
}
else
{
console.error(`Directory no found '${dir}' in your project!`)
}
}
else
{
console.error(`Directory '${dir}' not found in your project!`)
}
}
else
{
console.error(`Directory '${dir}' not found on the root of your project!`)
}
}
catch (e)
{
const translations = require('../../locales/index')
return translations[`${my_locale || getLocale()}_${getFile()}`] || {}
}
}
const getDirectory = () => directory
const getFile = () => file
const getLocale = () => locale
const t = ( text, arr = {}, locale = null ) =>
{
const translator = load(locale)
text = translator[text] || text
for ( let key in arr )
text = text.replace(`{${key}}`, arr[key])
return text
}
module.exports = {
t, setLocale, setDirectory, setFile, getDirectory, getFile, getLocale
}