13

I want to obtain only the available languages for my android application, i.e. those for which I have a folder named values- (e.g. values-en, values-fr) in res folder.

I do not want to store the language codes and I think to list all the sub folders of "res" of the form values-* and take the language code from their name. (eventually check if the code is in the array returned by Locale.getAvailableLocales() to be sure that it is correct). This idea is stated here How to get the available languages (Not all of them, just the languages available on my app).

I have tried using

getResources().getAssets().list("res"); getResources().getAssets().list("/res"); 
getResources().getAssets().list("/res/");

but none of them worked.

Do you have any idea how I can list the sub folders of "res" folder?

Thank you in advance.

Community
  • 1
  • 1
niculare
  • 3,629
  • 1
  • 25
  • 39

2 Answers2

2

I don't know of a direct way to do this, but I have done a similar thing with the assets folder which should work fine. I use IntelliJ IDEA and ANT on Windows for my building, but you should be able to adapt this for Eclipse or other IDE and *nix/OSX.

Before compiling, I use an ANT build facet to run a dir command to list all files in the assets folder and pipe the output to a text file:

dir /assets /s /b > /res/raw/filelist.txt

I then read filelist.txt into a hashmap to give me very easy and fast way to find any file (I have hundreds of files in dozens of assets subfolders and AssetManager is too brain dead to deal with that).

public class AssetsFileManager {

   private static Map<String, String> files = new HashMap<String, String>();

   static{

        BufferedReader in = new BufferedReader(new InputStreamReader(this.getResources.openRawResource(R.raw.filelist)))
        String line = "";
        while ((line = in.readLine()) != null) {               
           map.put(line.substring(line.lastIndexOf("\\")+1), line);
        }
        in.close();
   }
}

Note the escaped backslash, \\

This gives me a map keyed on the filename and the full path as the value.

You could easily adapt this approach to get your folder list. E.g. you might use dir /ad /b /s

Simon
  • 14,407
  • 8
  • 46
  • 61
0

I'm not sure that this will work, but you can get all possible languages with Locale.getAvaliableLocales() and try to get some resource (e.g. app_name) for each locale in list. Here is one possible way.

Community
  • 1
  • 1
Jin35
  • 8,602
  • 3
  • 32
  • 52