Tuesday, June 16, 2015

// //

Android Localization Tutorial

What is Localization?
Is the process of adapting a product or service to a particular language, culture, and desired local "look-and-feel.

What are we going to do?
This time we will have to make an android localization and variety of languages.
Check the latest language code.

1.
Android Localization Tutorial

2.
Android Localization Tutorial

3.
Android Localization Tutorial


We are going to create a four (values) in resources folder:

1.values-ar
2.values-en
3.values-hi
4.values-ta






Copy this String.xml and styles.xml to the values(values-ar,values-en..etc)

Note that you can change it into the your real language.



    Androidlocalization
    Hello world!
    AndroidLocalize
    It is just a test
    Select the language
    Choose the language

      
       
        ?????
        ?????
        English
          algerian
    



please note that the (ar,en,hi,ta) is the language code(ar-arabic, en-english, hi-hindi, ta-tamil) 

activity_main.xml



    

    
    
    
    
  

MainActivity.java
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	int selected =0;
	int temp;String text="";
	
	Button btn;
	Locale myLocale;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	
	}
	
	public void SelectLanguage (View v){
		build_popup();
	}
	

	public void setLocale(String lang) {

		myLocale = new Locale(lang);
		Resources res = getResources();
		DisplayMetrics dm = res.getDisplayMetrics();
		Configuration conf = res.getConfiguration();
		conf.locale = myLocale;
		res.updateConfiguration(conf, dm);
		Intent refresh = new Intent(this, MainActivity.class);
		startActivity(refresh);

	}




	private void build_popup() {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Alarm Type!");
		
		builder.setSingleChoiceItems(R.array.languages, selected, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				temp = which;
			}

			
			
		});
		
		builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				selected = temp;
				
				switch(selected) {
				
				case 0:text = "?????";
				setLocale("ta");
				
				
				//Toast.makeText(MainActivity.this, "You Select " + text, Toast.LENGTH_LONG).show();
				break;
				
				case 1:text = "?????";
				
				setLocale("hi");
				
				break;
				
				case 2:text = "English";
				setLocale("en");
				break;
				
				case 3:text = "English";
				setLocale("ar");
				break;
								
				}
				
				
			//	Vibratetext2.setText(text);
				//Toast.makeText(MainActivity.this, "You Selected " + text, Toast.LENGTH_LONG).show();
				
			}
		});
		
		builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			dialog.cancel();	
			}
		});
		
		AlertDialog al =builder.create();
		al.show();
	}

}


strings.xml


    Androidlocalization
    Hello world!
    AndroidLocalize
    TEST
    Select the language in which you want to greet Sachin!!!!
    Choose the language

      
     
        ?????
        ?????
         English
          algerian