从资源加载语言特定的string?

在我的一个Android应用程序中,我需要加载特定语言的string。 例如:

values: <string name="txt_help">Help</string> values-de: <string name="txt_help">Hilfe</string> values-fr: <string name="txt_help">Aider</string> 

现在我需要默认(值)文本。

有没有办法用给定的语言“en”加载密钥“txt_help”? 我无法find一个方法,我可以设置一个区域作为一个额外的参数。

或者我可以创build一个给定的语言环境的新的ResourceManager,并通过这个资源对象读取string?

提前谢谢了。 HJW

Solutions Collecting From Web of "从资源加载语言特定的string?"

查看文档 ,这看起来很有希望:

 Resources standardResources = context.getResources(); AssetManager assets = standardResources.getAssets(); DisplayMetrics metrics = standardResources.getDisplayMetrics(); Configuration config = new Configuration(standardResources.getConfiguration()); config.locale = Locale.US; Resources defaultResources = new Resources(assets, metrics, config); 

换句话说,创build一个configuration为标准语言环境的新的资源对象。

从我自己的问题,并参考这个答案和这个答案 ,我想出了以下自定义类的解决scheme:

 package com.my.package.localisation; import android.content.Context; import android.content.res.AssetManager; import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.support.annotation.NonNull; import android.util.DisplayMetrics; import java.util.Formatter; import java.util.Locale; /** * Class to manage fetching {@link Resources} for a specific {@link Locale}. API levels less * than {@link Build.VERSION_CODES#JELLY_BEAN_MR1} require an ugly implementation. * <p/> * Subclass extends {@link Resources} in case of further functionality requirements. */ public class MyResources { private final Context mContext; private final AssetManager assetManager; private final DisplayMetrics metrics; private final Configuration configuration; private final Locale targetLocale; private final Locale defaultLocale; public MyResources(@NonNull final Context mContext, @NonNull final Locale defaultLocale, @NonNull final Locale targetLocale) { this.mContext = mContext; final Resources resources = this.mContext.getResources(); this.assetManager = resources.getAssets(); this.metrics = resources.getDisplayMetrics(); this.configuration = new Configuration(resources.getConfiguration()); this.targetLocale = targetLocale; this.defaultLocale = defaultLocale; } public String[] getStringArray(final int resourceId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { configuration.setLocale(targetLocale); return mContext.createConfigurationContext(configuration).getResources().getStringArray(resourceId); } else { configuration.locale = targetLocale; final String[] resourceArray = new ResourceManager(assetManager, metrics, configuration).getStringArray(resourceId); configuration.locale = defaultLocale; // reset new ResourceManager(assetManager, metrics, configuration); // reset return resourceArray; } } public String getString(final int resourceId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { configuration.setLocale(targetLocale); return mContext.createConfigurationContext(configuration).getResources().getString(resourceId); } else { configuration.locale = targetLocale; final String resource = new ResourceManager(assetManager, metrics, configuration).getString(resourceId); configuration.locale = defaultLocale; // reset new ResourceManager(assetManager, metrics, configuration); // reset return resource; } } private final class ResourceManager extends Resources { public ResourceManager(final AssetManager assets, final DisplayMetrics metrics, final Configuration config) { super(assets, metrics, config); } /** * Return the string array associated with a particular resource ID. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * @return The string array associated with the resource. * @throws NotFoundException Throws NotFoundException if the given ID does not exist. */ @Override public String[] getStringArray(final int id) throws NotFoundException { return super.getStringArray(id); } /** * Return the string value associated with a particular resource ID, * substituting the format arguments as defined in {@link Formatter} * and {@link String#format}. It will be stripped of any styled text * information. * {@more} * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * @param formatArgs The format arguments that will be used for substitution. * @return String The string data associated with the resource, * stripped of styled text information. * @throws NotFoundException Throws NotFoundException if the given ID does not exist. */ @NonNull @Override public String getString(final int id, final Object... formatArgs) throws NotFoundException { return super.getString(id, formatArgs); } /** * Return the string value associated with a particular resource ID. It * will be stripped of any styled text information. * {@more} * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * @return String The string data associated with the resource, * stripped of styled text information. * @throws NotFoundException Throws NotFoundException if the given ID does not exist. */ @NonNull @Override public String getString(final int id) throws NotFoundException { return super.getString(id); } } }