Search code examples
androidlocale

Changing the device system locale not working for Indian languages other than Hindi and Bengali


With reference to this link, i'm building an android app which changes android device's system locale. It is working properly in case of Indian languages Hindi and Bengali, but for other Indian languages it's not working completely (but changing partially in some area like the am/pm part of the time). My sample code snippet:

    public void toEnglish(View v){
    IActivityManager am = ActivityManagerNative.getDefault();
    Configuration config;
    try {
        config = am.getConfiguration();
        config.locale = Locale.US;
        am.updateConfiguration(config);
        //Trigger the dirty bit for the Settings Provider.  
       BackupManager.dataChanged("com.android.providers.settings"); 
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  public void toHindi(View v) {
    IActivityManager am = ActivityManagerNative.getDefault();
    Configuration config;
    try {
        Locale locale = new Locale("hi");
        config = am.getConfiguration();
        config.locale = locale;
        am.updateConfiguration(config);
        //Trigger the dirty bit for the Settings Provider.  
       BackupManager.dataChanged("com.android.providers.settings"); 
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void toTamil(View v) {
    IActivityManager am = ActivityManagerNative.getDefault();
    Configuration config;
    try {
        Locale locale = new Locale("ta");
        config = am.getConfiguration();
        config.locale = locale;
        am.updateConfiguration(config);
        //Trigger the dirty bit for the Settings Provider.  
       BackupManager.dataChanged("com.android.providers.settings"); 
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Its not like I don't have these language support in my device, if I go through Settings -> Language & input-> Language and change language there, it works. But how to do it programmatically?


Solution

  • Adding the country string while initializing the locale done the trick for me. Here I'm adding the string "IN".

    public void toTamil(View v) {
    IActivityManager am = ActivityManagerNative.getDefault();
    Configuration config;
    try {
        Locale locale = new Locale("ta","IN");
        config = am.getConfiguration();
        config.locale = locale;
        am.updateConfiguration(config);
        //Trigger the dirty bit for the Settings Provider.  
       BackupManager.dataChanged("com.android.providers.settings"); 
    } catch (Exception e) {
        e.printStackTrace();
    }