Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the UI language cookie #1717

Merged
merged 14 commits into from
Dec 12, 2024
18 changes: 18 additions & 0 deletions resource/js/navbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function setLangCookie (lang) {
// The cookie path should be relative if the baseHref is known
const cookiePath = window.SKOSMOS.baseHref?.replace(window.origin, '') || '/'
const date = new Date()
date.setTime(date.getTime() + 365 * 24 * 60 * 60 * 1000) // 365 days from now
document.cookie = `SKOSMOS_LANGUAGE=${lang}; expires=${date.toGMTString()}; path=${cookiePath}; SameSite=Lax`
}
function addLanguageEventListeners () {
const languageLinks = document.querySelectorAll('.nav-item.language a')
languageLinks.forEach(function (link) {
link.addEventListener('click', function (event) {
const langValue = this.getAttribute('id').substring(9) // strip the 'language-' part of the id
setLangCookie(langValue)
})
})
}
// register the language link event listeners
document.addEventListener('DOMContentLoaded', addLanguageEventListeners)
4 changes: 2 additions & 2 deletions src/view/base-template.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
{% if languages|length > 1 %}
{% for langcode, langdata in languages %}
{% if request.lang != langcode %}
<li class="nav-item" id="language">
<a class="fs-6 text-light ms-3 text-decoration-none" id="language-{{ langcode}}" href="{{ request.langurl(langcode) }}"> {{ langdata.name }}</a>
<li class="nav-item language">
<a class="fs-6 text-light ms-3 text-decoration-none" id="language-{{ langcode }}" href="{{ request.langurl(langcode) }}"> {{ langdata.name }}</a>
</li>
{% endif %}
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions src/view/scripts.inc.twig
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ window.SKOSMOS = {

<!-- Other (non-Vue) JS functionality -->
<script src="resource/js/copy-to-clipboard.js"></script>
<script src="resource/js/navbar.js"></script>
16 changes: 16 additions & 0 deletions tests/cypress/template/navbar-languages.cy.js
joelit marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
describe('Navbar languages', () => {

it('Sets and reads SKOSMOS_LANGUAGE cookie correctly', () => {
cy.visit('/')
joelit marked this conversation as resolved.
Show resolved Hide resolved
cy.url().should('not.include', '/fi/') // The default language is not Finnish

cy.get('.nav-item.language a').first().click()
cy.getCookie('SKOSMOS_LANGUAGE').should('exist')
cy.getCookie('SKOSMOS_LANGUAGE').should('have.property', 'value', 'fi')

cy.url().should('include', '/fi/')

cy.visit('/')
cy.url().should('include', '/fi/') // Skosmos chooses the default language based on the cookie
})
joelit marked this conversation as resolved.
Show resolved Hide resolved
})
Loading