2

I need to switch between two languages inside the android application itself. Now, I have string values for two different languages in two strings.xml files in two different folders, one is under Values folder(by default) and another one is under values-fr(for french) which is created when tried Edit translation under language in android studio.But I don't know how to switch between the languages. It shows the default language (i.e., English) but don't know how to implement a way to switch to other language.

Does any one have easy way of implementing it...?

alamar
  • 18,729
  • 4
  • 64
  • 97
Gunaseelan
  • 23
  • 1
  • 9
  • try this tutorial http://www.androidhive.info/2014/07/android-building-multi-language-supported-app/ – Akshay Jan 25 '17 at 05:13

2 Answers2

1

You can use Android-LocalizationActivity

Here an excerpt from the documentation:

Keep calm and stay easy with multiple language supported in your android application.

It's basic for android application to be supported multiple languages. Yeah! It's very easy because android has String Resource. Developer just had to prepare the text for different languages then android system will use itself. But frequently problem is "On-time Language Changing". Because the String Resource was designed to be depending on current device language. but if we want to change the language by click some button. It will be difficult to handle it. This problem will solved because I have created a new library to handle application language. It called "Localization Activity" library.

Here the example to use it from the documentation:

import android.os.Bundle;
import android.view.View;

import com.akexorcist.localizationactivity.LocalizationActivity;

public class MainActivity extends LocalizationActivity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);

        findViewById(R.id.btn_th).setOnClickListener(this);
        findViewById(R.id.btn_en).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_en) {
            setLanguage("en");
        } else if (id == R.id.btn_th) {
            setLanguage("th");
        }
    }
}

In the example above, when user click on a button. It will change to English or Thai language.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

You can use the locale class to do this .Hope this helps

   public class LocaleLanguage {

    private static Locale mLocale;

    public static void setLocale(Locale locale) {
        mLocale = locale;
        if(mLocale != null) {
            Locale.setDefault(mLocale);
        }
    }

    public static void updateConfig(ContextThemeWrapper wrapper) {
        if(mLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Configuration configuration = new Configuration();
            configuration.setLocale(mLocale);
            wrapper.applyOverrideConfiguration(configuration);
        }
    }

    public static void updateConfig(Application app, Configuration configuration) {
        if(mLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //Wrapping the configuration to avoid Activity endless loop
            Configuration config = new Configuration(configuration);
            config.locale = mLocale;
            Resources res = app.getBaseContext().getResources();
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }
}

here is the application class

    public class App extends Application {
    public void onCreate(){
        super.onCreate();
        // get user preferred language set locale accordingly new locale(language,country)
        LocaleUtils.setLocale(new Locale("iw"));
        LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LocaleUtils.updateConfig(this, newConfig);
    }
}

here is the your activity

 public class MainActivity extends Activity {
    public BaseActivity() {
        LocaleUtils.updateConfig(this);
    }
}

And for more refer this link for whole tutorial refer this link

Community
  • 1
  • 1
Pratik Gondil
  • 689
  • 1
  • 6
  • 17
  • thanks mate....'ll try this one – Gunaseelan Jan 25 '17 at 07:42
  • ok try this.let me know if you have queries.:) – Pratik Gondil Jan 25 '17 at 11:36
  • i found it difficult to toggle....actually i need to develop an app which is in english by default, whenever i press the button in actionbar it toggles between tamil language and english(default)...if u have any way of implementation please share it here.... – Gunaseelan Jan 25 '17 at 13:55
  • I am implementing this in my app follow [link] (http://www.androidhive.info/2014/07/android-building-multi-language-supported-app/) step by step you get your requirements its easy.:) – Pratik Gondil Jan 27 '17 at 04:56