资产文件本地化

问题描述:

我在资产文件夹中有几个html文件。我如何本地化他们?我唯一的选择是让硬编码根据语言环境选择正确的文件吗?资产文件本地化

+1

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

用于在res /值/ strings.xml中的每一种语言的字符串资源我需要使用资产,因为emb在HTML中编辑图像不会从原始资源中工作。如果它是一个简单的html,raw就是要走的路。这里是谁可能会好奇如何从原始monocube.com读取HTML的人的教程2011/02/08/android-tutorial-html-file-in-webview – mishkin 2014-09-25 03:04:38

这是不直接支持,但这里是我做了什么......

独立文件放入由国家代码组(就像你正常的资源文件做什么),然后创建一个本地化的字符串每个本地化的string.xml文件都被称为“前缀”(例如前缀为英语“en”)。

然后,当你建立你的资产文件名简单地使用像getString("prefix") + "-" + "<name-of-asset->

以上至少有一些变化应该适合你。

+15

谢谢阅读HTML的教程!改进你的想法,我可以使用Locale.getLanguage()。substring(0,2).toLowerCase()来获得这个前缀,所以我不需要在string.xml中添加前缀var – mishkin 2011-03-25 03:04:15

+8

请注意'getLanguage()。 substring(0,2)'不是android中的versitle方法,因为'zh-rTW'(taiwanese又名繁体中文。zh是简体中文)无法处理。 – Youngjae 2013-12-22 11:39:57

+1

优秀的解决方案。我使用这种变化将资产置于assets/en/...,assets/es/...等等。好的一点是,如果您在strings.xml中定义了前缀,那么您知道目录将存在于资产中(只要您在每次添加新的本地化版本的strings.xml时制作目录)。我正在使用它来本地化不仅html而且其他一些资产。 – mkasberg 2015-06-29 00:19:46

尝试使用assets-ja本地化将不起作用,因为可悲的是这些不是资源文件。最好的选择是以正确的语言环境以编程方式进行本地化。或者,HTML文件的内容只是纯文本。例如,如果它符合您的项目,则可以尝试将此字符串存储为新文件夹(或您自己的myHtmlText.xml?)文件中的条目。

+4

我需要使用资产,因为HTML中的嵌入式图像不会从原始的工作。但如果它是一个简单的html,那么你是对的,raw就是要走的路。这里是谁可能会好奇如何从原始http://www.monocube.com/2011/02/08/android-tutorial-html-file-in-webview/ – mishkin 2011-03-25 03:07:14

如果你想本地化HTML文件,你可以简单地把它放在RES/RAW-<语言> /filename.html(其中<语言> = EN,ES,FR,IT等),那么访问它从您的代码与资源ID R.raw.filename。该框架将根据语言环境选择正确的文件。

+1

请参阅以上关于HTML中嵌入图像的评论 - 这就是为什么我无法使用资源 – mishkin 2014-09-25 03:05:50

+1

此解决方案适用于PDF等文件。谢谢。 – Hong 2016-03-31 18:12:35

+0

适用于txt文件以及... getResources()。openRawResource(R.raw.filename) – Veener 2017-02-28 16:00:16

将文件放入raw-LOCALE文件夹将自动选择正确的位置,但是如果您在webView中加载这些文件,使用Proguard进行混淆后,路径将会中断。

Proguard Breaks Android WebView, Why?

那么唯一的解决办法是使用资产文件夹,并使用文件语言环境,并挑选了正确的文件。

使用每个国家码的一个文件(如在Andrew White'sPJ_Finnegan's答案中描述)的替代方法是定义HTML仅一次(例如,在assets文件夹),并使用@string标识在它,像这样

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

这种方法的好处是,你必须指定HTML的结构只有一次,并使用android's localization mechanism。另外,它允许在strings.xml中递归引用字符串,这不受Context.getResources()的支持。例如:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="message_text">Some string @string/another_one.</string> 
</resources> 

的缺点是,解析是在运行时完成,因此指定一个专门的HTML代码中每一种语言都有应用程序中使用时有更好的表现。

有关采用此代码到HTML从资产文件转换为“风格化” CharSequence(使用Kuitsi's TagHandler),其可以被显示在一个TextView一个例子见TextUtil

将您的文件放入具有本地后缀的资产文件夹中。为每个文件定义一个字符串资源“myLocalizedFileName”,并通过R.string.myLocalizedFileName获取文件名。

实施例:

文件夹结构:

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

web视图主叫:

public class HelpActivity extends AppCompatActivity { 
    protected void onCreate(Bundle savedInstanceState) { 
    ... 
    findViewById(R.id.helpWebView) 
     .loadUrl("file:///android_asset/" 
     + getString(R.string.helpFile)); 
    } 
} 
+1

这似乎是最干净的解决方案 - 谢谢! – 2015-11-11 17:33:06

+0

感谢您的方法,尼斯和干净 – 2015-12-21 18:14:12

+0

良好的解决方案保存字符串资源的路径 – 2016-06-10 10:38:55