language.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class Lang {
  2. constructor() {
  3. console.log("lang constructor");
  4. this.polyglot = Polyglot;
  5. if (localStorage.getItem('krestianstvo_locale')) {
  6. } else {
  7. localStorage.setItem('krestianstvo_locale', 'en');
  8. }
  9. //this.locale = localStorage.getItem('krestianstvo_locale');
  10. //this.setLanguage(this.locale);
  11. }
  12. async getLang(langID) {
  13. let response = await fetch("/lib/locale/"+ langID + '/' + langID + ".json");
  14. let data = await response.json();
  15. return data
  16. }
  17. async setLanguage(langID) {
  18. var currentLang = localStorage.getItem('krestianstvo_locale');
  19. if (langID) {
  20. currentLang = langID
  21. }
  22. let phrases = await this.getLang(currentLang);
  23. this.language = new Polyglot({ phrases });
  24. // this.getLang(langID).then(phrases => {
  25. // this.language = new polyglot({ phrases });
  26. // return phrases
  27. // });
  28. }
  29. get locale() {
  30. return localStorage.getItem('krestianstvo_locale')
  31. }
  32. set locale(langID){
  33. localStorage.setItem('krestianstvo_locale', langID);
  34. }
  35. // setLocale(langID){
  36. // localStorage.setItem('krestianstvo_locale', langID);
  37. // this.locale = langID;
  38. // }
  39. async changeLanguageTo(langID){
  40. this.setLocale(langID);
  41. await this.setLanguage(langID);
  42. }
  43. }
  44. export {Lang as default}