3

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

Community
  • 1
  • 1
usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305

4 Answers4

18

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.

Jorge Cevallos
  • 3,667
  • 3
  • 25
  • 37
  • If you want to get the selected language of your device, this might help you: Locale.getDefault().getDisplayLanguage(); – Alan Nov 07 '16 at 17:55
3

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.

Kenneth Browning
  • 1,360
  • 1
  • 13
  • 22
1

See reference to android.view.Display class (reference) and java.util.Locale class (reference)

Vladimir
  • 1,532
  • 1
  • 13
  • 13
1

Look the answer here, especially notice the comment.

Community
  • 1
  • 1
Gangnus
  • 24,044
  • 16
  • 90
  • 149