From d07b398613ad0aff5299e6e82cfb205b04d8c6e6 Mon Sep 17 00:00:00 2001 From: ThereseRingPersen Date: Mon, 6 Nov 2023 14:21:47 +0100 Subject: [PATCH] KURSP-925 remove indirect imports of hbs files into vue components, as this breaks the build of storybook --- src/vue/utils/lang-utils.js | 2 +- src/vue/utils/previous-lang-utils.js | 142 +++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/vue/utils/previous-lang-utils.js diff --git a/src/vue/utils/lang-utils.js b/src/vue/utils/lang-utils.js index 46d91f27..78f30e06 100644 --- a/src/vue/utils/lang-utils.js +++ b/src/vue/utils/lang-utils.js @@ -1,4 +1,4 @@ -import multilanguage from "../../js/3party/multilanguage"; +import multilanguage from "./previous-lang-utils"; /** * Cleans a string to extract content based on a specified language code. * @param {string} label - The input string containing language-specific content. diff --git a/src/vue/utils/previous-lang-utils.js b/src/vue/utils/previous-lang-utils.js new file mode 100644 index 00000000..e58dabd4 --- /dev/null +++ b/src/vue/utils/previous-lang-utils.js @@ -0,0 +1,142 @@ + +// Utility class to keep utility functions out of global scope +class MultilangUtils { + + static get LANGUAGES() { + return [ + { code: 'nb', name: "Bokmål" }, + { code: 'se', name: "Sápmi" }, + { code: 'nn', name: "Nynorsk" } + ] + } + + static get COOKIE_NAME() { + return 'lang' + } + + static languagesExcept(language) { + if (util.isSamiskCourse(util.course)) { + var languages = [ + { code: 'nb', name: "Bokmål" }, + { code: 'se', name: "Sápmi" }, + ] + return languages.filter(lang => lang.code !== language) + } + else if (util.isNynorskCourse(util.course)) { + var languages = [ + { code: 'nb', name: "Bokmål" }, + { code: 'nn', name: "Nynorsk" } + ] + return languages.filter(lang => lang.code !== language) + } + return MultilangUtils.LANGUAGES.filter(lang => lang.code !== language) + } + + static getLanguageParameter() { + const params = new URLSearchParams(location.search) + return params.get(this.COOKIE_NAME) + } + + static setLanguageParameter(languageCode) { + if (!this.isValidLanguage(languageCode)) { + return + } + + const params = new URLSearchParams(location.search) + params.set(this.COOKIE_NAME, languageCode) + window.history.replaceState({}, '', `${location.pathname}?${params}`) + } + + static getLanguageCookie() { + return document.cookie.replace(/(?:(?:^|.*;\s*)courselanguage\s*\=\s*([^;]*).*$)|^.*$/, "$1") + } + + static setLanguageCookie(languageCode) { + if (!this.isValidLanguage(languageCode)) { + return + } + + document.cookie = `courselanguage=${languageCode}; SameSite=Strict; path=/` + } + + static getLanguageCode() { + const langCode = MultilangUtils.getLanguageParameter() + const langCookie = MultilangUtils.getLanguageCookie() + if (langCode) { + return langCode + } else if (langCookie) { + return langCookie + } + else { + return 'nb' + } + } + static getPreferredLanguage() { + if (!util.isMultilangCourse) { + return api.getLocale() + } + if (MultilangUtils.getLanguageCode == "nb") { + return api.getLocale() + } + return MultilangUtils.getLanguageCode() + } + + static setActiveLanguage(activeLang) { + if (!this.isValidLanguage(activeLang)) { + return + } + + MultilangUtils.setLanguageCookie(activeLang) + MultilangUtils.setLanguageParameter(activeLang) + const styleElement = document.getElementById('language-style') + styleElement.innerHTML = MultilangUtils.createCss(activeLang) + } + + static isValidLanguage(languageCode) { + if (util.isSamiskCourse(util.course)) { + var languages = [ + { code: 'nb', name: "Bokmål" }, + { code: 'se', name: "Sápmi" }, + ] + } + else if (util.isNynorskCourse(util.course)) { + var languages = [ + { code: 'nb', name: "Bokmål" }, + { code: 'nn', name: "Nynorsk" } + ] + } + else { + var languages = this.LANGUAGES + } + return languages.some(lang => lang.code === languageCode) + } +} + +export default (function () { + return { + getLanguageCode: () => { + return MultilangUtils.getLanguageCode() + }, + setActiveLanguage: (langCode) => { + MultilangUtils.setActiveLanguage(langCode) + }, + languagesMap: () => { + return MultilangUtils.languagesMap() + }, + languagesExcept: (selectedLang) => { + return MultilangUtils.languagesExcept(selectedLang) + }, + getLanguageParameter: () => { + return MultilangUtils.getLanguageParameter() + }, + isValidLanguage: (language) => { + return MultilangUtils.isValidLanguage(language) + }, + languagesMap: () => { + return MultilangUtils.languagesMap() + }, + getPreferredLanguage: () => { + return MultilangUtils.getPreferredLanguage() + } + } +})();