home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / gtcache.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  1.9 KB  |  63 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (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
  12. # GNU 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. # Caching gettext functions
  19.  
  20. import gettext as _gt
  21. import locale
  22. import config
  23. import prefs
  24. import platformutils
  25. import os
  26.  
  27. _gtcache = None
  28.  
  29. def init():
  30.     global _gtcache
  31.     _gtcache = {}
  32.     if not platformutils.localeInitialized:
  33.         raise Exception, "locale not initialized"
  34.     locale.setlocale(locale.LC_ALL, '')
  35.  
  36.     _gt.bindtextdomain("miro", config.get(prefs.GETTEXT_PATHNAME))
  37.     _gt.textdomain("miro")
  38.     _gt.bind_textdomain_codeset("miro","UTF-8")
  39.  
  40. def gettext(text):
  41.     text = text.encode('utf-8')
  42.     try:
  43.         return _gtcache[text]
  44.     except KeyError:
  45.         out = _gt.gettext(text).decode('utf-8')
  46.         _gtcache[text] = out
  47.         return out
  48.     except TypeError:
  49.         print "DTV: WARNING: gettext not initialized for string \"%s\"" % text
  50.         import traceback
  51.         traceback.print_stack()
  52.         return text
  53.  
  54. def ngettext(text1, text2, count):
  55.     text1 = text1.encode('utf-8')
  56.     text2 = text2.encode('utf-8')
  57.     try:
  58.         return _gtcache[(text1,text2,count)]
  59.     except:
  60.         out = _gt.ngettext(text1, text2, count).decode('utf-8')
  61.         _gtcache[(text1,text2,count)] = out
  62.         return out
  63.