Android Localization

被刻印的时光 ゝ 提交于 2019-12-12 07:14:46

问题


The following code is for changing app locale into Spanish is working fine in some devices, but in some devices it is enlarging (zooming) the views in the app. Does anyone have a the solution?

Configuration config = getResources().getConfiguration();

// change this to a different Locale than your device
Locale locale = new Locale("es", "es_ES"); 
config.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());
Log.i("onSelected..", Locale.getDefault().getCountry());
startActivity(new Intent(getApplicationContext(), HomePage.class));
finish();   

回答1:


I use this method when i have to use different languages:

1) Set a int for all the languages supported. 2) Use a basic function to set Default Locale. 3) Use a function to launch in different languages.

This is the example:

2)

public static void setDefaultLocale(Context context,String locale) 
{
    Locale appLoc = new Locale(locale);
    Locale.setDefault(appLoc);

    Configuration appConfig = new Configuration();
    appConfig.locale = appLoc;

    context.getResources().updateConfiguration(appConfig, context.getResources()
            .getDisplayMetrics());
}

where locale follow the ISO 639-1

1)

private Language myLanguage;
public enum Language 
{
    Null,Spanish,English,Catalan
}

3)

    private void launchApplication(int language)
{
    // Set Language
    switch (language)
    {
        case 1:
            // Español
            setDefaultLocale(getApplicationContext(),"es");
            myLanguage = Language.Spanish;
            break;
        case 2:
            // English
            setDefaultLocale(getApplicationContext(),"en");
            myLanguage = Language.English;
            break;
        default:
            // Catalan
            setDefaultLocale(getApplicationContext(),"ca");
            myLanguage = Language.Catalan;
            break;
    }

    Intent intent = new Intent(this, MyActivity.class);
    startActivityForResult(intent, 2);
    // Finish the Activity when return from the other Activity
    finish();


}

Then, call launchApplication(int selected); and must be work!




回答2:


and you have to add 'locale' to configuration changes for your activity in manifest. Without this my activity ignored locale changes sometimes.




回答3:


//you are updating the configuration using the displaymetris

so it will made changes to your configuration

getBaseContext().getResources().updateConfiguration(config, getResources().getDisplayMetrics());


来源:https://stackoverflow.com/questions/8806061/android-localization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!