我有几个HTML文件在assets文件夹中。如何将它们本地化?我的唯一选择是在代码中硬编码以根据语言环境选择正确的文件吗?
我有几个HTML文件在assets文件夹中。如何将它们本地化?我的唯一选择是在代码中硬编码以根据语言环境选择正确的文件吗?
这个并不直接支持,但我所做的是...
将文件按国家代码分组(就像正常资源文件一样),然后在每个本地化的string.xml文件中创建一个本地化字符串,例如叫做"prefix"(其中"prefix"可以是英语的"en")。
然后当你构建资产文件名时,简单地使用类似于getString("prefix") + "-" + "<name-of-asset->
这样的东西。
至少上述某种变体应该适用于您。
getLanguage().substring(0, 2)
不是一个万能的方法,因为它无法处理zh-rTW
(台湾地区的繁体中文,也称为传统中文;而zh
表示简体中文)。 - Youngjaeassets/
assets/help.html
assets/help_de.htlm
每种语言的字符串资源在res/values/strings.xml中:
<resource>
<string name=helpFile>help.html</string>
</resource>
WebView调用:
public class HelpActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
...
findViewById(R.id.helpWebView)
.loadUrl("file:///android_asset/"
+ getString(R.string.helpFile));
}
}
如果不想使用每个国家代码一个文件的方式(如Andrew White和PJ_Finnegan的回答中所述),则可以定义一次HTML(例如在assets
文件夹中),并在其中使用@string
ID,如下所示。
<html>
<body>
<p>@string/message_text</p>
</body>
</html>
将资产加载到字符串中后,您可以将其内容传递给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's resource strings (e.g.
* <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 (e.g. 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 (e.g. 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());
}
Context.getResources()
不支持。例如:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="message_text">Some string @string/another_one.</string>
</resources>
缺点是解析是在运行时完成的,因此为每种语言指定专用的HTML在应用内使用时具有更好的性能。
使用此代码从资产文件转换HTML以生成可在TextView
中显示的“可样式化”CharSequence
(使用{{link1:Kuitsi's TagHandler}}),请参见{{link2:TextUtil }}的示例。
assets-ja
进行本地化不起作用,因为这些不是资源文件。最好的选择是通过正确的语言环境进行编程本地化。或者,HTML文件的内容只是纯文本。如果适合您的项目,您可以尝试将此字符串存储为新的values-ja
文件夹中的strings.xml
(或您自己的myHtmlText.xml
?)文件的条目。