From 9983f77c4af3b08b9f0ffd2041a8809e9652796a Mon Sep 17 00:00:00 2001 From: hellekin Date: Sat, 6 Jan 2018 11:44:22 +0100 Subject: Add cookie for lang --- assets/js/ps-app.js | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'assets/js') diff --git a/assets/js/ps-app.js b/assets/js/ps-app.js index 33f4f99..4903ab2 100644 --- a/assets/js/ps-app.js +++ b/assets/js/ps-app.js @@ -35,20 +35,53 @@ // Use ?lang=XX if available var URLParams = new URLSearchParams(window.location.search); if (URLParams.has('lang')) { + console.log('Setting lang from param to ' + URLParams.get('lang')) setLang(URLParams.get('lang')) + } else if (C.get('lang')) { + console.log('Setting lang to ' + $.cookie('lang')) + setLang(C.get('lang')) } // Translate to target lang function setLang(lang) { if (lang == 'fr') { - $('html').attr('lang', 'fr') + $('html').attr('lang', lang) $('#lang-switch').html('EN').attr('title', 'Read in English') } else { - $('html').attr('lang', 'en') + lang = 'en' + $('html').attr('lang', lang) $('#lang-switch').html('FR').attr('title', 'Lire en français') } + C.set('lang', lang) } + }); + // Basic cookie functions + var C = { + set: function(name, value) { + // Make the cookie expire at the end of session + var cookie = name + '=' + encodeURIComponent(value) + ';path=/;domain=.lesoiseaux.io;secure' + document.cookie = cookie + return cookie + }, + get: function(name) { + try { + var val = document.cookie.split(';')[0] + var key = name + '=' + return (val.indexOf(key) == 0) ? decodeURIComponent(val.substr(key.length)) : null + } catch(e) { + return null + } + }, + has: function(name) { + try { + return (document.cookie.split(';')[0].indexOf(name + '=') == 0) + } catch(e) { + return false + } + } + } + })($) -- cgit v1.2.3