Home > Tutorials : XUL Tutorial : 10.6 - Property Files

Previous Next

## Property Files

In a script, entities cannot be used. Property files are used instead.

### Properties

DTD files are suitable when you have text in a XUL file. However, a script does not get parsed for entities. In addition, you may wish to display a message which is generated from a script, if, for example, you do not know the exact text to be displayed. For this purpose, property files can be used.

A property file contains a set of strings . You will find property files alongside the DTD files with a .properties extension. Properties in the file are declared with the syntax name=value. An example is shown below:

    notFoundAlert=No files were found matching the criteria.
    deleteAlert=Click OK to have all your files deleted.

Here, the property file contains two properties. These would be read by a script and displayed to the user. You could write the code to read properties yourself, however XUL provides the stringbundle element which does this for you. The element has a number of functions which can be used to get strings from the property file and get other locale information. This element reads in the contents of a property file and builds a list of properties for you. You can then look up a particular property by name.

    <stringbundle id="strings" src="strings.properties"/>

Including this element will read the properties from the file 'strings.properties' in the same directory as the XUL file. Use a chrome URL to read a file from the locale.

This stringbundle element has a number of properties. The first is getString which can be used in a script to read a string from the bundle.

    var strbundle=document.getElementById("strings");
    var nofilesfound=strbundle.getString("notFoundAlert");

    alert(nofilesfound);

This example first gets a reference to the bundle using its id. Then, it looks up the string 'notFoundAlert' in the property file. The function getString returns the value of the string or null if the string does not exist. Finally, the string is displayed in an alert box.

(Next) In the next section, we will look at XBL, which can be used to define the behavior of an element.

Previous Next

Copyright (C) 1999 - 2004 XulPlanet.com