2016-09-05 69 views
0

我在不同的語言中有幾個Xml,但是當我以編程方式獲取XmlResourceParser時,我總是獲取默認語言。Android本地化xml文件

這是我應得的編程XML:

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

我的XML看起來是這樣的:

<?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> 

我對每一種語言的不同版本的文件在不同的文件夾中(XML ,xml-es,xml-fr,xml-it),但即使將我的設備語言更改爲某些其他語言,我也始終得到默認版本... 我在做錯什麼?沒有辦法將plist本地化嗎?

感謝您的幫助。何塞

回答

0

我終於設法本地化我的XML文件,我所做的就是我的文件移動到原始文件夾,所以我結束了一個(原始,原始ES,原始的,原始-FR)

並解析從他們身上我用的信息:我希望這可以是一個人的未來有用

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; 
    } 

。 Registers Jose