xweb-locale.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2009 Matt Magoffin (spamsqr@msqr.us)
  2. //
  3. // ===================================================================
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU General Public License as
  6. // published by the Free Software Foundation; either version 2 of
  7. // the License, or (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. // 02111-1307 USA
  18. // ===================================================================
  19. // $Id: matte-locale.js,v 1.8 2007/06/25 10:35:32 matt Exp $
  20. // ===================================================================
  21. var XWEB_LOCALE_DEFAULT_LANG = 'en';
  22. var XwebLocaleClass = function() {};
  23. XwebLocaleClass.prototype = {
  24. bundle: {},
  25. init: function(messages) {
  26. if ( messages ) this.bundle = messages;
  27. },
  28. /**
  29. * Initialize from DOM nodes of type <msg key='x'>value</msg>.
  30. */
  31. initXmsg: function(xMsgNodes) {
  32. if ( !(xMsgNodes && xMsgNodes.length) ) return;
  33. for ( var i = 0; i < xMsgNodes.length; i++ ) {
  34. if ( !xMsgNodes[i].hasChildNodes() ) continue;
  35. var key = xMsgNodes[i].getAttribute('key');
  36. xMsgNodes[i].normalize();
  37. var value = xMsgNodes[i].firstChild.nodeValue;
  38. this.bundle[key] = value;
  39. }
  40. },
  41. /**
  42. * Initialize from JSON object. The object key/value pairs are turned
  43. * into the message bundle data.
  44. */
  45. initJson: function(msgData) {
  46. if ( !msgData ) return;
  47. for ( var key in msgData ) {
  48. this.bundle[key] = msgData[key];
  49. }
  50. },
  51. i18n : function(key,params) {
  52. var msg = this.bundle[key];
  53. if ( !msg ) {
  54. msg = '??'+key+'??';
  55. } else if ( params ) {
  56. var i = 0;
  57. for ( i = 0; i < params.length; i++ ) {
  58. msg = msg.replace(new RegExp('\\{'+(i)+'\\}','g'),params[i]);
  59. }
  60. }
  61. return msg;
  62. }
  63. };