home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 220 / 220.xpi / chrome / flashgot.jar / content / flashgot / Strings.js < prev    next >
Encoding:
JavaScript  |  2010-01-24  |  2.7 KB  |  83 lines

  1. /***** BEGIN LICENSE BLOCK *****
  2.  
  3.     FlashGot - a Firefox extension for external download managers integration
  4.     Copyright (C) 2004-2009 Giorgio Maone - g.maone@informaction.com
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.                              
  20. ***** END LICENSE BLOCK *****/
  21.  
  22. function Strings(chromeName) {
  23.   this.chromeName = chromeName;
  24. }
  25.  
  26. Strings.wrap = function(s, length, sep) {
  27.   if (!sep) sep = ' ';
  28.     
  29.   function wrapPara(p) {
  30.     if (!length) length = 80;
  31.     if (p.length <= length) return p;
  32.     chunks = [];
  33.     var pos;
  34.     while (p.length > length) {
  35.       pos = p.lastIndexOf(sep, length);
  36.       if (pos < 0) pos = p.indexOf(sep, length);
  37.       if (pos < 0) break;
  38.       chunks.push(p.substring(0, pos));
  39.       p = p.substring(pos + 1);
  40.     }
  41.  
  42.     if (chunks.length) {
  43.       res  = chunks.join("\n");
  44.       if (p.length) res += "\n" + p;
  45.       return res;
  46.     } else return p;
  47.   }
  48.   if (typeof(s) != "string") s = s.toString();
  49.   var paras = s.split("\n");
  50.   
  51.   for (var j = 0; j < paras.length; j++) paras[j] = wrapPara(paras[j]);
  52.   return paras.join("\n");
  53. }
  54.  
  55. Strings.prototype = {
  56.   bundles: {},
  57.   getBundle: function(path) {
  58.     if (path in this.bundles) return this.bundles[path];
  59.     try {
  60.       return this.bundles[path] = 
  61.         CC["@mozilla.org/intl/stringbundle;1"]
  62.                   .getService(CI.nsIStringBundleService)
  63.                   .createBundle("chrome://" + this.chromeName +  "/" + path +
  64.                                 "/" + this.chromeName + ".properties");
  65.     } catch(ex) {
  66.       return this.bundles[path] = null;
  67.     }
  68.   },
  69.   
  70.   _stringFrom: function(bundle, name, parms) {
  71.     try {
  72.       return parms ? bundle.formatStringFromName(name, parms, parms.length) : bundle.GetStringFromName(name);
  73.     } catch(ex) {
  74.       return null;
  75.     }
  76.   }
  77. ,
  78.   getString: function(name, parms) {
  79.     var s=this._stringFrom(this.getBundle("locale"), name, parms);
  80.     return s || this._stringFrom(this.getBundle("content/en-US"), name, parms) || name;
  81.   }
  82.   
  83. };