howto

Add multiple language support for your Android application independent of the phone global settings

There is a way to create a multiple language application in Android and let the user choose which language to use on application level. This is very positive because not everybody wants to use the application on the system level language or they do not have a phone in their language. So the following is a way to implement application level language settings. This excerpt is used in the application when the Menu button is pressed, it will show all the options you need plus the language selection. After the language is selected, the application will restart itself in the new language.

Code snippet:

case 112: //Language Select
dialog = new Dialog(MainActivity.this);
  dialog.setContentView(R.layout.langselect);
  dialog.setCancelable(true);

  dialog.setTitle(R.string.sChooseLang);

     Button butBack = (Button) dialog.findViewById(R.id.backButton);

     butBack.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
             dialog.dismiss();
         }
     });

 hmLocales = new HashMap<String, String>();
 hmLocales.put(getString(R.string.sMacedonian), "mk"); // Macedonian
 hmLocales.put(getString(R.string.sAlbanian), "sq"); // Albanian
 hmLocales.put(getString(R.string.sEnglish), "en"); // English
 hmLocales.put(getString(R.string.sSerbian), "sr"); // Serbian
 hmLocales.put(getString(R.string.sFrench), "fr"); // French

 lvLangs = (ListView) dialog.findViewById(R.id.lvLanguages);
 String sLanguages[] = this.getResources().getStringArray(R.array.saLanguages);
 lvLangs.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , sLanguages));                        

 dialog.show();

 lvLangs.setOnItemClickListener(new OnItemClickListener() {
    //@Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        String t = (String) lvLangs.getItemAtPosition(position);
        String sLocale = hmLocales.get(t);

        MyApplication.updateLanguage(getApplicationContext(), sLocale);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        dialog.dismiss();
    }
 });

 

After you have finished coding, you will need to create new folders for the translated strings based on the country/language codes that you have in your application. Based on the example above there should be  res/values, res/values-en, res/values-sq, res/values-fr and res/values-sr folders. There is no res/values-mk because the Macedonian translation would be the default and therefore it is stored in res/values. Note that the country/language codes on the folders need to match the ones in the code.

You are all set! Now get busy and translate the strings 🙂

In another post I will talk how to store the language value permanently (unless the user changes it) so that it will not reset to default on every restart of the application.

How to move your WordPress to a new domain name

As you might have noticed, there is a new domain name for this blog. Now the domain is http://redoem.com It is easier to remember and shorter than the previous one (http://blog.zasekoj.com).

The whole process took approximately 45 minutes, but yours could vary depending on the amount of posts, comments, images etc that you have.

NOTE: this is not for blogs hosted on wordpress.com. For them many of the steps will be different, but you can try and adapt the following.

OK, enough chit-chat, let’s get to work. First of all, of course, you need to buy a new domain name (I am using DreamHost – you can register as well for hosting and use coupon REDOEM30 to get 1 extra FREE lifetime domain registration and $30 off your total amount!).

  1. Create a new user in your panel (if the hosting does not do it automatically) which will have FTP access to your domain name.
  2. Remove all the junk that you do not need like spam comments pending, drafts you are not planning to use, themes that you don’t need etc. This takes additional space and there is no need to transfer it across different databases.
  3. Disable all the WordPress plugins and empty cache if you are using caching plugin
  4. Export your database
    • Login to your MySQL server (phpMyAdmin) and click on the Export tab
    • Select your database on the left of the screen (do not select the Information_Scheme database)
    • All of the settings by default should be OK, unless you are advanced user and you know what you are doing and what else you might need
    • Go to the bottom of the page and click the check box that says Save as file
    • Enter a template for your filename (the default is OK)
    • Leave the Compression set to None. It will cut few steps later
    • Click the Go button on the bottom-right and save the file to your disk
  5. Open the saved export file in a text editor which supports Find and Replace (like Notepad++, TextMate, GEdit etc)
  6. Find and replace all of the instances where your old domain name is used. NOTE: start with the longest string, for example db.olddomain.com would be replaced with db.newdomain.com, then go on to the lower lever by replacing olddomain.com to newdomain.com, and last just the domain name from olddomain to newdomain. Please be careful with this last one because it could change some of the things that you don’t want to be changed. I recommend to check which instances are they and either replace them one by one where needed or use extra filters. For example when I was doing this last bit I used /home/olddomain/ to /home/newdomain/ because that worked for me, see if such options will work for you too.
  7. If you want to change the database name, at the top of the file there should be a statement like “CREATE DATABASE namehere ….” change the name to whatever you want. Please note that later you will need this name.
  8. Save the file
  9. Login to your new MySQL Server (phpMyAdmin) for the new domain.
  10. Click Import and select the file that you modified. Click OK
  11. When the import is complete, you will receive a message stating that, if there are errors you will be informed as well. Now we will assume that it was successful.
  12. Login via (S)FTP to your olddomain where the WordPress installation is currently located
  13. Copy all the files (the wp-include, wp-content and wp-admin folders as well) from olddomain.com to your local disk (recommended) or directly to the newdomain (usually via SSH)
  14. Open the file wp-config.php in a text editor and modify the following fields:
    • define(‘DB_NAME’, ‘newdatabase’);  — this is the name from step 7
    • define(‘DB_USER’, ‘newdatabaseuser’);   — this is the user that you use to connect to the database/phpMyAdmin
    • define(‘DB_PASSWORD’, ‘newdbpass’);  — this is the password that you use to connect to the database/phpMyAdmin
    • define(‘DB_HOST’, ‘db.newdomain.com’);  — this is the hostname for the new database
  15. Transfer all the files to the newdomain.com folder
  16. Try to open the newdomain.com site — it should open successfully
  17. Reactivate all the plugins that you had previously, or the ones that you want one. Some of them might need to be re-configured to work on the new domain (SEO, Google Sitemap etc)
  18. Change any custom code that you have, like Google Analytics, Adwords etc
  19. Create a new file called .htaccess(note the dot at the beginning and no extension) for permanent redirect from the old site to the new site. This is important so that you don’t lose any traffic. However if you are losing the olddomain.com you cannot do this. Enter the following in it:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
  20. Place the newly created file .htaccess inside the olddomain.com directory
  21. Remove all the files inside the olddomain.com directory (EXCEPT the .htaccess file that you added in step 20)
  22. Try the old site and see if the redirect works properly

Well, that was it. After all of these steps your WordPress installation should be moved to the new domain name and everything should stay the same. The redirect is very important (step 19) so make sure you keep your old domain for at least several more months after the move so that all the old traffic is redirected successfully. You can omit this step if you have a new site with very few visits, but then again I guess you can just create a new WordPress installation and do not perform the above steps 🙂

Enjoy and let me know in the comments if you have any problems.

 

Execute process in Linux/UNIX to run in the background by using NOHUP

If you need to execute a process that will run in the background so that it does not lock one terminal window then you should try using the NOHUP command.

Example usage:

nohup <command here> &

After executing it, a nohup.out file will be created (append if it exists) which will contain all the output from the command (if any).

However, if you want to close the terminal window, like if you are working on a remote machine, do the following:

(nohup <command here> & ) &

This will disconnect the process from your session completely so you should be able to close the terminal window and do not interrupt the process.

To check if the process is still running, perform the following:

ps -ef|grep -i <command name or part of command name>

This is listing all the processes on the machine and then grepping for the command name with case insensitive (-i).

If you want to stop the process, you will need to kill it with the following command:

kill <process id>

The process-id can be seen from running the ps command above. In most instances it is the first number of the row.

If the process hangs after the kill command, do force kill:

kill -9 <process id>

This will kill any process, so please double check the process id before executing it.

Safely erase a hard disk drive with shred

I have an older tiny HDD of 80GB and I am planning to give it to a friend or donate it to the local hacklab, but before it goes out of the house I want to make sure that nobody (at least with the technology available to most individuals) can restore what was on the disk. There was nothing very important or secretive on it, but people snooping around my files creeps me out a bit.

There are many ways to completely erase a hard disk drive, a partition or individual files with the GNU/Linux utility called shred.

The command to use is very simple:

sudo shred -n <NUMBER OF ITERATIONS> -vz /dev/to/hddOrPartition

And to shred a file:

shred -n <NUMBER OF ITERATIONS> -v file1 file2 file3 etc

It should be executed as super user/root so that there will be full access to erase/write everything. I use about 20 iterations (default is 25), because that seems like enough to me. The -z option means to run an extra run at the end which will zero the drive which will hide the shredding (in -n is 20, there will be total of 21 passes). -v is verbose, to show progress of the task.

WARNING: please be VERY careful about the disk/partition that you select because EVERYTHING on it will be deleted. Create backup before the command is executed!

 

Enable global keys (hot-keys) in Ubuntu on Asus K53

On Asus K53 most of the hot-keys are not working with the default installation of Ubuntu 11.04 with kernel 2.6.38-8, however there is a package available that will enable them, here are instructions on how to perform that:

sudo apt-get install git  (unless you already have git installed)
git clone git://git.iksaif.net/acpi4asus-dkms.git
cd acpi4asus-dkms 
make 
sudo make install
sudo modprobe asus-nb-wmi   (this command will load the newly installed module)

After every kernel update the above will be erased and you will need to re-do it every time until it is included in a kernel version at a later stage. To make it easier create a .deb package which will be easier to install.

sudo apt-get install debhelper 
cd acpi4asus-dkms 
dpkg-buildpackage
sudo dpkg -i [newly-created-package].deb
sudo modprobe asus-nb-wmi

Disable “Restart Now” on automatic updates in Windows

If you have setup your Windows machine to perform automatic updates, when a new update is available it will nag you with a popup message to restart your computer so that the updates can be applied.

There are several ways to disable or post-pone this from annoying you every 10 minutes (default setting).

  1. Disable the Automatic Updates Service for the current session, meaning it will not show up this message until the computer is restarted. Note that this will be reset after the restart so you will need to repeat it again next time when you have new updates to be applied. To disable the automatic updates for the current session type the following in the Run screen in Windows (Start -> Run):
    sc stop wuauserv

  2. Modify the group policy settings so the time between message popup is greater than the default value. Open the Group Policy Settings by entering gpedit.msc in Start -> Run. On the screen that will open, go to the following location:
    Local Computer Policy

    Computer Configuration
    Administrative Templates
    Windows Components
    Windows Update


    Now, locate the Re-prompt for restart with schedule installations option in the list and double click on it.

    Set it is as Enabled and change the waiting period to 1440 minutes (maximum value == 1 day). Enjoy the uninterrupted work! 🙂

Note: Option 2 works only on Windows XP Professional and not on Windows XP Home. However, option 1 works for all versions of Windows XP. These options were not tested on Windows 7, but they might work there as well.

HOWTO: Opt-out of behavioral advertising

All of the advertising companies are storing cookies on the computer so that they can monitor the users online activities and web preferences to serve ads which are similar to their taste so that they can increase their effectiveness. This is also known as Behavioral Advertising.

There are several ways to opt-out of the behavioral advertising

  1. Google’s advertising companies have a website where you are able to see what they have stored about you already in their cookie(s). They are listed as categories and you can individually remove some or opt-out completely so that in the future they do not collect information about you and just serve you ads related to the search or website you are visiting (more random). Click here to visit the Google website where you can opt-out of their advertising companies.
  2. Network Advertising Initiative provides a big list of advertising companies who are keeping cookies for behavioral advertising and they offer a feature to opt-out of them all at once. In the list you can see which of advertising companies already have a cookie on your computer, but you will not be able to see details about the information they have about you, like Google does. Click here to visit the Network Advertising Initiative website.

Note: The opt-out in this text is related to the cookies kept on your computer and how the advertising companies are monitoring your online activities to serve you tailored ads. You will still see ads from these companies after you opt-out using the method described above.

Go to Top