Translator.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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] === "it" || newLocale.split('_')[0] === "it") {
  40. newLocale = "it-IT";
  41. } else if (newLocale.split('-')[0] === "en" || newLocale.split('_')[0] === "en") {
  42. newLocale = "en-US";
  43. } else {
  44. newLocale = "en-US";
  45. }
  46. var xhr = new XMLHttpRequest(),
  47. path = translationsPath + '/' + newLocale + ".json";
  48. xhr.open("GET", path);
  49. xhr.onload = function () {
  50. if (xhr.status === 200) {// HTTP OK
  51. dictionary = JSON.parse(xhr.response);
  52. locale = newLocale;
  53. }
  54. cb();
  55. };
  56. xhr.send(null);
  57. }
  58. function getLocale() {
  59. return locale;
  60. }
  61. this.translate = translate;
  62. this.getLocale = getLocale;
  63. function init() {
  64. setLocale(locale, function () {
  65. callback(self);
  66. });
  67. }
  68. init();
  69. };
  70. });