-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathapp.js
117 lines (94 loc) · 2.99 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import express from "express";
import compression from "compression";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import Handlebars from "handlebars";
import { engine } from "express-handlebars";
import indexRoute from "./routes/index.js";
import aboutRoute from "./routes/about.js";
import joinRoute from "./routes/join.js";
import learnRoute from "./routes/learn.js";
import sitemapRoute from "./routes/sitemap.js";
import { I18n } from "i18n";
import fs from "fs";
import path from "node:path";
import { readdirSync, readFileSync } from "fs";
import { fileURLToPath } from "node:url";
import sortArrayOfObjects from "./modules/sort-array-of-objects.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
let translationData = [],
locales = [];
readdirSync(`${__dirname}/locales`)
.filter((file) => path.extname(file) === ".json")
.forEach((locale) => {
locale = locale.replace(".json", "");
locales.push(locale);
const translationDataObj = JSON.parse(
fs.readFileSync(
`${__dirname}/translations/${locale}/${locale}.json`,
"utf8"
)
);
translationDataObj.code = locale;
translationData.push(translationDataObj);
});
const i18n = new I18n({
directory: path.join(__dirname, "locales"),
autoReload: true,
defaultLocale: "en-us",
queryParameter: "lang",
cookie: "locale",
});
Handlebars.registerHelper("__", function () {
return i18n.__.apply(this, arguments);
});
Handlebars.registerHelper("__n", function () {
return i18n.__n.apply(this, arguments);
});
Handlebars.registerHelper("ifEquals", (firstArg, secondArg, options) => {
return firstArg === secondArg ? options.fn(this) : options.inverse(this);
});
const app = express();
app.use(compression());
app.use(express.static("public"));
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(cookieParser());
app.use(bodyParser.json());
app.use(i18n.init);
app.use((req, res, next) => {
const cookies = cookieParser.JSONCookies(req.cookies);
let currentLocale = "en-us";
res.locals.languages = i18n.getLocales();
res.translations = sortArrayOfObjects(translationData, "label_lat");
if (req.query.lang) {
currentLocale = req.query.lang;
} else if (cookies && cookies.locale) {
currentLocale = cookies.locale;
} else if (req.getLocale()) {
currentLocale = req.getLocale();
}
try {
res.currentLocale = translationData.filter(
(locale) => locale.code === currentLocale
)[0];
} catch (err) {
/* noop */
}
res.cookie("locale", currentLocale, {
maxAge: 365 * 24 * 60 * 60 * 1000
});
next();
});
app.engine("handlebars", engine());
app.set("view engine", "handlebars");
app.set("views", "./views");
app.use("/", indexRoute);
app.use("/about", aboutRoute);
app.use("/join", joinRoute);
app.use("/learn", learnRoute);
app.use("/sitemap.xml", sitemapRoute);
export default app;