langs.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const supportLangs = [
  2. {
  3. name: "English",
  4. value: "en-US",
  5. icon: "🇺🇸",
  6. },
  7. {
  8. name: "فارسی",
  9. value: "fa-IR",
  10. icon: "🇮🇷",
  11. },
  12. {
  13. name: "简体中文",
  14. value: "zh-CN",
  15. icon: "🇨🇳",
  16. },
  17. {
  18. name: "繁體中文",
  19. value: "zh-TW",
  20. icon: "🇹🇼",
  21. },
  22. {
  23. name: "Русский",
  24. value: "ru-RU",
  25. icon: "🇷🇺",
  26. },
  27. {
  28. name: "Tiếng Việt",
  29. value: "vi-VN",
  30. icon: "🇻🇳",
  31. },
  32. {
  33. name: "Español",
  34. value: "es-ES",
  35. icon: "🇪🇸",
  36. },
  37. {
  38. name: "Indonesian",
  39. value: "id-ID",
  40. icon: "🇮🇩",
  41. },
  42. {
  43. name: "Український",
  44. value: "uk-UA",
  45. icon: "🇺🇦",
  46. },
  47. {
  48. name: "Türkçe",
  49. value: "tr-TR",
  50. icon: "🇹🇷",
  51. },
  52. {
  53. name: "Português",
  54. value: "pt-BR",
  55. icon: "🇧🇷",
  56. },
  57. ];
  58. function getLang() {
  59. let lang = getCookie("lang");
  60. if (!lang) {
  61. if (window.navigator) {
  62. lang = window.navigator.language || window.navigator.userLanguage;
  63. if (isSupportLang(lang)) {
  64. setCookie("lang", lang, 150);
  65. } else {
  66. setCookie("lang", "en-US", 150);
  67. window.location.reload();
  68. }
  69. } else {
  70. setCookie("lang", "en-US", 150);
  71. window.location.reload();
  72. }
  73. }
  74. return lang;
  75. }
  76. function setLang(lang) {
  77. if (!isSupportLang(lang)) {
  78. lang = "en-US";
  79. }
  80. setCookie("lang", lang, 150);
  81. window.location.reload();
  82. }
  83. function isSupportLang(lang) {
  84. for (l of supportLangs) {
  85. if (l.value === lang) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }