UNPKG

1.39 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const path = require('path')
3const osLocale = require('os-locale')
4
5/**
6 * CMD环境:根据本机系统语言,获取语言包内容
7 * @returns {Object} 语言包内容
8 */
9module.exports = () => {
10 const locale = osLocale.sync()
11 let pathname
12
13 pathname = path.resolve(__dirname, `../locales/${locale}.json`)
14 if (fs.existsSync(pathname)) return fs.readJsonSync(pathname)
15
16 pathname = path.resolve(__dirname, `../locales/${locale.toLowerCase()}.json`)
17 if (fs.existsSync(pathname)) return fs.readJsonSync(pathname)
18
19 pathname = path.resolve(__dirname, `../locales/${locale.replace(/_/g, '-')}.json`)
20 if (fs.existsSync(pathname)) return fs.readJsonSync(pathname)
21
22 pathname = path.resolve(__dirname, `../locales/${locale.replace(/_/g, '-').toLowerCase()}.json`)
23 if (fs.existsSync(pathname)) return fs.readJsonSync(pathname)
24
25 pathname = path.resolve(__dirname, `../locales/${locale.split('_')[0]}.json`)
26 if (fs.existsSync(pathname)) return fs.readJsonSync(pathname)
27
28 pathname = path.resolve(__dirname, `../locales/en.json`)
29 return fs.readJsonSync(pathname)
30
31 // const l = fs.existsSync(path.resolve(__dirname, `../locales/${locale.toLowerCase()}.js`))
32 // ? require(`../locales/${locale.toLowerCase()}`)
33 // : require(`../locales/en_us`)
34 // return l
35}