-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.js
247 lines (243 loc) · 7.77 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { createRequestHandler } from "@remix-run/express";
import fs from "fs/promises";
import path from "path";
import serveStatic from "serve-static";
import { walk } from "@root/walk";
const buildKnownStatics = async (prefix, root) => {
const knownStatics = new Set();
await walk(root, async (err, pathname, dirent) => {
if (!dirent.isDirectory()) {
knownStatics.add(`${prefix}/${path.relative(root, pathname)}`);
}
});
return knownStatics;
};
const buildHandlersForLocale = async (
buildDir,
locale,
{ mode, getLoadContext: providedGetLoadContext },
) => {
const handler = createRequestHandler({
build: await import(`./${buildDir}/${locale}/server/index.js`),
getLoadContext: async (req, res, next) => ({
i18n: res.locals.i18n,
}),
});
const _static = serveStatic(`${buildDir}/${locale}/client`, {
maxAge: 3_600_000,
});
const knownStatics = await buildKnownStatics(
`/${locale}`,
`${buildDir}/${locale}/client`,
);
const defaultHandler = createRequestHandler({
build: await import(`./${buildDir}/${locale}-default/server/index.js`),
getLoadContext: async (req, res) => ({
...(await providedGetLoadContext(req, res)),
i18n: res.locals.i18n,
}),
mode,
});
const defaultKnownStatics = await buildKnownStatics(
"",
`${buildDir}/${locale}-default/client`,
);
const defaultStatic = serveStatic(`${buildDir}/${locale}-default/client`, {
maxAge: 3_600_000,
});
return {
prefix: `/${locale}/`,
handler,
static: _static,
knownStatics,
defaultHandler,
defaultKnownStatics,
defaultStatic,
};
};
const buildHandlersForLocales = async (
buildDir,
locales,
{ mode, getLoadContext },
) => {
const localized = [];
const defaults = {};
for (const locale of locales) {
const {
prefix,
handler,
static: _static,
knownStatics,
defaultHandler,
defaultKnownStatics,
defaultStatic,
} = await buildHandlersForLocale(buildDir, locale, {
mode,
getLoadContext,
});
localized.push({ prefix, handler, static: _static, knownStatics });
defaults[locale] = {
handler: defaultHandler,
static: defaultStatic,
knownStatics: defaultKnownStatics,
};
}
const redirectPrefixes = new Set([...locales].map((l) => `/${l}`));
for (const [loc, { handler }] of Object.entries(defaults)) {
defaults[loc].handler = (req, res) => {
if (redirectPrefixes.has(req.originalUrl)) {
res.redirect(`${req.originalUrl}/`);
} else {
handler(req, res);
}
};
}
return {
localized,
defaults: defaults,
availableLocales: [...Object.keys(defaults)],
};
};
export const createI18nRemixHandler = async (
buildDir,
{ checkHeader, fallbackLocale, getUserPreference, getAllowedLocales },
{ mode, getLoadContext },
) => {
const builds = new Set(await fs.readdir(buildDir));
const nonDefaultBuilds = new Set(
[...builds].filter((b) => b.split("-").length === 1),
);
for (const ndb of nonDefaultBuilds) {
if (!builds.has(`${ndb}-default`)) {
throw new Error(
`Invalid build. For "${ndb}" no "${ndb}-default" was found.`,
);
}
}
const { localized, defaults, availableLocales } =
await buildHandlersForLocales(buildDir, nonDefaultBuilds, {
mode,
getLoadContext,
});
return async (req, res, next) => {
const _fallbackLocale = await fallbackLocale(req, res);
const availableLocalesLookup = new Set(availableLocales);
const scopedAllowedLocales = [...new Set([...(await getAllowedLocales(req, res)), _fallbackLocale])];
const allowedLocales = scopedAllowedLocales || [...Object.keys(defaults)]
const validAllowedLocales = allowedLocales.filter((l) => availableLocalesLookup.has(l));
const validAllowedLocalesLookup = new Set(validAllowedLocales);
let validHeaderPreference;
if (await checkHeader(req, res)) {
const headerPreference = req.acceptsLanguages([...Object.keys(defaults)]);
validHeaderPreference = validAllowedLocalesLookup.has(headerPreference) ? headerPreference : null;
} else {
validHeaderPreference = null;
}
const userPreference = await getUserPreference(req, res);
const validUserPreference = validAllowedLocalesLookup.has(userPreference)
? userPreference
: null;
for (const loc of localized) {
if (validAllowedLocales.length === 0 || validAllowedLocales.length == 1) {
// if there is only fallback locale or one allowed locale do not handle the
// /<locale>/... paths
break;
}
if (req.originalUrl.startsWith(loc.prefix)) {
console.log(req.originalUrl, loc.prefix);
const { handler, knownStatics, static: _static } = loc;
res.locals.build = loc.prefix.replace(/\//g, "");
if (knownStatics.has(req.originalUrl)) {
res.locals.realUrl = req.originalUrl;
Object.defineProperty(req, "originalUrl", {
value: req.originalUrl.replace(loc.prefix, "/"),
});
req.url = req.originalUrl;
_static(req, res, next);
} else {
res.locals.i18n = {
availableLocales: validAllowedLocales.length === 0 ? [_fallbackLocale] : validAllowedLocales,
fallbackLocale: _fallbackLocale,
locale: loc.prefix.replace(/\//g, ""),
defaultLocale:
validUserPreference || validHeaderPreference || _fallbackLocale,
explanation: "url-prefix",
userPreference,
};
handler(req, res, next);
}
return;
}
}
if (validUserPreference) {
const {
handler,
knownStatics,
static: _static,
} = defaults[validUserPreference];
res.locals.build = `${validUserPreference}-default`;
if (knownStatics.has(req.originalUrl)) {
res.locals.realUrl = req.originalUrl;
req.url = req.originalUrl;
_static(req, res, next);
} else {
res.locals.i18n = {
availableLocales: validAllowedLocales.length === 0 ? [_fallbackLocale] : validAllowedLocales,
fallbackLocale: _fallbackLocale,
locale: validUserPreference,
defaultLocale: validUserPreference,
explanation: "user-preference",
userPreference,
};
handler(req, res, next);
}
return;
}
if (validHeaderPreference) {
const {
handler,
knownStatics,
static: _static,
} = defaults[validHeaderPreference];
res.locals.build = `${validHeaderPreference}-default`;
if (knownStatics.has(req.originalUrl)) {
res.locals.realUrl = req.originalUrl;
req.url = req.originalUrl;
_static(req, res, next);
} else {
res.locals.i18n = {
availableLocales: validAllowedLocales.length === 0 ? [_fallbackLocale] : validAllowedLocales,
fallbackLocale: _fallbackLocale,
locale: validHeaderPreference,
defaultLocale: validHeaderPreference,
explanation: "accept-language-header",
userPreference,
};
handler(req, res, next);
}
return;
}
const {
handler,
knownStatics,
static: _static,
} = defaults[_fallbackLocale];
res.locals.build = `${_fallbackLocale}-default`;
if (knownStatics.has(req.originalUrl)) {
res.locals.realUrl = req.originalUrl;
req.url = req.originalUrl;
_static(req, res, next);
} else {
res.locals.i18n = {
availableLocales: validAllowedLocales.length === 0 ? [_fallbackLocale] : validAllowedLocales,
fallbackLocale: _fallbackLocale,
locale: _fallbackLocale,
defaultLocale: _fallbackLocale,
explanation: "fallback-locale",
userPreference,
};
handler(req, res, next);
}
return;
};
};