Saturday 22 June 2013

Localization Example in Android

LocalActivity.java
package com.local;

import java.util.Locale;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class LocalActivity extends Activity {
    /** Called when the activity is first created. */
Button mButton;
TextView mTextView;
String language ="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton=(Button)findViewById(R.id.button);
        mTextView=(TextView)findViewById(R.id.txt);
     
        mButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

       Locale locale = null;
       if (language.equalsIgnoreCase("en")) {
           locale = new Locale("en");
       } else if (language.equalsIgnoreCase("es")) {
           locale = new Locale("es");
       }
       Locale.setDefault(locale);
       Configuration config = new Configuration();
       config.locale = locale;
       getBaseContext().getResources().updateConfiguration(config, null);
       mTextView.setText(getString(R.string.app_name));
//        mButton.setText(getString(R.string.app_name));
}
});
       
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu , menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.english:
            language="en";
              break;
              case R.id.spanish:
             language="es";
             break;
        default:
              return super.onOptionsItemSelected(item);
        }
        return true;
}
}
In values folder string.xml
string.xml files
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Application Name</string>
</resources>
-----------------------------------------------------------------
in values-es folder string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, LocalActivity!</string>
    <string name="app_name">nombre de la aplicaciĆ³n</string>
</resources>

in Device select menu option for change language.



1 comment:

  1. Hi,
    If you’re interested to collaboratively translate web software, PC software, mobile software or any other type of software, I warmly recommend POEditor, a localization platform that my team developed not long ago, which will probably help you get your translation projects done a lot faster and easier.
    POEditor has an intuitive work interface, which makes it easy to use for technically inexperienced translators. It is perfect for crowdsourcing and it has a lot of management-oriented features, that give full control over the translation workflow, which you can find enlisted on our website here:
    http://poeditor.com/
    You can import from multiple language file formats or just use our REST API.
    Feel free to try it out and recommend it to developers and everyone who might find it useful.

    ReplyDelete