docs(site): i18n handler, prettier code blocks and highlighting (#951)

* fix: sirv, commonjs plugin, add rollup sig

* replace deprecated rollup plugins

* remove dollarsign

* fix i18n setting, code snippet styling, lng switch

* fix segment, rm unused styles
This commit is contained in:
Max Peintner
2020-11-11 16:12:09 +01:00
committed by GitHub
parent 8dbf0cfee0
commit b5a7263124
8 changed files with 39 additions and 22 deletions
+15 -3
View File
@@ -1,5 +1,6 @@
import { getLocaleFromNavigator, init, locale as $locale, register } from 'svelte-i18n';
import { LANGUAGES } from '../config.js';
import { getCookie, setCookie } from './modules/cookie.js';
export const INIT_OPTIONS = {
@@ -8,6 +9,7 @@ export const INIT_OPTIONS = {
loadingDelay: 200,
formats: {},
warnOnMissingMessages: true,
localeOptions: LANGUAGES,
};
let currentLocale = null;
@@ -28,9 +30,10 @@ $locale.subscribe((value) => {
// initialize the i18n library in client
export function startClient() {
console.log('nav', getLocaleFromNavigator());
init({
...INIT_OPTIONS,
initialLocale: getCookie('locale') || getLocaleFromNavigator(),
initialLocale: getCookie('locale') || INIT_OPTIONS.localeOptions.find(option => option == cropCountryCode(getLocaleFromNavigator())) || INIT_OPTIONS.initialLocale,
});
}
@@ -53,8 +56,13 @@ export function i18nMiddleware() {
// no cookie, let's get the first accepted language
if (locale == null) {
if (req.headers['accept-language']) {
const headerLang = req.headers['accept-language'].split(',')[0].trim();
if (headerLang.length > 1) {
const headerLngs = req.headers['accept-language'].split(',');
const headerLngCodes = headerLngs.map(lng => lng.split(';')[0].trim());
const headerLang = headerLngCodes.find(code => {
return INIT_OPTIONS.localeOptions.find(option => option == code);
});
if (headerLang) {
locale = headerLang;
}
} else {
@@ -68,4 +76,8 @@ export function i18nMiddleware() {
next();
};
}
function cropCountryCode(code) {
return code.split('-')[0].trim();
}