Translator.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Copyright (C) 2013 KO GmbH <copyright@kogmbh.com>
  3. *
  4. * @licstart
  5. * This file is part of WebODF.
  6. *
  7. * WebODF is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU Affero General Public License (GNU AGPL)
  9. * as published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * WebODF is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with WebODF. If not, see <http://www.gnu.org/licenses/>.
  19. * @licend
  20. *
  21. * @source: http://www.webodf.org/
  22. * @source: https://github.com/kogmbh/WebODF/
  23. */
  24. /*global define, runtime, XMLHttpRequest */
  25. define("webodf/editor/Translator", [], function () {
  26. "use strict";
  27. return function Translator(translationsPath, locale, callback) {
  28. var self = this,
  29. dictionary = {};
  30. function translate(key) {
  31. return dictionary[key];
  32. }
  33. function setLocale(newLocale, cb) {
  34. // TODO: Add smarter locale resolution at some point
  35. if (newLocale.split('-')[0] === "de" || newLocale.split('_')[0] === "de") {
  36. newLocale = "de-DE";
  37. } else if (newLocale.split('-')[0] === "nl" || newLocale.split('_')[0] === "nl") {
  38. newLocale = "nl-NL";
  39. } else if (newLocale.split('-')[0] === "fr" || newLocale.split('_')[0] === "fr") {
  40. newLocale = "fr-FR";
  41. } else if (newLocale.split('-')[0] === "it" || newLocale.split('_')[0] === "it") {
  42. newLocale = "it-IT";
  43. } else if (newLocale.split('-')[0] === "eu" || newLocale.split('_')[0] === "eu") {
  44. newLocale = "eu";
  45. } else if (newLocale.split('-')[0] === "en" || newLocale.split('_')[0] === "en") {
  46. newLocale = "en-US";
  47. } else {
  48. newLocale = "en-US";
  49. }
  50. var xhr = new XMLHttpRequest(),
  51. path = translationsPath + '/' + newLocale + ".json";
  52. xhr.open("GET", path);
  53. xhr.onload = function () {
  54. if (xhr.status === 200) {// HTTP OK
  55. dictionary = JSON.parse(xhr.response);
  56. locale = newLocale;
  57. }
  58. cb();
  59. };
  60. xhr.send(null);
  61. }
  62. function getLocale() {
  63. return locale;
  64. }
  65. this.translate = translate;
  66. this.getLocale = getLocale;
  67. function init() {
  68. setLocale(locale, function () {
  69. callback(self);
  70. });
  71. }
  72. init();
  73. };
  74. });