问题
I just found a nice tutorial that will help me implement an easy user guide system in my application, as I will be able to draw it with any plain old HTML editor.
My question is simple: since all res
ources are localizable, I can easily translate my application into new languages. What about assets
? Is there an automatic localization mechanism? I don't think so.
How do I retrieve system locale in order to construct a localized URL when loading the help page's HTML file? Is there some call to retrieve display size/density the same as coded in the resources world (ie. public String getDensity()
returns "xhdpi", "ldpi"...)?
Danke ;-)
回答1:
If you really need to check current system locale, one simple way to do it is to add an identifier string in your strings.xml files.
For example, in values/strings.xml, add:
<string name="lang">en</string>
in values-es/strings.xml, add:
<string name="lang">es</string>
in values-fr/strings.xml, add:
<string name="lang">fr</string>
and so on. Then you'll be able to check current language programmatically with something like this (to be improved of course, just as an example):
if( "es".equals( getString(R.string.lang) ) ) {
I hope this helps you.
回答2:
It is an old question, but just in case someone is doing this. I don't think you need an extra language string as suggested above:
<string name="lang">fr</string>
Simply add the assets URL string and translate it:
<string name="content_location">file:///android_asset/en/index.html</string>
in values-fr/strings.xml, add:
<string name="content_location">file:///android_asset/fr/index.html</string>
and then in the code access it as:
String URL = getResources().getString(R.string.content_location);
and this should display the URL based on the local since you defined it into the specific localized string files.
回答3:
See reference to android.view.Display class (reference) and java.util.Locale class (reference)
回答4:
Look the answer here, especially notice the comment.
来源:https://stackoverflow.com/questions/9831067/localizable-assets-in-android