Ok,

Find attached a simple program reading french or english strings
depending of the value of LANGUE variable.
The xml file discribing all the strings in Freevo could be like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<freevo>
  <string name="tvguide">
    <locale lang="en">TV Guide</locale>
    <locale lang="fr">Programme Télé</locale>
  </string>
  <string name="recordprog">
    <locale lang="en">Recorded Program</locale>
    <locale lang="fr">Programme Enregistré</locale>
  </string>
<freevo>

Using the attached class, the line 148 in tv.py
items = [ menu.MenuItem('TV Guide', action=self.start_tvguide),
would be replace by:
items = [ menu.MenuItem(xmlparse.strings['tvguide'],
action=self.start_tvguide),

xmlparse being an instance of the class ParseString. I do not check the
python gettext module but my experience with C gettext version and all
.po files is that it sounds more complicate than the proposed solution.
But I may wrong...

As usual, comments are welcomed.

David
import xmllib
import xml
from xml.parsers import expat

class ParseString:

    def __init__(self):
        self.strings ={}

    def loadfile(self, filename):
        sourcexml = open(filename, 'r')
        xmlpp = sourcexml.read()
        
        def s_el(name, attrs):
            global glob_name
            global type
            global lang
            glob_name = name
            if name == 'string':
                type=attrs['name']
            if name == 'locale':
                lang=attrs['lang']
        def e_el(name):
            pass

        def c_data(data):
            global glob_name
            global glob_val
            global glob_url
            if data:
                if glob_name == 'locale':
                    if lang==LANGUE:
			self.strings[type]=data
                    glob_name = 0

        # Parse the file
        prs = xml.parsers.expat.ParserCreate()
        prs.StartElementHandler = s_el
        prs.EndElementHandler = e_el
        prs.CharacterDataHandler = c_data
        prs.returns_unicode = 0
        prs.Parse(xmlpp)


# Freevo will use french strings
LANGUE='fr'
xmlfile = ParseString()
xmlfile.loadfile("string.xml")
print xmlfile.strings['tvguide']
print xmlfile.strings['recordprog']

# Freevo will use english strings
LANGUE='en'
xmlfile.loadfile("string.xml")
print xmlfile.strings['tvguide']
print xmlfile.strings['recordprog']

Reply via email to