Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get available languages for an application

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.

like image 783
niculare Avatar asked Nov 23 '12 15:11

niculare


People also ask

Can an app use multiple languages?

You can provide support for different locales by using the resources directory in your Android project. You can specify resources tailored to the culture of the people who use your app. You can provide any resource type that is appropriate for the language and culture of your users.


1 Answers

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

like image 121
Simon Avatar answered Oct 20 '22 13:10

Simon