From 5a0fbe1badf615364bbcffe937fa5d74c593d7b3 Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Mon, 14 May 2018 10:47:20 +0200 Subject: [PATCH] :bug: fix invalid language tag when passing locale argument to iframe --- source/i18n.js | 5 ++--- source/utils.js | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/source/i18n.js b/source/i18n.js index 8a3b6b3ad..d9f9907d5 100644 --- a/source/i18n.js +++ b/source/i18n.js @@ -1,14 +1,13 @@ -import { I18nextProvider } from 'react-i18next' import i18next from 'i18next' import queryString from 'query-string' -import { getIframeOption } from './utils' +import { getIframeOption, parseDataAttributes } from './utils' import enTranslations from './locales/en.yaml' let lang = getIframeOption('lang') || queryString.parse(location.search)['lang'] || - sessionStorage['lang'] || + parseDataAttributes(sessionStorage['lang']) || 'fr' sessionStorage['lang'] = lang diff --git a/source/utils.js b/source/utils.js index 8781c7c88..cdb1cb8c3 100644 --- a/source/utils.js +++ b/source/utils.js @@ -1,8 +1,21 @@ export let capitalise0 = name => name[0].toUpperCase() + name.slice(1) export let getUrl = () => window.location.href.toString() + +export let parseDataAttributes = value => + value === 'undefined' + ? undefined + : value === null + ? null + : !isNaN(value) + ? +value + : /* value is a normal string */ + value + export let getIframeOption = optionName => { let url = getUrl(), hasOption = url.includes(optionName + '=') - return hasOption && url.split(optionName + '=')[1].split('&')[0] + return parseDataAttributes( + hasOption && url.split(optionName + '=')[1].split('&')[0] + ) }