moe-localize.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * This file is part of Moeditor.
  3. *
  4. * Copyright (c) 2016 Menci <huanghaorui301@gmail.com>
  5. * Copyright (c) 2016 lucaschimweg
  6. *
  7. * Moeditor is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Moeditor is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Moeditor. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. if (typeof window.localized === 'undefined') window.localized = [];
  21. document.querySelector('html').setAttribute('lang', moeApp.locale.locale);
  22. window.__ = moeApp.locale.get;
  23. function localize() {
  24. let elements;
  25. elements = document.getElementsByClassName('l10n') || [];
  26. for (let e of elements) {
  27. let text = e.getAttribute('data-origin-text');
  28. if (!text) {
  29. if (e.tagName.toUpperCase() === 'OPTION') {
  30. text = e.text;
  31. } else {
  32. text = e.innerText;
  33. }
  34. e.setAttribute('data-origin-text', text);
  35. }
  36. text = __(text) || text;
  37. if (e.tagName.toUpperCase() === 'OPTION') {
  38. e.text = text;
  39. } else {
  40. e.innerText = text;
  41. }
  42. }
  43. elements = document.getElementsByClassName('l10n-title') || [];
  44. for (let e of elements) {
  45. let title = e.getAttribute('data-origin-title');
  46. if (!title) {
  47. title = e.getAttribute('title');
  48. e.setAttribute('data-origin-title', title);
  49. }
  50. let exdata = e.getAttribute('exdata');
  51. title = __(title)+(exdata ? exdata : "");
  52. e.setAttribute('title', title);
  53. }
  54. if (window.localized !== []) {
  55. for (let f of window.localized) f();
  56. window.localized = [];
  57. }
  58. };
  59. window.addEventListener('DOMContentLoaded', localize);
  60. require('electron').ipcRenderer.on('setting-changed', (e, arg) => {
  61. if (arg.key === 'locale') {
  62. localize();
  63. }
  64. });