1
votes

I want to create an app which asks the user questions, scores the answers and potentially react to them presenting subsequent questions. For this I thought of something like the following XML in res/xml/questions.xml:

<?xml version="1.0" encoding="utf-8"?>
<questions>
    <question id="000" category="2">
        <text>Yes or no?</text>
        <answers>
            <choice id="0" score="+5">Yes</choice>
            <choice id="1" score="-5">No</choice>
        </answers>
    </question>
    <question id="010" category="1">
        <parent id="000" choice="0"/>
        <text>Whats my question?</text>
        <answers>
            <choice id="0" score="-5">Shut up.</choice>
            <choice id="1" score="0">I don't care.</choice>
            <choice id="2" score="+5">I like your attitude!</choice>
        </answers>
    </question>
</questions>

I want to support multiple languages. How can I translate the contents of <text> and <choice> without redefining the same logic in different XMLs? (Or should I abandon the XML approach altogether?)

1

1 Answers

1
votes

Here are some options:

Option #1: Have res/xml/questions.xml and other variants of that XML for different languages (e.g., res/xml-es/questions.xml, res/xml-de/questions.xml, res/xml-zh/questions.xml)

Option #2: Where you have the English strings, instead have values that map to string resources. So, res/xml/questions.xml might look like:

<?xml version="1.0" encoding="utf-8"?>
<questions>
    <question id="000" category="2">
        <text>question_000</text>
        <answers>
            <choice id="0" score="+5">question_000_choice_0</choice>
            <choice id="1" score="-5">question_000_choice_1</choice>
        </answers>
    </question>
    <question id="010" category="1">
        <parent id="000" choice="0"/>
        <text>question_010</text>
        <answers>
            <choice id="0" score="-5">question_010_choice_0</choice>
            <choice id="1" score="0">question_010_choice_1</choice>
            <choice id="2" score="+5">question_010_choice_2</choice>
        </answers>
    </question>
</questions>

Then you would have string resources for question_000, question_000_choice_0, and so on. When you parse the XML, you then use getIdentifier() on a Resources object to look up the string resource ID corresponding with something like question_000_choice_0.

Option #3: Have the XML simply describe the basics:

<?xml version="1.0" encoding="utf-8"?>
<questions>
    <question id="000" category="2">
        <answers>
            <choice id="0" score="+5" />
            <choice id="1" score="-5" />
        </answers>
    </question>
    <question id="010" category="1">
        <parent id="000" choice="0"/>
        <answers>
            <choice id="0" score="-5" />
            <choice id="1" score="0" />
            <choice id="2" score="+5" />
        </answers>
    </question>
</questions>

You would still have string resources for question_000, question_000_choice_0, and so on. However, instead of having those names in the XML, you would just generate them from the question and choice IDs.