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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
import express from "express";
import hbs, { ExpressHandlebars } from "express-handlebars";
import Handlebars from 'handlebars';
import cookieParser from "cookie-parser";
import i18next from "i18next";
import Backend from "i18next-fs-backend";
import { LanguageDetector, handle } from 'i18next-http-middleware';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import path from 'path';
const require = createRequire(import.meta.url);
const handlebarsI18next = require('handlebars-i18next');
// Recreate __dirname in ES module
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import routes from "./routes.js";
import frontend from "./routes/frontend.js";
import activitypub from "./routes/activitypub.js";
import event from "./routes/event.js";
import group from "./routes/group.js";
import staticPages from "./routes/static.js";
import magicLink from "./routes/magicLink.js";
import { initEmailService } from "./lib/email.js";
import { getI18nHelpers } from "./helpers.js";
import {
activityPubContentType,
alternateActivityPubContentType,
} from "./lib/activitypub.js";
import moment from "moment";
const app = express();
app.locals.sendEmails = initEmailService();
// function to construct __dirname with ES module
const getLocalesPath = () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
return path.join(__dirname, '..', 'locales');
};
async function initializeApp() {
// Cookies //
app.use(cookieParser());
// i18next configuration
await i18next
.use(Backend)
.use(LanguageDetector)
.init({
backend: {
loadPath: path.join(getLocalesPath(), '{{lng}}.json'),
},
fallbackLng: 'en',
preload: ['en-US', 'ja'],
supportedLngs: ['en','en-US', 'ja'],
nonExplicitSupportedLngs: true,
load: 'languageOnly',
debug: false,
detection: {
order: ['header', 'cookie'],
lookupHeader: 'accept-language',
lookupCookie: 'i18next',
caches: ['cookie']
},
interpolation: {
escapeValue: false
}
});
app.use(handle(i18next));
// to Switch language
app.use((req, res, next) => {
const currentLanguage = i18next.language;
i18next.changeLanguage(req.language);
const newLanguage = i18next.language;
// Uncomment for debugging
// console.log('Language Change:', {
// header: req.headers['accept-language'],
// detected: req.language,
// currentLanguage: currentLanguage,
// newLanguage: newLanguage
// });
next();
});
// Uncomment for debugging
// app.use((req, res, next) => {
// console.log('Language Detection:', {
// header: req.headers['accept-language'],
// detected: req.language,
// i18next: i18next.language
// });
// next();
// });
// View engine //
const hbsInstance: ExpressHandlebars = hbs.create({
defaultLayout: "main",
partialsDir: ["views/partials/"],
layoutsDir: "views/layouts/",
helpers: {
json: function (context: any) {
return JSON.stringify(context);
},
// add i18next helpers
...getI18nHelpers(),
plural: function (key: string, count: number, options: any) { // Register the plural helper
const translation = i18next.t(key, { count: count });
return translation;
}
},
});
// calling i18nextHelper
if (typeof handlebarsI18next === 'function') {
handlebarsI18next(hbsInstance.handlebars, i18next);
} else if (typeof handlebarsI18next.default === 'function') {
handlebarsI18next.default(hbsInstance.handlebars, i18next);
} else {
console.error('handlebars-i18next helper is not properly loaded');
}
i18next.on('languageChanged', function(lng) {
moment.locale(lng);
});
app.engine("handlebars", hbsInstance.engine);
app.set("view engine", "handlebars");
app.set("hbsInstance", hbsInstance);
// Static files //
app.use(express.static("public"));
// Body parser //
app.use(express.json({ type: alternateActivityPubContentType }));
app.use(express.json({ type: activityPubContentType }));
app.use(express.json({ type: "application/json" }));
app.use(express.urlencoded({ extended: true }));
// Router //
app.use("/", staticPages);
app.use("/", frontend);
app.use("/", activitypub);
app.use("/", event);
app.use("/", group);
app.use("/", magicLink);
app.use("/", routes);
}
initializeApp().catch(console.error);
export default app;
|