Android and menu localization

I want to programmatically change the language (application language).

The main problem for me is updating menu shortcuts.

I tried the following method:

 @Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if (shouldChangeMenuLabels) {
        for (int i = 0; i < menu.size(); i++) {
            MenuItem menuItem = menu.getItem(i);
            switch (menuItem.getItemId()) {
                case R.id.menu_main_about:
                    menuItem.setTitle(R.string.menu_about);
                    break;
                case R.id.menu_main_preferences:
                    menuItem.setTitle(R.string.menu_prefs);
                    break;
            }
        }
        shouldChangeMenuLabels = false;
    }

But I'm sure this is a bad idea. I want to avoid using the switch-case statement, since this is not a universal method (I cannot just transfer the cut off to other actions / I cannot make an abstract class that would do this).


By the way, all the menus were described in the menu / * files. xml, so I do not want to duplicate the code. Does anyone have any ideas?

Concerned about the first answer: I changed the locale with the following code:

            Locale locale = new Locale((String)newValue);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getApplication().getResources().updateConfiguration(config, getApplication().getResources().getDisplayMetrics());

But since I want to control the rotation for each action, they never end. Maybe I did something wrong?

+5
3

, , :

 @Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if (shouldChangeLocale) {
        menu.clear();
        MenuInflater inflater = getMenuInflater(); // -->onCreateMenu (Menu) 
        inflater.inflate(R.menu.menu_main, menu);  // /
        shouldChangeLocale=false;
    }

    return super.onMenuOpened(featureId, menu);
}

, ?

+5

Android Localization? , /, . menu.xml , .

0

menu.add(0, MENU_ABOUT, 0, _mContext.getResources(). getString (R.string.mymenu)). setIcon (R.drawable.ic_menu);

.

0

All Articles