Search code examples
javaandroidlocalization

Localization in android


I m writing one application which display all menu and all text of application in english and spanish. can any one tell me how to solve this problem.one thing i can do is to convert all text in spanish when suser select spanish language .is there any inbuilt android functionality that convert all text to selected language..


Solution

    1. create folder values-es (for Spanish)

    2. create strings.xml file for Spanish language

    3. I suppose you have two button in screen to change the language, add following code on button click

           String language ="es";
           Locale locale = null;
           if (language.equalsIgnoreCase("en")) {
               locale = new Locale("en");
           } else if (language.equalsIgnoreCase("es")) {
               locale = new Locale("es");
           }
           Locale.setDefault(locale);
           Configuration config = new Configuration();
           config.locale = locale;
           getBaseContext().getResources().updateConfiguration(config, null);
      
    4. If you want to change the language on the fly then you cane call oncreate(null)

    Thank You,