Android Multi-Language Support


By default android takes English as primary language but if you want to support any other language you just need to create a string.xml file that.
                  
For this example we learn how to do multi-language support in our application.


You have to note some things before implementing multi-language support in the application.

 1) Don't set static text directly with the component. Instead use string resources.  
    
       tv.setText( “ Hello ”)     //Don’t

     First, create a string in a string.xml file than use.
       <string name=“str_hello”>Hello</string>

        tv.setTExt(R.string.str_hello);     //Do

  2) If your application supports any language from below then you need to
      give  Rtl(Right To Left) property to true in manifest.xml file.

      <application android:supportRtl=“true” />
  1.  Aramaic
  2.  Azeri
  3.  Dhivehi/Maldivian
  4.  Hebrew
  5.  Kurdish (Sorani)
  6.  Persian/Farsi
  7.  Urdu
  8. Arabic
3) If your application support Rtl than you need to keep in mind that do not use left 
    and right instead of use start and end example:-
      android:paddingLeft should be changed to android:paddingStart.

In this example, we see how to support the Hindi language to your application.

activity_Main.xml

<layout>
   
<RelativeLayout>        

<TextView          
android:id="@+id/act_login_tv_change_language"          android:text="@string/change_language"            
android:textSize="17dp" />        

<LinearLayout>           

<TextViewandroid:text="@string/str_welcome"     
 android:textSize="32dp" />            

<EditText                
  android:hint="@string/str_username" />            

<EditText    
   android:id="@+id/act_login_et_password"
   android:hint="@string/str_password" />

<Button
  android:id="@+id/act_login_bt_login"
  android:text="@string/str_login" />        

</LinearLayout>


<TextView           
   android:text="@string/str_sign_up"           
   android:textSize="18dp" />

</RelativeLayout>
</layout>


Activity_main.java

public class MainActivity extends AppCompatActivity{
   
private ActivityMainBinding mBinding;    

@Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,                                                                     R.layout.activity_main);       
          setonClickListner();    
}    

private void setonClickListner() {
        mBinding.actLoginTvChangeLanguage.setOnClickListener(new View.OnClickListener() {

           @Override
            public void onClick(View view) {
               Locale locale = new Locale("hi");                                                                           Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;                                                                                            getBaseContext().getResources().updateConfiguration(config,                               getBaseContext().getResources().getDisplayMetrics());                

                 //Restart the activity to apply changes               
                 Intent refresh = new Intent(MainActivity.this, MainActivity.class);
                 startActivity(refresh);               
                 finish();           
}        
});    
}\
}


string.xml ( For English)
<resources>    
<string name="app_name">MultiLanguageSupportDemo</string>
<string name="str_welcome">Welcome</string>   
<string name="str_username">Username</string>
<string name="str_password">Password</string>    
<string name="str_login">Login</string>   
<string name="str_sign_up">Don\'t have an account ? Sign Up Here</string>
<string name="change_language">Change Language</string>
</resources>

To create string.xml for Hindi follow below step:-

- Create a new directory in res and name it as values-hi.
- Copy the string.xml file in it change the text as below.

resource_folder

string.xml (For Hindi)

<resources>
<string name="str_welcome">स्वागत</string>   
<string name="str_username">उपयोगकर्ता नाम</string>    
<string name="str_password">पासवर्ड</string>   
<string name="str_login">लॉग इन</string>   
<string name="str_sign_up">एक खाता नहीं है? पंजी यहॉ करे</string>
<string name="change_language">भाषा बदलो</string>
</resources>

Demo:-

           

You can give as many languages support you wanted. For that, just create the right folder and put a string.xml file in it. Below is the list that shows the different countries and value names.

Language
Locale
values
German
de
values-de
Chinese
zh
values-zh
Czech
cs
values-cs
Dutch
nl
values-nl
French
fr
values-fr
Italian
it
values-it
Japanese
ja
values-ja
Korean
ko
values-ko
Polish
pl
values-pl
Russian
ru
values-ru
Spanish
es
values-es
Arabic
ar
values-ar
Bulgarian
bg
values-bg
Catalan
ca
values-ca
Croatian
hr
values-hr
Danish
da
values-da
Finnish
fi
values-fi
Greek
el
values-el
Hebrew
iw
values-iw
Hindi
hi
values-hi
Hungarian
hu
values-hu
Indonesian
in
values-in
Latvian
lv
values-lv
Lithuanian
lt
values-lt
Norwegian
nb
values-nb
Portuguese
pt
values-pt
Romanian
ro
values-ro
Serbian
sr
values-sr
Slovak
sk
values-sk
Slovenian
sl
values-sl
Swedish
sv
values-sv
Tagalog
tl
values-tl
Thai
th
values-th
Turkish
tr
values-tr
Ukrainian
uk
values-uk
Vietnamese
vi
values-vi

Comments