An alternative to using a single file for a country code (as described in Andrew White's and PJ_Finnegan ) is to define the HTML only once (for example, in the assets folder) and use @string ID in it, for example
<html> <body> <p>@string/message_text</p> </body> </html>
After loading an asset into a string, you can pass its contents to replaceResourceStrings() :
private static final String REGEX_RESOURCE_STRING = "@string/([A-Za-z0-9-_]*)"; private static final String DEF_TYPE_STRING = "string"; public static String replaceResourceStrings(Context context, String source) {
The best part about this approach is that you should specify the HTML structure only once and use the android localization mechanism . In addition, it allows you to recursively refer to strings in strings.xml, which is not supported by Context.getResources() . For example:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="message_text">Some string @string/another_one.</string> </resources>
The disadvantage is that parsing is performed at runtime, so specifying highlighted HTML for each language has better performance when used in an application.
In an example that uses this code to convert HTML from an asset file to a "stylish" CharSequence (using the Kuitsi TagHandler ) that can be displayed in a TextView see TextUtil .
schnatterer Oct 19 '14 at 17:16 2014-10-19 17:16
source share