forked from bitcart/bitcart-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.js
52 lines (44 loc) · 1.19 KB
/
i18n.js
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
import path from "path"
import deepmerge from "deepmerge"
import fs from "fs-extra"
import { globbySync } from "globby"
const LANGUAGE_NAMES = {
be: "Беларуская",
en: "English",
pl: "Polski",
ru: "Русский",
hi_IN: "हिंदी"
}
const settings = {
locales: [],
defaultLocale: "en",
vueI18n: { locale: "en", fallbackLocale: "en", messages: {} },
}
const locales = []
function addLocale(locale) {
const existing = locales.find((l) => l.lang === locale.lang)
if (existing) {
existing.strings = deepmerge(existing.strings, locale.strings)
} else {
locales.push(locale)
}
}
function loadLocales() {
const paths = globbySync(["./locales/*.json"], {
cwd: __dirname,
absolute: true,
})
paths.forEach((file) => {
const basename = path.basename(file)
const lang = basename.substr(0, basename.indexOf("."))
const strings = fs.readJsonSync(file)
addLocale({ lang, strings })
})
}
loadLocales()
locales.forEach((locale) => {
const langName = LANGUAGE_NAMES[locale.lang] || locale.lang
settings.locales.push({ code: locale.lang, name: langName })
settings.vueI18n.messages[locale.lang] = locale.strings
})
export default settings