Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

android - Load language specific string from resource?

At one point in one of my Android apps I need to load strings of a specific language. For example:

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

Now I need the default (values) text.

Is there a way to load the key "txt_help" with a given language "en"? I can't find a method where I can set a locale as an additional parameter.

Or can I create a new ResourceManager with a given locale and read the String through this resource object?

Many thanks in advance. hjw

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looking in the documentation, this looks promising:

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

In other words, create a new Resources object configured to a standard locale.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...