Skip to content

Commit

Permalink
fix: set lang attribute based on URL param
Browse files Browse the repository at this point in the history
  • Loading branch information
aqandrew committed Jan 5, 2021
1 parent ce51319 commit b3f8636
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/lib/strings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const DEFAULT_LANG = 'en'

export const strings = {
en: {
APP_TITLE: 'HTTP Cats',
Expand All @@ -7,7 +9,7 @@ export const strings = {
USAGE_NOTE_TEXT: 'If you need an extension at the end of the URL just add',
LANGUAGE_LINK_TEXT: 'Versió Català',
DEVELOPED_BY: 'Developed by',
IMAGES_BY: 'Images by'
IMAGES_BY: 'Images by',
},

cat: {
Expand All @@ -19,12 +21,19 @@ export const strings = {
'Si necessiteu una extensió al final de l’URL només heu d’afegir',
LANGUAGE_LINK_TEXT: 'Versió Català',
DEVELOPED_BY: 'Desenvolupat per',
IMAGES_BY: 'Imatges de'
}
IMAGES_BY: 'Imatges de',
},
}

const getString = label => {
const lang = window.location.search.indexOf('lang=cat') !== -1 ? 'cat' : 'en'
const getString = (label) => {
const urlParams = new URLSearchParams(window.location.search)
const langParam = urlParams.get('lang')
const isKnownLang = langParam in strings
const lang = isKnownLang ? langParam : DEFAULT_LANG

if (isKnownLang && document.documentElement.getAttribute('lang') !== lang) {
document.documentElement.setAttribute('lang', lang)
}

return strings[lang][label]
}
Expand Down

0 comments on commit b3f8636

Please sign in to comment.