XSLTools

Annotated docs/labels.html

687:ae7bdbf61e32
2009-06-22 Paul Boddie Added some error handling to the questionnaire application.
paulb@615 1
<?xml version="1.0" encoding="iso-8859-1"?>
paulb@444 2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
paulb@444 3
<html xmlns="http://www.w3.org/1999/xhtml"><head>
paulb@444 4
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" />
paulb@615 5
  <title>Creating Applications: Labelling Multiple-Choice Values</title>
paulb@444 6
  <link href="styles.css" rel="stylesheet" type="text/css" /></head>
paulb@444 7
<body>
paulb@444 8
<h1>Creating Applications: Labelling Multiple-Choice Values</h1>
paulb@444 9
<p>When introducing the item type multiple-choice field into the application, we defined the following values:</p><pre>&lt;?xml version="1.0"?&gt;<br />&lt;type&gt;<br />  &lt;type-enum value="(Not selected)"/&gt;<br />  &lt;type-enum value="Important"/&gt;<br />  &lt;type-enum value="Not important"/&gt;<br />  &lt;type-enum value="Personal"/&gt;<br />&lt;/type&gt;</pre><p>For
paulb@444 10
simple applications with a limited audience, it is often acceptable to
paulb@615 11
use values which are then presented unchanged such that the value <code>Personal</code> is known both inside the application and is also shown to the user as the textual string <code>Personal</code>. However, for other applications there may be good reasons not to show values directly in this way:</p><ol><li>There may be a special internal value which is not descriptive; for example <code>123</code> representing the same concept as <code>Personal</code>.</li><li>The value might be understandable not be understandable to some users; for example, the text <code>Personal</code> may not be understood by users who do not speak or otherwise use the English language.</li></ol><p>We
paulb@444 12
must therefore consider introducing additional label information in
paulb@444 13
order to remedy the first case, at least. Consequently, we may modify
paulb@444 14
the defined values as follows:</p><pre>?xml version="1.0"?&gt;<br />&lt;type&gt;<br />  &lt;type-enum value="0"&gt;(Not selected)&lt;/type-enum&gt;<br />  &lt;type-enum value="I"&gt;Important&lt;/type-enum&gt;<br />  &lt;type-enum value="N"&gt;Not important&lt;/type-enum&gt;<br />  &lt;type-enum value="P"&gt;Personal&lt;/type-enum&gt;<br />&lt;/type&gt;</pre><p>Here,
paulb@444 15
we have provided user-visible labels which can now be used by the
paulb@444 16
template. A single change to the item type choices list is required to
paulb@444 17
include these labels as the visible text in that particular form
paulb@615 18
control whilst maintaining the usage of the internal values:</p><pre>  &lt;p&gt;<br />    Item type:<br />    &lt;select template:multiple-choice-list-field="type,type-enum,value" name="..." multiple="multiple"<br />      onchange="requestUpdate(<br />        'comments',<br />        '{template:list-attribute('type-enum', 'value')}',<br />        '{template:other-elements(../options)}',<br />        '{template:child-attribute('value', template:child-element('comment', 1, template:other-elements(../options)))}',<br />        '/structure/item/options')"&gt;<br />      &lt;option template:multiple-choice-list-value="type-enum,value,selected<span style="font-weight: bold;">,text()</span>" value="..." /&gt;<br />    &lt;/select&gt;<br />  &lt;/p&gt;</pre><p>The addition, as described in the <a href="reference.html#multiple-choice-value"><code>template:multiple-choice-value</code></a> and <a href="reference.html#multiple-choice-list-value"><code>template:multiple-choice-list-value</code></a> sections of the <a href="reference.html">"Template Attribute Reference"</a> document, selects the text inside the appropriate <code>type-enum</code> elements and inserts it as a label into each choice in the item type list.</p><ul>
paulb@444 19
</ul><h3>Updating the Web Resource</h3>
paulb@615 20
<p>To update the special WebStack resource, we
paulb@444 21
now need to modify a few of the class attributes:</p>
paulb@444 22
<pre>    template_resources = {<br />        "structure" : ("structure_multivalue_label_template.xhtml", "structure_output.xsl")<br />        }<br />    init_resources = {<br />        "structure" : ("structure_multivalue_label_template.xhtml", "structure_input.xsl")<br />        }<br />    document_resources = {<br />        "types" : "structure_types_label.xml"<br />        }<br /></pre>
paulb@444 23
<p>With these adjustments, it should now be possible to see the
paulb@444 24
original labels and yet have the application manipulate a separate set
paulb@444 25
of internal values.
paulb@444 26
Note that it may be necessary to remove the old stylesheet for
paulb@615 27
producing output, <code>structure_output.xsl</code>, so that the updated version of the template is taken into use.</p><h3>Translating Labels</h3><p>Whilst
paulb@444 28
the above work made it possible to satisfy the first motivation of the
paulb@444 29
use of labels - to hide internal values - it did not permit us to
paulb@444 30
provide translations for different languages. In fact, there are at
paulb@615 31
least two approaches which could provide labels in multiple languages:</p><ol><li>Define a file containing the <code>types</code> and <code>type-enum</code> elements for each language.</li><li>Use the internationalisation support in XSLForms to translate the labels.</li></ol><p>The
paulb@444 32
former approach might work in situations where multiple-choice values
paulb@444 33
are obtained from a repository, such as a database, which contains the
paulb@444 34
labels for items in each supported language. One can envisage a product
paulb@444 35
database, for example, containing product descriptions for each
paulb@444 36
language or market, and such information could be extracted in such a
paulb@444 37
way that it could be convenient to use many different data files (or to
paulb@615 38
extract the information dynamically, insert it into the form data
paulb@444 39
document, and to provide a reference to the form data document as a
paulb@444 40
source of value information).</p><p>Let us concentrate, however, on the
paulb@444 41
latter, more convenient approach for our example application. In order
paulb@615 42
to produce translated labels, we must first define a <a href="internationalisation.html#PreparingTheTranslations">translations file</a> as described in the <a href="internationalisation.html">"Internationalisation"</a> document; this file can be saved alongside our other resources with the name <code>translations.xml</code>, and its contents can be defined as follows:</p><pre>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;<br />&lt;translations&gt;<br />  &lt;locale&gt;<br />    &lt;code value="nb"/&gt;<br />    &lt;code value="nb_NO"/&gt;<br />    &lt;translation value="(Not selected)"&gt;(Ikke valgt)&lt;/translation&gt;<br />    &lt;translation value="Important"&gt;Viktig&lt;/translation&gt;<br />    &lt;translation value="Not important"&gt;Ikke viktig&lt;/translation&gt;<br />    &lt;translation value="Personal"&gt;Personlig&lt;/translation&gt;<br />  &lt;/locale&gt;<br />&lt;/translations&gt;</pre><p>To make use of this file, we must add additional references in the Web resource's attributes:</p><pre>    document_resources = {<br />        "types" : "structure_types_label.xml",<br />        <span style="font-weight: bold;">"translations" : "translations.xml"</span><br />        }</pre><p>And to introduce the translation mechanisms into the output production, we must modify the resource further:</p><pre>        # Complete the response.<br /><br />        <span style="font-weight: bold;">stylesheet_parameters["locale"] = trans.get_content_languages()[0]</span><br />        self.send_output(trans, [trans_xsl], structure, stylesheet_parameters,<br />            <span style="font-weight: bold;">references={"translations" : self.prepare_document("translations")}</span>)</pre><p>Here, we define a <code>locale</code>
paulb@444 43
parameter for the output stylesheet using the first language specified
paulb@444 44
in each user's browser's language preferences. Then, we add a reference
paul@678 45
to the translations document specified above.</p><p>Finally, we have to change the template to make use of the translations:</p><pre>  &lt;p&gt;<br />    Item type:<br />    &lt;select name="..." template:multiple-choice-list-field="type,type-enum,value" multiple="multiple"&gt;<br />      &lt;option template:multiple-choice-list-value="type-enum,value,selected<span style="font-weight: bold;">,template:i18n(text())</span>" value="..." /&gt;<br />    &lt;/select&gt;<br />  &lt;/p&gt;</pre><p>Note that we use the <a href="../apidocs/XSLForms.Output-module.html#i18n"><code>template:i18n</code></a> extension function to modify the text found in each <code>type-enum</code> element in the types document. The usage of this function is described in the <a href="../apidocs/XSLForms.Output-module.html">extension function API documentation</a>.</p><p>Now, upon adding items in the application, if the browser is set up appropriately - in this case using <code>Norwegian Bokm&aring;l [nb]</code> as the first choice of language - the item types will appear translated in the final output.</p>
paulb@444 46
<h2>Further Reading</h2>
paulb@444 47
<p>Now that we have designed and implemented a simple application, it
paulb@444 48
may be worth reading some <a href="advice.html">recommendations</a>
paulb@444 49
about developing your own applications.</p>
paulb@615 50
</body></html>