Last active
October 18, 2021 14:45
-
-
Save Renik07/74125c1274cc2974f9729cbda7a34b62 to your computer and use it in GitHub Desktop.
Multilanguages site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2 | |
export default { | |
'cat': 'Cat', | |
'bird': 'Bird', | |
'dog': 'Dog', | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ru"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
// 1 | |
<div data-lang="cat">Кот</div> | |
<div data-lang="bird">Птица</div> | |
<div data-lang="dog">Собака</div> | |
<script src="eng.js" type="module"></script> | |
<script src="it.js" type="module"></script> | |
<script src="script.js" type="module"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2 | |
export default { | |
'cat': 'Gatto', | |
'bird': 'Uccello', | |
'dog': 'Cane', | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import eng from './eng.js'; | |
import it from './it.js'; | |
let userLang = window.navigator.language; // 3 | |
let lang; | |
let dataElements = document.querySelectorAll('*[data-lang]'); | |
// 4 | |
if (userLang === 'eng' || userLang === 'en') { | |
lang = eng; | |
} else if (userLang === 'it' || userLang === 'ita') { | |
lang = it; | |
} | |
// 5 | |
for (let dataElem of dataElements) { | |
dataElem.innerHTML = lang[dataElem.dataset.lang]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Для каждого элемента сайта прописываем дата-атрибут | |
2) Для каждого языка создаем отдельный js-модуль | |
3) Определяем язык браузера пользователя | |
4) Пишем условие if опираясь на ISO 639-1 codes | |
5) Находим все элементы с дата и подставляем им значения с js-модуля |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment