Archive for August, 2007

Don’t let Unicode support be the death of you

Posted on August 19th, 2007 in Linux, Programming, Web Development | 4 Comments »

You Americans (and British, Australians and any other English speakers) have it easy. When you need to create a web application, all you need to use for your text is the basic English alphabet, only 26 different characters and 10 digits. The rest of the world, myself included, aren’t so lucky. Our languages are damn complicated, with additional letters, along with such grammatical ‘features’ such as accents to see where the pronunciation is at its strongest.

Seeing that I was born in Chicago, my first language is English. Although my parents are both from Puerto Rico, they never enforced the usage of Spanish upon me. When I moved to Puerto Rico when I was 8 years old, the only word in Spanish I knew was “Gracias”. Unfortunately, I didn’t have Dora The Explorer to help me learn the language when I was a boy. It’s taken years, but I finally consider myself fully bilingual in both English and Spanish, and am usually very careful as far as spelling and grammar go.

However, I’ve been wracking my brain for a long time now when making web applications in Spanish. Sometimes the additional characters, like the letter “ñ“, or words like “presentación” are a pain to get working immediately. It’s like I have to jump through hoops to get those characters working right.

In case anyone else shares my pain (or in case I forget in the next couple of months), I’ve compiled a short list with the things you should first look for when these Unicode characters are appearing incorrectly.

Don’t forget the <meta> tag

One common mistake web developers make is forgetting to set the default character set of the page they’re working on. Without this tag, the browser will automatically set the character set to the browser’s default, usually ISO-8859-1, which does not show Unicode characters. To set the character set of the page to UTF-8, which correctly displays Unicode characters, you simply need to add the following meta tag as the first line in the <head> section:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

This should enforce your browser to use UTF-8 when displaying the page’s characters. Remember to set this first, before any other tags in the head section, or else it won’t work at all.

Check your web server configuration

I remember once working with a PHP application, and pulling my hair out because the characters simply wouldn’t display correctly, no matter what I did. After hours of searching through Google, I found out my problem. The Apache Web Server, which was responsible for serving the website, had its default character set set to ISO-8859-1. If you control your own server and can change Apache’s configuration, just go to the configuration file named httpd.conf (this name can vary, depending on your Linux distribution) and make sure the AddDefaultCharset option is correctly set:

AddDefaultCharset UTF-8

After reloading the Apache Web Server, your pages should be displaying Unicode characters correctly. With the Lighttpd Web Server, which I’m using now, I haven’t had to set any option for correct Unicode support. However, in case someone needs it, just go to your Lighttpd configuration file, go to the mimetype.assign section, search for the .html assignment, and add the following at the end:

".html" => "text/html; charset=utf-8"

Another file to verify, although not necessary in most cases, is the PHP configuration file, named php.ini. PHP is responsible enough to use the encoding set in the page by the meta tag mentioned above, but sometimes some joker decides to change the default character set in the configuration file. In this case, simply comment out the default_charset option, and reload your web server.

The database has data too, you know…

With those two fixes above, your static text should be displaying correctly. However, you notice all Unicode characters stored and retrieved from the database are still being incorrectly displayed. This is due to your database character set not being set to UTF-8. In my app, I’m using MySQL, and the database server’s default character set is set to latin1_general_cl, which apparently doesn’t display Unicode at all. If you don’t explicitly indicate which character set you want to use for your database, the default will be used for not only the database, but the rest of the tables (unless explicitly defined, as well). What we want is the utf8_bin character set, which will display the Unicode correctly.

There are different ways to change this default behavior, from starting the database server with an option to change the default character set, to recompiling the entire program (providing it’s Open Source). But I find it much easier to just remember to make your database use the correct character set. In the MySQL prompt on the command line, it’s as simple as this:

CREATE DATABASE database_name CHARACTER SET utf8;

If you have an existing database not using the UTF-8 character set, the easiest way is to use a program like PHPMyAdmin for MySQL, or your preferred GUI for your database server, and change it there. You can also do it through the command line, but I won’t go into those details here. Search Google and you’ll get a ton of information.

Your text editor has a hand in this too

Don’t forget the tool you’re using to create your web pages. They could be the ones giving you major headaches. In my case, I’m testing out Intype, which is still in alpha, but very usable. Intype has the nasty habit (which I wish is fixed soon) to automatically set the file’s character set to ANSI by default. Once you save the file with this character set, it stays that way, wreaking havoc on what you want to see.

To fix this problem, just make sure your text editor, whether it’s Intype, e, Textmate, Vim or any other text editor in vogue right now, is saving your file using the adequate character set. In my case, I’m using UTF-8 Plain with Intype, and my characters are showing up correctly.

These tips should save you a ton of headaches down the road if you’re doing web development for a non-English audience. If you have any to add, feel free to do so.

OpenID - The login of the future?

Posted on August 18th, 2007 in Open Source, Programming | No Comments »

Following my small rant yesterday about not logging enough programming time, I cleared my schedule completely this afternoon and evening (with a rest or two to catch the awesome G4 show Code Monkeys) to catch up with some things. It also didn’t hurt that Hurricane Dean passed rather close to Puerto Rico, meaning that my plans were all washed out, but that’s okay, I don’t blame freak forces of nature for any of that.

The first thing I did was read some news around the ‘Net, as I usually do, and I saw a rather interesting article about OpenID, which piqued my curiosity. In case you don’t know, OpenID is (according to the main site) “an open, decentralized, free framework for user-centric digital identity”. In nicer terms, it’s basically a place where anyone can validate their identities in websites around the universe, by storing your allowed sites in a single place. This means you can be able to login any OpenID-enabled site by simply entering your own OpenID URL, logging into your OpenID account, allow the site, and that’s it. No more remembering multiple passwords ever again, only your OpenID URL and password.

At first, I thought this was simply a glorified password-saving system, or something similar to the ill-fated Microsoft Passport / Live system. I didn’t think it was going to be a great idea. But after reading more about it, I realized the true strength of this system over proprietary systems like Microsoft’s: It’s available for everyone who wants to use it. And I don’t mean that in the sense of creating accounts. I mean it in the sense that any developer can add OpenID functionality to their own web applications for free. That’s the beauty of the system.

Although very few sites I frequestly visit don’t have this functionality yet, I went ahead and created my own OpenID account over at MyOpenID. They’re one of the first OpenID providers, and they make the process rather easy. You simply go to their website, go through their short sign-up process, and once you have the account ready, you can go to any OpenID-enabled site (they also include a directory with sites that have this functionality enabled), and you’re ready to log into that site with the URL you created. It’s a simple, yet powerful tool.

The site I’m creating right now has a forum powered by the awesomely simple Beast forum app, and it has OpenID support right out of the box, thanks to the ruby-openid gem that you can easily install on your server. After installing the forum I’m going to test on my server, I immediately used my OpenID to create the account, and it just worked without a hitch. I’m sure everyone loves it when things work on the first try as intended.

If you haven’t read about OpenID, I suggest you click that link above to go to OpenID’s website, and read all you can about it. I believe this will cause an impact in the Open Source industry, and more applications will start to have this included. I know that whenever I can, I’ll be adding this functionality to my websites. I already found a tutorial that helps integrate OpenID functionality with the acts_as_authenticated plugin. I’ll be sure to look into that, and post my results of that experiment soon.

Where’s the balance with programming?

Posted on August 17th, 2007 in Programming | 1 Comment »

Well, it’s been week two with my nice little VPS, and it’s working like a charm. I’m loving this system. I’ve been playing around with it, tweaking it to squeeze a bit more out of the limited RAM I have, or just to learn new things I haven’t tried before, like setting up DNS (rather easy) or Sendmail (talk about a pain in the ass). It’s certainly been a blast setting up everything, and that’s probably the geekiest thing anyone could say.

In any case, I’ve been wanting to sit down all week and try to finish my Rails project that I have about 75 completed (although I’ve had so many new and cool ideas, I’m considering starting again from scratch), and I just haven’t been able to do it. The time is seriously lacking with my 8-to-5 job, and it’s frustrating. I want to learn so much, but can’t do much, other than read the latest news and that’s it.

I’m still trying to strike a balance between learning and getting some programming, and doing other things. Right now, I’ve been busy having fun with some of my co-workers, as well as some old friends I hadn’t seen in a while. If I wanted to learn programming, that means killing part - or all - of my social life. While I consider myself an introvert, I still enjoy the company of close friends, and I wouldn’t want to neglect them, as I have before in the past.

So right now, if I want to seriously start learning new things, I’m going to have to either cut ties with some friends and stop hanging out with them frequently, risking that they just ditch me whenever they go out, or just sleep less and code more. I know some people who can function well with just two or three hours of sleep. I’m not one of them. I need at least seven hours of sleep, or else I’ll most likely be moody the next day due to a nasty headache that just won’t quit.

I’m also afraid of getting into the habit of drinking highly-caffeinated drinks. I think I drink a lot more Red Bull than the average person, but I don’t do it every day to avoid creating a dependency on them. It happened once with Mountain Dew (where I drank two every morning before 9:00 AM, and once I stopped, the morning headaches started). It’s scary to think about creating an unhealthy habit, like any drug addict would.

So, how do people do it? I’m really curious. I want to create some study habits that will continue no matter what I want to learn, but I don’t want to sacrifice something that I know I will miss in the future (like my friends or my health). Does anyone have any tips on study habits? Does it cut into time you can spend with friends and family? Do you barely sleep? I hope I can get right on track in the upcoming months, and be the coding-maching I know I can be.

Site Redesign… With Wordpress?

Posted on August 8th, 2007 in Linux, Open Source | No Comments »

If you’ve been to this site before, you’ll notice a whole lot has changed since the past week. I decided to make some changes, not only to the site, but to the entire architecture running the site as well.

The first major change I made was to switch from a regular hosting service to a VPS. It’s made a world of difference. I don’t want to name my previous hosting company, since the service is pretty good, especially for what I paid for it (not too much). However, I felt it was a bit too slow for my tastes. Also, I’m sort of a control freak when it comes to controlling my work. So I scoured the web, and found a company that offers cheap VPS service, so I signed on.

While I do have system administration experience in Linux and am very comfortable working from the command line, it took a while for me to learn how to set up a domain to resolve in your own DNS, how to set up E-Mail accounts with different domains, and other random things. I’ve learned a lot this past week, and for now, my domains are working correctly. It’s fun when you have to bust your ass to get something working, and it finally does. The hard work makes you appreciate it more.

Since my VPS service is limited to 128 MB of RAM (this is just a test VPS to see how it’ll work for my needs, so maybe I’ll spring for more RAM later on), I immediately shut down the Apache Web Server that’s automatically run by default, and grabbed a copy of Lighttpd. After compiling it, learning how to configure it, and making it run, it’s also made a world of difference, as far as speed and ease of administration goes. Although it has some quirks to get some software working (especially Wordpress), it’s not to difficult to figure out on your own.

FInally, my old site ran on Rails. After running it here, I noticed that it consumed too much RAM for my tastes. So I set out on rewriting the page again using PHP, but saw that my Wordpress installation is more than adequate to run the few pages I’ve had for now. I was also convinced to use this because I fell in love with this theme, called Hemingway. So I decided to make it easy on myself and just drop everything in Wordpress. I know Wordpress is used for blogging, but for a simple site as I have now, it’s more than okay to have a few additional non-blog pages around. It just looks good and it was quick to do, and that’s what I wanted all along, since I need to focus my energy on another task that I’ve been doing for the past two weeks.

So, after this switch to a VPS and the redesign, I’m happy with what I have for now. Not only do I have a good-looking site, I not only learned a lot more about Linux, I also have a nice little virtual server running smoothly. Right now, I’m running the server with a web server (Lighttpd), mail server (Postfix + Dovecot), a database server (MySQL) and it’s only consuming 57 MB of RAM. Go Linux!

If you’ve been here before, or even if you haven’t, tell me what you think about the site now, about his it looks, how it runs, whatever you want to comment about. I’ll appreciate any feedback.