Skip to main content

Change language programatically in Android



Is it possible to change the language of an app programmatically while still using Android resources?





If not, is it possible to request a resource in an specific language?





I would like to let the user change the language of the app from the app.


Comments

  1. It's possible. You can set the locale. However, i would not recommend that. We've tried it at early stages, it's basically fighting the system. We have same requirement for changing language, but decided to settle to the fact that UI should be same as phone UI. It was working via setting locale but was too buggy. And you have to set it every time you enter activity ( each activity ) from my experience. here is a code if you still need this ( again, i don't recommend that )

    Resources res = context.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(language_code.toLowerCase());
    res.updateConfiguration(conf, dm);


    If you have language specific content - you can change that base on the setting.

    ReplyDelete
  2. If u write

    android:configChanges="locale"

    in every activity than no need to set it every time you enter activity

    ReplyDelete
  3. I was looking for a way to change the system language programmatically.
    While I fully understand that a normal application should never do that and instead either:


    the user should be pointed(through an intent) to the system settings to change it manually
    the application should handle its localization on its own just like described in the answer of Alex


    there was a need to really change the language of the system programmtically.

    This is undocumented API and thus should not be used for market/end-user applications!

    Anyway heres the solution i found:

    Locale locale = new Locale(targetLocaleAsString);

    Class amnClass = Class.forName("android.app.ActivityManagerNative");
    Object amn = null;
    Configuration config = null;

    // amn = ActivityManagerNative.getDefault();
    Method methodGetDefault = amnClass.getMethod("getDefault");
    methodGetDefault.setAccessible(true);
    amn = methodGetDefault.invoke(amnClass);

    // config = amn.getConfiguration();
    Method methodGetConfiguration = amnClass.getMethod("getConfiguration");
    methodGetConfiguration.setAccessible(true);
    config = (Configuration) methodGetConfiguration.invoke(amn);

    // config.userSetLocale = true;
    Class configClass = config.getClass();
    Field f = configClass.getField("userSetLocale");
    f.setBoolean(config, true);

    // set the locale to the new value
    config.locale = locale;

    // amn.updateConfiguration(config);
    Method methodUpdateConfiguration = amnClass.getMethod("updateConfiguration", Configuration.class);
    methodUpdateConfiguration.setAccessible(true);
    methodUpdateConfiguration.invoke(amn, config);

    ReplyDelete
  4. It's really work... fa=Presian... en=English...
    enter your language code in languageToLoad :

    import android.app.Activity;
    import android.content.res.Configuration;
    import android.os.Bundle;

    public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String languageToLoad = "fa"; // your language
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());
    this.setContentView(R.layout.main);
    }
    }

    ReplyDelete
  5. Alex Volovoy answer only works for me if it's in onCreate method of the activity.

    The answer that works in all the methods is in another thread

    Change language programatically in Android

    Here is the adaptation of the code



    Resources standardResources = getBaseContext().getResources();

    AssetManager assets = standardResources.getAssets();

    DisplayMetrics metrics = standardResources.getDisplayMetrics();

    Configuration config = new Configuration(standardResources.getConfiguration());

    config.locale = new Locale(languageToLoad);

    Resources defaultResources = new Resources(assets, metrics, config);



    Hope that it helps.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex