简体   繁体   中英

Android: Supporting different languages without using strings.xml

I am working on an app. The app has to parse an XML to display some data. That XML is in the English language. Now we have to support the German language as well. We cannot create another XML file. We somehow have to translate that XML at runtime. The problem is XML will contain English and there are more than 500 strings that need to be translated. Storing all these strings in strings.xml using local is not efficient. Since after parsing manually we have to get values from string.xml.

My question is how can we translate this data at runtime? Can we use a property file instead? If yes, how?

Edit: Sample from my XML file is

<element name="Journal in cast iron">
     <value name="Lubrication">Lubrication of grease</value>
     <value name="µ">0.1</value>
</element>

now "Journal in cast iron", "Lubrication" and "Lubrication of grease" need to be translated to german. Similar to above element I have around 300 elements in my XML. When XML renders it should display like this:


Journal in cast iron | Lubrication of grease | µ = 0.1 |

I have written the logic to display data after rendering from xml.

Yes, I guess it's possible without creating other String files you can support different language using api's, you have to create an api which will take input as your device language & it will returns the array of those string which are defines in your string xml in KEY & Value format as api response, key will be same in both side, using that key you can use that value of different languages.

Once you fetch all the values you can store the response in your application share preferences for next time use key as your device language & response is value, next time you have to check using device language as key for value if it's not stored in device then need to go for api response.

but you have to fetch all this data before launching your login or other screen, means you have to fetch this data on Splash Screen.

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