简体   繁体   中英

Android localize xml file

I have several Xml in different languages, however when I get the XmlResourceParser programatically I'm always getting the default language.

This is how I'm getting the Xml programatically:

XmlResourceParser xpp = TCXApplication.getContext().getResources().getXml(R.xml.some_name);

My Xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<plist version="1.0">
    <array>
        <dict>
            <key>type</key>
            <string>some</string>
            <key>other_some</key>
            <array>
                <string>...</string>
                <string>...</string>
                <string>...</string>
            </array>
            <key>other_other_some</key>
            <array>
                <string>...</string>
                <string>...</string>
                <string>...</string>
            </array>
        </dict>
    </array>
</plist>

I have different versions of this file in different folders for every language (xml, xml-es, xml-fr, xml-it) but I'm always getting the default version even when I change my device language to some of the other languages... Am I doing something wrong? Is there no way localize plist?

Thanks for the help. Jose

I finally manage to localize my xml files, what I did was to move my files to the raw folder, so I ended up with an (raw, raw-es, raw-it, raw-fr)

And to parse the information from them I use:

private List<PlistItem> parse() {
        try {
            InputStream inputStream = MyApplication.getContext().getResources()
                    .openRawResource(R.raw.some);
            XmlPullParserFactory pullParserFactory;
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = pullParserFactory.newPullParser();
            xmlPullParser.setInput(inputStream, null);

            String key = null;
            PlistItem item = null;
            int eventType = xmlPullParser.getEventType();
            while (!(eventType == XmlPullParser.END_TAG
                    && xmlPullParser.getName() != null
                    && xmlPullParser.getName().equals("plist"))) {

                if (eventType == XmlPullParser.START_DOCUMENT) {
                    list = new ArrayList<>();
                } else if (eventType == XmlPullParser.START_TAG && xmlPullParser.getName().contentEquals(PLIST_ELEMENT_DICT)) {
                    item = new PlistItem();
                } else if (eventType == XmlPullParser.END_TAG && xmlPullParser.getName().contentEquals(PLIST_ELEMENT_DICT)) {
                    list.add(item);
                } else if (eventType == XmlPullParser.START_TAG && xmlPullParser.getName().contentEquals(PLIST_ELEMENT_KEY)) {
                    xmlPullParser.next();
                    key = xmlPullParser.getText();
                } else if (eventType == XmlPullParser.START_TAG && xmlPullParser.getName().contentEquals(PLIST_ELEMENT_STRING)) {
                    xmlPullParser.next();

                    if (key != null && item != null) {
                        if (key.contentEquals(PLIST_VALUE_TYPE)) {
                            item.type = xmlPullParser.getText();
                        }
                        if (key.contentEquals(PLIST_VALUE_SOME)) {
                            item.some.add(xmlPullParser.getText());
                        }
                        if (key.contentEquals(PLIST_VALUE_OTHER_SOME)) {
                            item.otherSome.add(xmlPullParser.getText());
                        }
                    }
                }

                eventType = xmlPullParser.next();
            }

            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

I hope this can be useful for someone in the future. Regards Jose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM