Android app uses multi-language switching to implement _android

Source: Internet
Author: User
Tags locale

The example of this article for everyone to share the Android app implementation of multi-language switching function for your reference, the specific content as follows

1. Add multiple language files
adding string.xml files in different languages under different value folders (such as value, Value-en, VALUES-ZH-RTW folders), our project adds three languages in English, Simplified Chinese, and traditional Chinese, as shown in the following illustration:

Project mode:


Android Mode:


English translation, traditional if not specifically translated, you can find a simple conversion site, directly Simplified Chinese into traditional Chinese, recommend a website:
http://www.vifo.com.cn/fanti/

Below I only explain the practice of Simplified Chinese and English;

2. Now we can get the language of the system in the Activity Initiation (onCreate () or Onresume ()):

Locale Locale = Locale.getdefault ();
String language = Locale.getlanguage ();

Language is the language we get from the system, here if it is Chinese, get the "en", if the English is "CN";

3. Got the system language, then we have to change the system language of our app's corresponding system language:

 protected void ShowLanguage (String language) {
    //Set Application language type resources
    = Getresources ();
    Configuration config = resources.getconfiguration ();
    Displaymetrics DM = Resources.getdisplaymetrics ();
    if (language.equals ("en")) {
      Config.locale = Locale.simplified_chinese;
    } else {
      Config.locale = locale.english;
    }
    Resources.updateconfiguration (config, DM);
    Save the type of the settings language (I'm going to save it first, I'll talk about his usefulness)
    preferenceutil.commitstring ("language", language);
  }

This way, if your phone starts with the system in Chinese, then your app is Chinese, if the system is in English at the beginning, then your app is in English, and this is only available when the language has been set up, but if your current app is in Chinese and app doesn't quit, Just go to the system and set it up in English, then you will not be in English when you return to your app, still Chinese, must be in the program after the exit, then come in to become English, such experience is not good, then how can we do you change the system language, my app will follow to change it, The so-called dynamic change. Then we can do it like this.

 @Override
  protected void Onresume () {
    super.onresume ();
    String LAN = preferenceutil.getstring ("Language", "");
    Locale Locale = Locale.getdefault ();
    String language = Locale.getlanguage ();
    LOG.I ("Language ————————", language);
    if (!language.equals ("")) {
      if (!) Lan.equals (language)) {
        freshview ();//reboot mainactivity

      }
    showlanguage (language);
  }


   private void Freshview () {
    Intent Intent = new Intent (this, mainactivity.class);
    Intent.setflags (Intent.flag_activity_new_task | Intent.flag_activity_clear_task);
    StartActivity (intent);

  }

First of all Preferenceutil this is my own writing tool class, is sharedpreferences storage, in Onresume () inside, the important thing is to make a judgment, to get the language is not the same as the previous Save system language, If it's not the same, reboot the mainactivity, this way you can get the system language again.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.