langs.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const supportLangs = [
  2. {
  3. name : "English",
  4. value : "en-US",
  5. icon : "🇺🇸"
  6. },
  7. {
  8. name : "Farsi",
  9. value : "fa_IR",
  10. icon : "🇮🇷"
  11. },
  12. {
  13. name : "汉语",
  14. value : "zh-Hans",
  15. icon : "🇨🇳"
  16. },
  17. {
  18. name : "Russian",
  19. value : "ru_RU",
  20. icon : "🇷🇺"
  21. },
  22. ]
  23. function getLang(){
  24. let lang = getCookie('lang')
  25. if (! lang){
  26. if (window.navigator){
  27. lang = window.navigator.language || window.navigator.userLanguage;
  28. if (isSupportLang(lang)){
  29. setCookie('lang' , lang , 150)
  30. }else{
  31. setCookie('lang' , 'en-US' , 150)
  32. window.location.reload();
  33. }
  34. }else{
  35. setCookie('lang' , 'en-US' , 150)
  36. window.location.reload();
  37. }
  38. }
  39. return lang;
  40. }
  41. function setLang(lang){
  42. if (!isSupportLang(lang)){
  43. lang = 'en-US';
  44. }
  45. setCookie('lang' , lang , 150)
  46. window.location.reload();
  47. }
  48. function isSupportLang(lang){
  49. for (l of supportLangs){
  50. if (l.value === lang){
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. function getCookie(cname) {
  57. let name = cname + "=";
  58. let decodedCookie = decodeURIComponent(document.cookie);
  59. let ca = decodedCookie.split(';');
  60. for(let i = 0; i <ca.length; i++) {
  61. let c = ca[i];
  62. while (c.charAt(0) == ' ') {
  63. c = c.substring(1);
  64. }
  65. if (c.indexOf(name) == 0) {
  66. return c.substring(name.length, c.length);
  67. }
  68. }
  69. return "";
  70. }
  71. function setCookie(cname, cvalue, exdays) {
  72. const d = new Date();
  73. d.setTime(d.getTime() + (exdays*24*60*60*1000));
  74. let expires = "expires="+ d.toUTCString();
  75. document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  76. }