0

This question here is very helpful for me to start somehow. I have never tried localization of the application. So I am totally new here. But I am facing some problems still. I have list of languages like this;

<string-array name="languages">
    <item>English</item>
    <item>Finnish</item>
    <item>French</item>
    <item>German</item>
    <item>Slovakian</item>
    <item>Polish</item>
</string-array>

I want to set default to English when the application starts. And when the language is changed I want the same language to be run through the whole activities. In the link above, in method public void setLocale(String localeCode), what will be the localeCode in my case. And what will be the locale code here,Locale locale = new Locale(localeCode); . I know here might be Locale.setDefault(Locale.ENGLISH); . I have done so far like this, I know this is crap because it is not working what I am expecting. Could you please help me a right path. Thanks in advance

public class Base_Activity extends Activity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    final Spinner spinner = (Spinner) menu.getItem(0).getActionView()
            .findViewById(R.id.spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.languages, R.layout.spinner_row);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        // private String currentLanguage;

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            // currentLanguage = spinner.getSelectedItem().toString();

            Locale locale = null;
            switch (arg2) {
            case 0:
                locale = new Locale("en_US");
                break;
            case 1:
                locale = new Locale("fi");
                break;
            case 2:
                locale = new Locale("fr");
                break;
            case 3:
                locale = new Locale("de");
                break;
            case 4:
                locale = new Locale("sk");

                break;
            case 5:
                locale = new Locale("pl");
                break;

            default:
                locale = new Locale("en_US");
                break;
            }
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());

            startActivity();

        }

        private void startActivity() {
            overridePendingTransition(0, 0);
            Log.i("DEBUG", "GATEWAY");

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            Locale.setDefault(Locale.ENGLISH);

        }

    });
    return super.onCreateOptionsMenu(menu);
}}
bShah
  • 309
  • 1
  • 6
  • 19
  • try by commenting Locale.setDefault(locale);check the following link: http://developer.android.com/reference/java/util/Locale.html#setDefault%28java.util.Locale%29 – Rama Feb 07 '14 at 09:12
  • @Rama Could you please explain briefly. Don't get you. – bShah Feb 07 '14 at 09:21
  • Hoping The following link give you the answer: http://stackoverflow.com/questions/2264874/changing-locale-within-the-app-itself – Rama Feb 07 '14 at 09:33
  • @Rama would you check my updated question above. I am not able to refresh the activity now. :( – bShah Feb 07 '14 at 10:49

2 Answers2

0

try this inside onITemSelectedListener

Locale locale = null ;

    switch (arg0) {
    case 0:
        locale = new Locale("en_US"); 
        break;
    case 1:
        locale = new Locale("fi"); 
        break;
    case 2:
        locale = new Locale("fr"); 
        break;
    case 3:
        locale = new Locale("de"); 
        break;
    case 4: 
        locale = new Locale("sk"); 

        break;
    case 5:
        locale = new Locale("pl"); 
        break;

    default:
        locale = new Locale("en_US"); 
        break;
    }

    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    this.getApplicationContext().getResources().updateConfiguration(config, null);
Sonali8890
  • 2,005
  • 12
  • 16
  • It is very hard for me to understand. c.locale = currentLanguage is not taking. It says changing the String currentLanguage to Locale, which is not possible. Could you explain me a bit briefly please. – bShah Feb 07 '14 at 07:49
  • shouldn't it be that switch (arg2), huh?? – bShah Feb 07 '14 at 08:09
  • but still no progress :( – bShah Feb 07 '14 at 08:13
  • With this I got some changes. In first activity, when I try to change language it does not change here. But when I enter to next activity, it changes the selected language here. I need to say you that this activity is a base activity where all other activities are extended to base activity. – bShah Feb 07 '14 at 09:21
  • you didn't see any changes here coz after changing language you have to refresh activity – Sonali8890 Feb 07 '14 at 09:39
  • I know to do this i have to use finish(); then start activity (getIntent()); But it is blinking the screen. How to do this?? – bShah Feb 07 '14 at 09:53
  • u can use overridePendingTransition(0, 0); with startActivity(); – Sonali8890 Feb 07 '14 at 10:17
  • do not change anything:(. Can you check my updated question above please. – bShah Feb 07 '14 at 10:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47022/discussion-between-bshah-and-user88) – bShah Feb 07 '14 at 13:14
  • should the Indent start the activity which application start at the first sight ? – Jeff Bootsholz Sep 13 '18 at 13:48
0

check the way i have done. 1.You have to create different "values" folders for different languages under resources folder: ie... values--fr(for france), values-en(for engish) 2. Now place string data for different languages in "string.xml" for relavent values folder

  1. Then set data to views based on local language. when you change language then refresh your data fields as follows:

    gameTitle.setString(GameActivity.gameActivity.getResources().getString(R.string.Jimmy_Jigsaw);

    Hoping it will help you

Rama
  • 1,156
  • 1
  • 7
  • 13
  • I have so many strings, Do you want I do for each string as you are mentioning above. I have done all you have said before like making folders /res/values-en/strings.xml. When I select one language it does not refresh at the mean time. That's only my problem. – bShah Feb 07 '14 at 13:25