forked from ppfun/pixelplanet
add stump for getting accept-language header
This commit is contained in:
parent
b4d19a2833
commit
3df0bcae1c
|
@ -38,9 +38,10 @@ const css = [
|
|||
* including setting global variables for countryCoords
|
||||
* and assetserver
|
||||
* @param countryCoords Cell with coordinates of client country
|
||||
* @param lang language code
|
||||
* @return html of mainpage
|
||||
*/
|
||||
function generateMainPage(countryCoords: Cell): string {
|
||||
function generateMainPage(countryCoords: Cell, lang: string): string {
|
||||
const [x, y] = countryCoords;
|
||||
// eslint-disable-next-line
|
||||
const html = ReactDOM.renderToStaticMarkup(
|
||||
|
|
|
@ -20,4 +20,26 @@ export function ccToCoords(cc: string) {
|
|||
return (coords) || [0, 0];
|
||||
}
|
||||
|
||||
/*
|
||||
* gets prefered language out of localisation string
|
||||
* @param location string (like from accept-language header)
|
||||
* @return language code
|
||||
*/
|
||||
export function languageFromLocalisation(localisation) {
|
||||
let lang = localisation;
|
||||
let i = lang.indexOf('-');
|
||||
if (i !== -1) {
|
||||
lang = lang.slice(0, i);
|
||||
}
|
||||
i = lang.indexOf(',');
|
||||
if (i !== -1) {
|
||||
lang = lang.slice(0, i);
|
||||
}
|
||||
i = lang.indexOf(';');
|
||||
if (i !== -1) {
|
||||
lang = lang.slice(0, i);
|
||||
}
|
||||
return lang;
|
||||
}
|
||||
|
||||
export default ccToCoords;
|
||||
|
|
|
@ -32,7 +32,7 @@ import generateMainPage from './components/Main';
|
|||
import { SECOND, MONTH } from './core/constants';
|
||||
import { PORT, DISCORD_INVITE, GUILDED_INVITE } from './core/config';
|
||||
|
||||
import { ccToCoords } from './utils/location';
|
||||
import { ccToCoords, languageFromLocalisation } from './utils/location';
|
||||
import { startAllCanvasLoops } from './core/tileserver';
|
||||
|
||||
startAllCanvasLoops();
|
||||
|
@ -183,9 +183,12 @@ app.get('/', async (req, res) => {
|
|||
|
||||
// get start coordinates based on cloudflare header country
|
||||
const country = req.headers['cf-ipcountry'];
|
||||
let language = req.headers['accept-language'];
|
||||
|
||||
const lang = (language) ? languageFromLocalisation(language) : 'en';
|
||||
const countryCoords = (country) ? ccToCoords(country) : [0, 0];
|
||||
|
||||
res.status(200).send(generateMainPage(countryCoords));
|
||||
res.status(200).send(generateMainPage(countryCoords, lang));
|
||||
});
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user