-
Notifications
You must be signed in to change notification settings - Fork 11
/
i18n.js
64 lines (55 loc) · 1.54 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
53
54
55
56
57
58
59
60
61
62
63
64
import path from "path"
import deepmerge from "deepmerge"
import fs from "fs-extra"
import { globbySync } from "globby"
const LANGUAGE_NAMES = {
be: "Беларуская",
en: "English",
fr: "Français",
pl: "Polski",
ru: "Русский",
hi: "हिंदी",
es: "Español",
}
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 getLanguageFromPath(file) {
const basename = path.basename(file)
const lang = basename.substr(0, basename.indexOf("."))
return lang
}
function loadLocales() {
const paths = globbySync(["./locales/*.json"], {
cwd: __dirname,
absolute: true,
}).sort(
(a, b) =>
Object.keys(LANGUAGE_NAMES).indexOf(getLanguageFromPath(a)) -
Object.keys(LANGUAGE_NAMES).indexOf(getLanguageFromPath(b))
)
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, iso: locale.lang, name: langName })
settings.vueI18n.messages[locale.lang] = locale.strings
})
export default settings