Skip to content

Commit

Permalink
Merge pull request #854 from geoadmin/bug-PB-530-print-date
Browse files Browse the repository at this point in the history
PB-530: Added print date to the print API spec
  • Loading branch information
ltshb authored May 23, 2024
2 parents 6b9e874 + 1c0ee7b commit 756fe37
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/api/print.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,14 @@ async function transformOlMapToPrintParams(olMap, config) {
},
})
}

const now = i18n.global.d(new Date(), 'datetime', i18n.global.locale)
const spec = {
attributes: {
map: encodedMap,
copyright: attributionsOneLine,
url: shortLink,
qrimage: qrCodeUrl,
printDate: now,
},
format: 'pdf',
layout: layout.name,
Expand Down
27 changes: 26 additions & 1 deletion src/modules/i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,43 @@ import rm from './locales/rm.json'

export const languages = { de, fr, it, en, rm }

const locales = Object.entries(languages).reduce((obj, entry) => {
const key = langToLocal(entry[0])
obj[key] = entry[1]
return obj
}, {})

export function langToLocal(lang) {
return ['de', 'fr', 'it', 'rm'].includes(lang) ? `${lang}-CH` : lang
}

// detecting navigator's locale as the default language
// (if it is a language served by this app)
let matchedLanguage = null
if (navigator.languages) {
// we keep the first match we found
matchedLanguage = navigator.languages.find((lang) => lang in languages)
matchedLanguage = navigator.languages.find((lang) => Object.keys(locales).includes(lang))
}

const datetimeFormats = Object.keys(locales).reduce((obj, key) => {
obj[key] = {
date: { year: 'numeric', month: 'numeric', day: 'numeric' },
datetime: {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
},
}
return obj
}, {})

const i18n = createI18n({
locale: matchedLanguage || 'en', // default locale
messages: languages,
legacy: false,
datetimeFormats,
})

export default i18n
4 changes: 2 additions & 2 deletions src/store/modules/i18n.store.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import i18n from '@/modules/i18n'
import i18n, { langToLocal } from '@/modules/i18n'

/**
* The name of the mutation for lang changes
Expand Down Expand Up @@ -29,7 +29,7 @@ const mutations = {}

mutations[SET_LANG_MUTATION_KEY] = function (state, { lang }) {
state.lang = lang
i18n.global.locale = lang
i18n.global.locale = langToLocal(lang)
}

export default {
Expand Down

0 comments on commit 756fe37

Please sign in to comment.