4

I have an application which can be localized to many languages. But I have one big trouble. I have default file values/strings.xml where I store string in English by default and I have folder values-uk/strings.xml (for Ukrainian localization). I am saving the selected localization in SharedPreferences. The trouble is when the application starts without any selected language preference (but the system language of device is Ukrainian, I checked) my app must load Ukrainian strings from values-uk/strings.xml, but it loads English values from values/strings.xml. Can anyone explain me why this happens and how can I solve this problem. Thank you very much!

3
  • 1
    show some code how you load language ?? Commented Jun 20, 2014 at 9:40
  • Hm, but according to android documentation if application has folder with name values-XX that matches with device system device language then it loads all strings from that folder, not from default folder. Commented Jun 20, 2014 at 9:48
  • Soryy, but /res/values-uk-UA gives me invalid directory name error(( Commented Jun 20, 2014 at 9:59

2 Answers 2

9

Also always check your build.gradle (app) android/defaultConfig, if there set resConfigs("en", "de", "ru"...). If yes only listed resources will leave in the project.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. You saved my day. I forget about this line in my build.gradle
At-last figure out why my translations isn't working just because i have write this line in my gradle (resConfigs :"en").
4

I had the same problem. Then I followed many tutorials. and finally I have used localization in my android application for all Indian Language. Have look at my code snippet:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        //SharedPreferences.Editor edit=getSharedPreferences("pref",MODE_PRIVATE).edit();

        switch (id){
            case R.id.action_assamese:
                setLocale("as");
                return true;
            case R.id.action_bihari:
                setLocale("bh");
                return true;
            case R.id.action_english:
                setLocale("en");
                return true;
            case R.id.action_gujarati:
                setLocale("gu");
                return true;
            case R.id.action_hindi:
                setLocale("hi");
                return true;
            case R.id.action_kannada:
                setLocale("kn");
                return true;
            case R.id.action_kashmiri:
                setLocale("ks");
                return true;
            case R.id.action_malayalam:
                setLocale("ml");
                return true;
            case R.id.action_marathi:
                setLocale("mr");
                return true;
            case R.id.action_oriya:
                setLocale("or");
                return true;
            case R.id.action_sanskrit:
                setLocale("sa");
                return true;
            case R.id.action_tamil:
                setLocale("ta");
                return true;
            case R.id.action_telgu:
                setLocale("te");
                return true;
            case R.id.action_urdu:
                setLocale("ur");
                return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void setLocale(String lang) {
        Locale myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, MainActivity.class);
        startActivity(refresh);
        finish();
    }

For more details you can follow this tutorial.

I hope it may help you.

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
Ok I will update it but This link provides complete tutorial. So I can add certain part. Either I have to copy all the text from that tutorial or I have to share the tutorial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.