0

I need to find how many language's string.xml is present in my application at run time. Example: I have string.xml for en, zh,fr.. and now my server is sending me language type as hi then I should check whether hi present in values folder or not???

Thanks in advance.

mradss
  • 31
  • 3
  • what you want to do depending on the server sends? And how it matters if you have or don't have translations? – Vladyslav Matviienko Jan 26 '17 at 07:28
  • [This question](http://stackoverflow.com/a/1495767/515948) should allow you to start if files are in your resources path. – Laur Ivan Jan 26 '17 at 07:31
  • If you know XPath, you may load the XML and query with it, resulting in a NodeList. If you could paste parts of the xml here, we could assemble a XPath query for you. – Balazs Vago Jan 26 '17 at 07:57

1 Answers1

0

Since your strings could be in a file other than string.xml, here's a more robust way to do this:

Take a string id that you know is translated into all your supported languages and test if it's different than the default.

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    Configuration configuration = new Configuration(getResources().getConfiguration());

    configuration.locale = new Locale(""); // default locale
    Resources resources = new Resources(getAssets(), metrics, configuration);
    String defaultStr = resources.getString(R.string.hello); // default string

    // lang is the two-character language code, e.g. "zh", "hi"

    configuration.locale = new Locale(lang);
    resources = new Resources(getAssets(), metrics, configuration);
    String testStr = resources.getString(R.string.hello);
    boolean supported = !defaultStr.equals(testStr);
    Log.d(TAG, lang + " is " + (supported ? "" : "not ") + "supported");

Note that Configuration.locale is deprecated since Nougat. See the docs for update.

kris larson
  • 30,387
  • 5
  • 62
  • 74