Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to support multiple language in a application ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 294
    Comment on it

    In this tutorial, we will know that how to use multiple language in a single application . We are using four language to show our contents such as English, Russian, German, French. Follow below steps.

    Step 1- Prepare string resource for the four language like English,French,Russian and German.

     

    <resources>
        <string name="app_name">MultiLanguage</string>
        <string name="hello_world">Hello World</string>
        <string name="btn_en">English</string>
        <string name="btn_ru">Russian</string>
        <string name="btn_de">German</string>
        <string name="btn_fr">French</string>
    </resources>

     

    Step2- Created string.xml file for all four language

    Source code values-ru/strings.xml (Russian)
        <resources>
        <string name="hello_world">, </string>
        <string name="btn_en"></string>
        <string name="btn_ru"></string>
        <string name="btn_de"></string>
        <string name="btn_fr"></string>
        </resources>
        
        Source code values-fr/strings.xml (French)
        <resources>
        <string name="hello_world">Bonjour monde</string>
        <string name="btn_en">Anglais</string>
        <string name="btn_ru">Russie</string>
        <string name="btn_de">Allemand</string>
        <string name="btn_fr">Franais</string>
        </resources>
       
       Source code values-de/strings.xml (German)
        <resources>
        <string name="hello_world">Hallo Welt</string>
        <string name="btn_en">Englisch</string>
        <string name="btn_ru">Russisch</string>
        <string name="btn_de">Deutsch</string>
        <string name="btn_fr">Franzsisch</string>
        </resources>

    For English , it will pick default content from string.xml resource file

     

    Step3- Create an activity_main.xml file

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
       
      <TextView
        android:id="@+id/txt_hello"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/hello_world" />
       
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:gravity="center">
        
    <Button
        android:id="@+id/btn_en"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_en" />
       
     <Button
        android:id="@+id/btn_ru"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_ru" />
       
     <Button
        android:id="@+id/btn_de"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_de" />
       
     <Button
        android:id="@+id/btn_fr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_fr" />
        </LinearLayout>
        </LinearLayout>

     

    Step4- MainActivity.java

    Finally, we have crated activity to do complete functionality.
    
     public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        private TextView txt_hello;
        private Button btn_en, btn_ru, btn_fr, btn_de;
        private Locale myLocale;
        
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        this.txt_hello = (TextView)findViewById(R.id.txt_hello);
        this.btn_en = (Button) findViewById(R.id.btn_en);
        this.btn_ru = (Button) findViewById(R.id.btn_ru);
        this.btn_fr = (Button)findViewById(R.id.btn_fr);
        this.btn_de = (Button)findViewById(R.id.btn_de);
        this.btn_en.setOnClickListener(this);
        this.btn_ru.setOnClickListener(this);
        this.btn_fr.setOnClickListener(this);
        this.btn_de.setOnClickListener(this);
        loadLocale();
        }
       
    
        public void loadLocale(){
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
        String language = prefs.getString(langPref, "");
        changeLang(language);
        }
        
    
       public void saveLocale(String lang)
        {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(langPref, lang);
        editor.commit();
        }
       
    
       public void changeLang(String lang) {
        if (lang.equalsIgnoreCase(""))
        return;
        myLocale = new Locale(lang);
        saveLocale(lang);
        Locale.setDefault(myLocale);
        android.content.res.Configuration config = new android.content.res.Configuration();
        config.locale = myLocale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        updateTexts();
        }
        
    
       private void updateTexts(){
        txt_hello.setText(R.string.hello_world);
        btn_en.setText(R.string.btn_en);
        btn_ru.setText(R.string.btn_ru);
        btn_fr.setText(R.string.btn_fr);
        btn_de.setText(R.string.btn_de);
        }
        
    
       @Override
        public void onClick(View v) {
        String lang = "en";
        switch (v.getId()) {
        case R.id.btn_en:
        lang = "en";
        break;
        case R.id.btn_ru:
        lang = "ru";
        break;
        case R.id.btn_de:
        lang = "de";
        break;
        case R.id.btn_fr:
        lang = "fr";
        break;
        default:
        break;
        }
       
       changeLang(lang);
        }
        
    
       @Override
        public void onConfigurationChanged(android.content.res.Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (myLocale != null){
        newConfig.locale = myLocale;
        Locale.setDefault(myLocale);
        getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
        }
        }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: