Skip to content

Commit

Permalink
Removed change in language dir based on browser. Now solely relying o…
Browse files Browse the repository at this point in the history
…n translation
  • Loading branch information
henrykaus committed Oct 19, 2024
1 parent ea50f9e commit d78f920
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions scripts/language-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@ const RTL_LANGUAGES = ['ar', 'fa', 'he', 'iw', 'kd', 'pk', 'ps', 'ug', 'ur', 'yi
/**
* Gets the language direction given a language code.
*
* @param {string} language
* @param {string} languageCode
* @returns 'rtl' or 'ltr'
*/
const getLangDirection = (language) => {
const langAbbrev = language.split('-', 1).at(0).toLowerCase();
const getLangDirection = (languageCode) => {
const langAbbrev = languageCode.split('-', 1).at(0).toLowerCase();
return RTL_LANGUAGES.includes(langAbbrev) ? 'rtl' : 'ltr';
}

/**
* Set the <html> dir attribute based on the browser or translation language.
* Set the <html> dir attribute based on the translation language.
*/
const setLangDirection = (event) => {
const browserLang = window.navigator.language;
const setLangDirection = () => {
const htmlLang = document.querySelector('html').lang;

const browserLangDir = getLangDirection(browserLang);
const htmlLangDir = getLangDirection(htmlLang);

const dir = (browserLangDir === 'rtl' || htmlLangDir === 'rtl') ? 'rtl' : 'ltr';
const dir = getLangDirection(htmlLang);

document.querySelector('html').setAttribute('dir', dir);
}
Expand All @@ -41,13 +36,13 @@ const langMutationCallback = (mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'lang') {
setLangDirection();
break;
}
}
}

// ACTIONS THAT SHOULD HAPPEN PRIOR TO DOCUMENT READY
setLangDirection();
window.addEventListener('languagechange', setLangDirection);

const langObserver = new MutationObserver(langMutationCallback)
langObserver.observe(document.querySelector('html'), { attributes: true });

0 comments on commit d78f920

Please sign in to comment.