XLSForm / pyxform

A Python package to create XForms for ODK Collect.
BSD 2-Clause "Simplified" License
82 stars 138 forks source link

Match default language on short code not just full language text #276

Open yanokwa opened 5 years ago

yanokwa commented 5 years ago

Inspired by https://forum.opendatakit.org/t/how-to-set-the-default-language-using-pyxform-to-convert-xlsx-to-xml/18541, I think it'd be nice that if you have the short code (e.g., pt) in a column as well as in the default_language column, that pyxform matches on that.

WinnyTroy commented 4 years ago

This seems to be possible. The only thing could be how the short codes are passed in the column headers. The current implementation uses regex to determine if the language provided in the column headers is valid. The regex provided searches for \(text\) pattern, within the language provided in the column header, where the short code is wrapped in enclosing brackets.

if that pattern is not provided, then it is assumed that the lang is not valid.

e.g survey sheet

type name label:(en) label:(pt) label:(es) hint
barcode user_qrcode User ID ID do usuário ID de usuario Scan QR Code

settings sheet

id_string default_language
test_lang_codes (pt)

Should convert successfully to:

<?xml version="1.0"?>
<h:html
    xmlns="http://www.w3.org/2002/xforms"
    xmlns:ev="http://www.w3.org/2001/xml-events"
    xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:jr="http://openrosa.org/javarosa"
    xmlns:odk="http://www.opendatakit.org/xforms"
    xmlns:orx="http://openrosa.org/xforms"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <h:head>
        <h:title>Test</h:title>
        <model>
            <itext>
                <translation lang="(en)">
                    <text id="/data/user_qrcode:label">
                        <value>User ID</value>
                    </text>
                </translation>
                <translation default="true()" lang="(pt)">
                    <text id="/data/user_qrcode:label">
                        <value>ID do usuário</value>
                    </text>
                </translation>
                <translation lang="(es)">
                    <text id="/data/user_qrcode:label">
                        <value>ID de usuario</value>
                    </text>
                </translation>
            </itext>
            <instance>
                <data id="Test" odk:generated-by="pyxform v0.15.1-97-g75d39d5">
                    <user_qrcode/>
                    <meta>
                        <instanceID/>
                    </meta>
                </data>
            </instance>
            <bind nodeset="/data/user_qrcode" type="barcode"/>
            <bind jr:preload="uid" nodeset="/data/meta/instanceID" readonly="true()" type="string"/>
        </model>
    </h:head>
    <h:body>
        <input ref="/data/user_qrcode">
            <label ref="jr:itext('/data/user_qrcode:label')"/>
            <hint>Scan QR Code</hint>
        </input>
    </h:body>
</h:html>
yanokwa commented 4 years ago

Seems like we can even drop the () and match on whatever two letter language code there is.