home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / xbmc-9.11.exe / scripts / AppleMovieTrailers / resources / lib / language.py < prev    next >
Encoding:
Python Source  |  2008-09-14  |  2.8 KB  |  70 lines

  1. """
  2. Language module
  3.  
  4. Nuka1195
  5. """
  6.  
  7. import os
  8. import xbmc
  9. import xml.dom.minidom
  10.  
  11.  
  12. class Language:
  13.     """ Language Class: creates a dictionary of localized strings { int: string } """
  14.     DEFAULT_LANGUAGE = "English"
  15.  
  16.     def __init__( self ):
  17.         """ initializer """
  18.         # language folder
  19.         base_path = os.path.join( os.getcwd(), "resources", "language" )
  20.         # get the current language
  21.         language = self._get_language( base_path )
  22.         # create strings dictionary
  23.         self._create_localized_dict( base_path, language )
  24.         
  25.     def _get_language( self, base_path ):
  26.         """ returns the current language if a strings.xml file exists else returns default language """
  27.         # get the current users language setting
  28.         language = xbmc.getLanguage()
  29.         # if no strings.xml file exists, use default language
  30.         if ( not os.path.isfile( os.path.join( base_path, language, "strings.xml" ) ) ):
  31.             language = self.DEFAULT_LANGUAGE
  32.         return language
  33.  
  34.     def _create_localized_dict( self, base_path, language ):
  35.         """ initializes self.strings and calls _parse_strings_file """
  36.         # localized strings dictionary
  37.         self.strings = {}
  38.         # add localized strings
  39.         self._parse_strings_file( os.path.join( base_path, language, "strings.xml" ) )
  40.         # fill-in missing strings with default language strings
  41.         if ( language != self.DEFAULT_LANGUAGE ):
  42.             self._parse_strings_file( os.path.join( base_path, self.DEFAULT_LANGUAGE, "strings.xml" ) )
  43.         
  44.     def _parse_strings_file( self, language_path ):
  45.         """ adds localized strings to self.strings dictionary """
  46.         try:
  47.             # load and parse strings.xml file
  48.             doc = xml.dom.minidom.parse( language_path )
  49.             # make sure this is a valid <strings> xml file
  50.             root = doc.documentElement
  51.             if ( not root or root.tagName != "strings" ): raise
  52.             # parse and resolve each <string id="#"> tag
  53.             strings = root.getElementsByTagName( "string" )
  54.             for string in strings:
  55.                 # convert id attribute to an integer
  56.                 string_id = int( string.getAttribute( "id" ) )
  57.                 # if a valid id add it to self.strings dictionary
  58.                 if ( string_id not in self.strings and string.hasChildNodes() ):
  59.                     self.strings[ string_id ] = string.firstChild.nodeValue
  60.         except:
  61.             # print the error message to the log and debug window
  62.             xbmc.output( "ERROR: Language file %s can't be parsed!" % ( language_path, ) )
  63.         # clean-up document object
  64.         try: doc.unlink()
  65.         except: pass
  66.  
  67.     def localized( self, code ):
  68.         """ returns the localized string if it exists """
  69.         return self.strings.get( code, "Invalid Id %d" % ( code, ) )
  70.