Asset File Localization

I have several html files in the resource folder. How can I localize them? Can I use only one hard drive to select the desired language based file?

+44
android localization
Mar 25 '11 at 2:32
source share
6 answers

This is not directly supported, but here is what I did ...

Divide your files into groups by country code (for example, what you do for regular resource files), and then create a localized line in each localized string.xml file called a "prefix" (where the prefix will be "en" "for English, for example )

Then, when you create your resource file names, just use something like getString("prefix") + "-" + "<name-of-asset-> .

At least some of the changes above should work for you.

+53
Mar 25 2018-11-11T00
source share
— -

If you want to localize the HTML file, you can simply put it under res / raw- <language> /filename.html (where <language> = en, es, fr, it, etc.), then access it from of your code with resource identifier R.raw.filename. The structure will display the correct file according to the locale.

+30
Mar 28 '12 at 15:56
source share

Put your files in a folder with a local suffix. Define the String resource "myLocalizedFileName" for each file and get the file name through R.string.myLocalizedFileName.

Example:

Folder structure:

 assets/ assets/help.html assets/help_de.htlm 

String resources for each language in res / values ​​/strings.xml:

 <resource> <string name=helpFile>help.html</string> </resource> 

WebView call:

 public class HelpActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { ... findViewById(R.id.helpWebView) .loadUrl("file:///android_asset/" + getString(R.string.helpFile)); } } 
+22
Aug 04 '15 at 18:53
source share

Trying to localize using assets-ja will not work, because, unfortunately, these are not resource files. The best option is localization programmatically, with the correct locale. Alternatively, the contents of the HTML file is plain text. If this suits your project, you can try to save this line as an entry in the strings.xml file (or your own myHtmlText.xml ?) myHtmlText.xml new values-ja folder, for example.

+1
Mar 25 2018-11-11T00:
source share

Placing files in the raw-LOCALE folder will automatically select the correct location, but if you upload these files to the webView path, they will be damaged after obfuscation using Proguard.

Proguard breaks Android WebView, why?

Then the only solution is to use the assets folder and use the LOCALE file and select the correct files.

+1
Jul 29 '14 at 6:24
source share

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() :

 /** * Regex that matches a resource string such as <code>@string/a-b_c1</code>. */ private static final String REGEX_RESOURCE_STRING = "@string/([A-Za-z0-9-_]*)"; /** Name of the resource type "string" as in <code>@string/...</code> */ private static final String DEF_TYPE_STRING = "string"; /** * Recursively replaces resources such as <code>@string/abc</code> with * their localized values from the app resource strings (eg * <code>strings.xml</code>) within a <code>source</code> string. * * Also works recursively, that is, when a resource contains another * resource that contains another resource, etc. * * @param source * @return <code>source</code> with replaced resources (if they exist) */ public static String replaceResourceStrings(Context context, String source) { // Recursively resolve strings Pattern p = Pattern.compile(REGEX_RESOURCE_STRING); Matcher m = p.matcher(source); StringBuffer sb = new StringBuffer(); while (m.find()) { String stringFromResources = getStringByName(context, m.group(1)); if (stringFromResources == null) { Log.w(Constants.LOG, "No String resource found for ID \"" + m.group(1) + "\" while inserting resources"); /* * No need to try to load from defaults, android is trying that * for us. If we're here, the resource does not exist. Just * return its ID. */ stringFromResources = m.group(1); } m.appendReplacement(sb, // Recurse replaceResourceStrings(context, stringFromResources)); } m.appendTail(sb); return sb.toString(); } /** * Returns the string value of a string resource (eg defined in * <code>values.xml</code>). * * @param name * @return the value of the string resource or <code>null</code> if no * resource found for id */ public static String getStringByName(Context context, String name) { int resourceId = getResourceId(context, DEF_TYPE_STRING, name); if (resourceId != 0) { return context.getString(resourceId); } else { return null; } } /** * Finds the numeric id of a string resource (eg defined in * <code>values.xml</code>). * * @param defType * Optional default resource type to find, if "type/" is not * included in the name. Can be null to require an explicit type. * * @param name * the name of the desired resource * @return the associated resource identifier. Returns 0 if no such resource * was found. (0 is not a valid resource ID.) */ private static int getResourceId(Context context, String defType, String name) { return context.getResources().getIdentifier(name, defType, context.getPackageName()); } 

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 .

+1
Oct 19 '14 at 17:16
source share



All Articles