forked from GoogleChrome/web.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale-handler.js
82 lines (75 loc) · 2.75 KB
/
locale-handler.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
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const locale = require('./shared/locale');
const indexJsonRegExp = /\/index\.json$/;
/**
* A handler that redirects the request based on requested locale,
* according to priority:
* 1. Locale directly expressed in the url, e.g. /en/some-post
* 2. Locale in accept-language HTTP header.
* 3. Locale in "preferred_locale" cookie.
* If requested locale's content does not exist, falls back to default.
* @param {Object} req Request object.
* @param {Object} res Response object.
* @param {Function} next Middleware next handler.
* @return {!Function}
*/
module.exports = (req, res, next) => {
const isNav = req.url.endsWith('/');
const isJson = req.url.endsWith('/index.json');
// Exit early if the url is not navigational.
if (!isNav && !isJson) {
return next();
}
const fileType = isJson ? 'index.json' : 'index.html';
const normalizedPath = req.path.replace(indexJsonRegExp, '');
const pathParts = normalizedPath.split('/');
const isLangInPath = locale.isSupportedLocale(pathParts[1]);
let lang;
let filePath;
// Check if language is specified in the url.
if (isLangInPath) {
lang = pathParts[1];
pathParts.splice(1, 1);
pathParts.push(fileType);
filePath = pathParts.join('/');
} else {
const langInCookie = locale.isSupportedLocale(req.cookies.preferred_lang);
// If language not in url, use accept-language header.
lang = langInCookie
? req.cookies.preferred_lang
: req.acceptsLanguages(locale.supportedLocales) || locale.defaultLocale;
filePath = path.join(normalizedPath, fileType);
}
if (lang === locale.defaultLocale) {
// Redirect to a canonical url, if needed.
const shouldRedirect = isLangInPath && !isJson;
return shouldRedirect ? res.redirect(path.join('/', filePath)) : next();
}
const localizedFilePath = path.join(
__dirname,
'dist', // Must serve from dist directory even in dev mode.
lang,
filePath,
);
if (fs.existsSync(localizedFilePath)) {
return isLangInPath ? next() : res.redirect(path.join('/', lang, filePath));
} else {
return isLangInPath ? res.redirect(path.join('/', filePath)) : next();
}
};