Quick and Simple Server SMTP

I have a number of servers that I look after in various places on the intertubes. I like to have things like MDADM (Linux software RAID manager) be able to mail me when the something goes wrong like a disk dies etc.

Some of these machines are in places without reliable SMTP servers for me to send mail through and I’ve tried running my own postfix and delivering the mail directly, but invariably I run into situations where the servers that I’m trying to deliver mail to don’t like DSL IPs… and not getting a mail about a dead disk is kinda a big issue.

I also don’t trust a lot of ISP’s SMTP, and some of my servers move around, so one day it’ll be behind a DSL IP and the next behind a Verizon IP (where it can’t talk to smtp.dslprovider.net etc).

My solution is quite simple, use google. (This guide is for Ubuntu but I’m sure you’ll figure it out with other distros)

  1. Create a gmail account for monitoring. I do this because I don’t want my gmail password floating around in plaintext on various machines.
  2. Install the ca-certificates package

    $ sudo aptitude install ca-certificates
    $ sudo update-ca-certificates

  3. Install msmtp

    $ sudo apt-get install msmtp

  4. Configure msmtp

    $ sudo vim /etc/msmtprc

    Set it to something like

    account gmail
    host smtp.gmail.com
    from myemailaddress@gmail.com
    auth on
    tls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt
    user notifyemailaddress@gmail.com
    password mys3cr3tp455w0rd
    port 587

    account default : gmail

  5. Create a sendmail simlink

    $ sudo ln -s /usr/bin/msmtp /usr/sbin/sendmail

  6. Run a test

    $ echo “This is a an awesome test email” | msmtp youremail@domain.com

  7. If you want mdadm to mail you when something goes wrong

    $ sudo vim /etc/mdadm/mdadm.conf

    and put your email address on the line that reads something like

    MAILADDR youremail@domain.com

  8. And then run a mdadm test by running

    $ sudo mdadm –monitor –scan –test –oneshot

  9. If everything is working according to plan you should receive an email. You can now rest assured that any future MDADM issues will get to you.

HTC Magic – Android Review

htc-magic-vodafoneI’ve been using an Android phone, the HTC Magic, for about two weeks and I think it’s time to report back.

Build Quality: The phone feels very good, it look slick (at first you’re surprised by how quickly it picks up fingerprints but that isn’t an issue after day 1). It isn’t as solid as an iPhone, but it doesn’t feel like it’ll break easily either.

Voice Quality: Better than my Nokia E61i, better or equal to all the phones I’ve had.

Screen: The screen is nice. The same resolution as the iPhone, just smaller (because the phone is smaller). It certainly is bright enough, I usually have the brightness set to 50%. The image quality is great, better than the iPhone IMHO.

On Screen Keyboard: This is the question most of my nerd friends ask first. When you first get the phone you feel like it’s going to be hard. Coming from the full qwerty on the E61i I was struggling to get up to pace. But it gets better, a lot better. After a while your brain figures out how to thumb the right keys and the Android’s brain figures out what you meant to type. This is like predictive text on steroids… if you’re typing a word but along the way hit a few wrong keys, the phone works out what words you could have meant to type with keys near the ones you pressed. It’s hard to explain but in practice it really does feel a little like magic. So overall the on screen keyboard is actually awesome.

Signal: So far I haven’t had any situations where I’d lost signal while others had signal. The wifi is MUCH better than my E61i.

Connections: The Magic just has one port, a mini usb port. Actually it’s slightly different to mini usb since mini usb fits it but it can also accommodate HTC’s proprietary connector for the headset. I would have loved a regular headphone jack, but they’re trying to keep it minimal. You can get an adaptor. The good news is that the phone charges over usb with a standard mini-usb cable. That in itself is f-ing awesome.

Battery Life: Okay, not great. Look, lets be honest, if you get one of these phones you’re always playing with it… that sucks power. I need to charge this phone every night in order to keep it happy, sometimes more regularly. Happily I usually just plug in into whichever computer I happen to be sitting at and let it charge that way. I’m sure that future androids and future OS improvements will have much better battery life.

General Experience: This phone KILLS anything I’ve ever used before because of the general experience. When you first boot the phone it asks you for a google profile and then from that moment on your phone is perfectly synced with Google Mail, Google Talk, Google Calendars etc. Mail arrives on your phone in seconds and an “@” sign is placed on the notifications bar. From any application you can drag the notification bar down and get a preview of the email and decide whether to read it then of come back to it later. SMS’s, Calendar events, App notifications, like Twitroid saying there are new tweets, etc are all handled by the notification bar.

Evolution: The most awesome thing about this phone is its position in the software evolution. Things are changing all the time. I’m currently running release 1.5 of the Android base system. Since I have already ROM’d and ROOTed my phone I will be trying out the HERO (HTC’s latest Android Phone) ROM, which, while still Android 1.5, comes with a bunch of interface improvements from HTC. Later on this year Google will release the Android Version 2 which no doubt come with a whole whack of ui improvements, stability, batter life etc. The key to me is survival of the fittest. Because the Android development team is far more open than anything else out there, there is a constant source of feedback into the community. I have a long list of suggested improvements that I’m collecting and will publish. It’s not unrealistic to think that the Android devs will read that list and maybe even comment on it.

Openness: Android lets you replace any part of the OS with a replacement part. What that means in general terms is that if I want to phone someone I use the Android default dialer app that ships with the phone. I can replace that app with one that queries an LDAP database over wifi to get all our staff phone numbers. Even the Home application (the one you see when you press the home button) is replaceable.

Just a little teaser... I've subsequently installed the Hero ROM... and it is beautiful.
Just a little teaser... I've subsequently installed the Hero ROM... and it is beautiful.

My Top 11 Android Apps

  1. Android Market – The marketplace has all the apps. Sure you can get a lot of the files in other places, but not as easily as using market
  2. Skymap – Using the compas and the tilt sensor this shows you the stars where the phone is “looking”. It really is quite amazing to see in action.
  3. Timeriffic – A very simple app but amazingly useful. It allows me to change the phone’s volume, brightness, ringtone etc based on certain times. This means my phone automatically gets louder at 8am and softer at 11pm.
  4. ACast – A podcast client in your phone. Can be configured to only download over wifi.
  5. TaskKiller Light – Simple app, lets you kill other apps.
  6. GPS Status – Shows you all the info it can get from the GPS, compass and tilt/motion sensors. ie. Gradient, G forces, GPS Accuracy, How many sattelites it’s using.
  7. CellFinder – Nerdy app, but cool. Shows you where the GSM tower is in relation to your GPS position and shows you how far away etc.
  8. Transdroid – Control your Bittorrent client from your phone.
  9. AndFTP – FTP and SFTP (ssh) client, on your phone. Awesome for grabbing stuff of the network. (Astro is a file browser that supports SMB too)
  10. Twitroid – Fully featured twitter client for your phone.
  11. Google Maps – While it doesn’t have turn based navigation for South Africa yet, it does have all the roads now and can show you your exact location with GPS. This is very cool.

Overall, this phone is amazing, mostly because of Android… I can’t wait to see where Android world goes next.

Rooting a Vodacom HTC Magic and upgrading the firmware in South Africa to get Android Market and more.

If you’re in South Africa and you own an HTC Magic Android phone you’ll soon realise that it’s missing Android Market, which to be honest, is pretty much what makes Android so damn amazing in the first place. In its place is the Leaf Open Market. Leaf are the South African HTC importers. Open Market is about as awesome as getting stabbed in the eye with a pencil, repeatedly. The application works okay, but the selection of applications is incredibly dismal.

Update 30/06/09: There are rumblings that Leaf is going to “release” Market in “two weeks”. Supposedly they’re going to be sending out instructions on how to ROM your phone or allowing you to take it into a vodacom store and getting it ROM’d there (most likely they’ll need to send it away). I have no idea how true these claims are. Maybe someone wants to phone Leaf and ask them?

Update 23/07/09: A really good resource for anyone looking to hack their Magic is the XDA-Developers Forum. Some kind souls have also recently added a Wiki specifically put together for hacking the Magic (Sapphire) which every one of you should read from start to finish.

Anyway, the primary reason I love Android is because it’s hackable. Commence hacking. Warning: Yes, this probably will void your warranty. Yes, it’s possible that the real Android Market will eventually be released in SA, but I’m not holding my breath.

Basically what we’re doing here is updating the phone’s firmware to the version HTC originally intended the Magic to ship with… There are stacks of other firmwares (called ROM’s in the ‘community’) floating around, I’m just pointing to one that definitely works. Using the wrong ROM can put your phone in a state where it refuses to boot… Never fear, see below ‘Recovering from a bricked Magic‘.

General Steps

I’m going to explain the general steps you go through first… This will help you understand what it is you’re about to do so that you aren’t just blindly following instructions.

  1. We get the Android SDK and Fastboot applications installed on our computer
  2. We copy the ROM we want to install onto the SD Card and rename it ‘update.zip’
  3. We boot the phone into FASTBOOT mode.
  4. Using the FASTBOOT program we temporarily upload and ‘install’ the daldroid-recovery.img ROM over USB. (This is a special boot loader app that lets us do cool stuff like made Nandroid backups and install that update.zip ROM from the SD Card.)
  5. The phone boots into the daldroid-recovery mode.
  6. We backup our original ROM to the SD Card using Nandoid, clear some settings and then install the new ROM.
  7. Congratulations, you have a rooted, real android phone 😉

Step by Step Instructions

Okay, now that you know the basic steps, here are the details.

  1. Enable USB debugging on the device in Settings->Applications->Development->”USB Debugging”
  2. Ensure you have the Android SDK downloaded, and the included USB driver installed correctly on your desktop.
  3. Download Fastboot. Since I use linux I just copied the ‘fastboot’ binary to the Android SDK’s tools directory and made it executable.
  4. Open up a terminal/dos prompt and “cd” to the tools directory in your Android SDK folder.
  5. Plug in your USB Cable.
  6. Run the following command:
    adb devices

    Linux users will need to run:

    sudo ./adb devices
  7. You should see your Android phone’s serial in the list. If you get an empty list, you need to sort out your USB driver.
  8. Download Daldroid’s Rooted (adb shell) Original HTC ROM for Magic and Daldroid’s Recovery Image
  9. Unzip the daldroid-recovery.zip file and put it in the Android SDK’s tools directory.
  10. Connect the phone with the USB cable and from the notifications bar, mount the SD card. Rename daldroid-htcmagic1-signed.zip to update.zip and copy it to the phone’s SD card.
  11. On your device, enter the bootloader FASTBOOT mode by turning off the phone and then, while it is off, pressing the volume down button while pressing the power button.
  12. You should get a screen with some androids on skateboards at the bottom…Give it a second to run whatever tests it runs.
  13. Pressing the back button will get you into FASTBOOT MODE and you should see the screen change to say ‘FASTBOOT USB’
  14. Type the following commands on your computer (not on the device):
    fastboot boot daldroid-recovery.img

    Again, Linux users will need to run:

    sudo ./fastboot boot daldroid-recovery.img
  15. This will upload (over usb) a mini recovery image and reboot your phone… It’ll take a few seconds and you’ll be prompted with the recovery interface.
  16. Select ‘Nandroid Backup 2.1’ from the menu. This will write a backup of your existing system to the SD card… useful in case something goes wrong.
  17. Select ‘Wipe Data/factory reset’. This erases settings etc that might not be compatible with the new firmware
  18. Select ‘Apply sdcard:update.zip’. This essentially installs the system image from the update.zip file.
  19. Select ‘reboot system now’.
  20. Wait a long time for the first initialization. Congratulations, you have a real, rooted, Android phone with Android Market and thousands of quality apps to install.

The really nice thing about the Daldroid image is that it comes with all the APN settings etc required for Vodacom so your HSDPA will work straight away.

Recovering from a ‘Bricked’ Magic

I promised earlier I would tell you how to recover from a ‘Bricked’ Android phone… I’ve put ‘bricked’ in quotes because a lot of people seem to be thinking they’ve bricked their phones when in reality it’s quite easy to recover.

  1. Check if you can get to into Fastboot Mode:
    1. Remove the battery.
    2. Wait 10 seconds (or more)
    3. While pressing the volume down button, pop the battery back in.
    4. If it doesn’t turn on by itself, keep pressing the volume down button and press the power button.
    5. You should get the Fastboot menu.
    6. If you don’t get into Fastboot mode, try a few more times and then panic. I don’t know how to help you. Try Google.
  2. You now need to copy the right update.zip (see step 8 above) onto your SD Card. There are two ways:
    1. Get a usb card reader or use another cell phone that will allow you to mount the SD Card over USB and just copy it.
    2. or, the slightly fancier way, while your phone is in Fastboot mode run the following command on your computer (first put the new update.zip in your Android SDK tools directory):
      adb push update.zip /sdcard/update.zip

      Again, Linux users will need to run:

      sudo ./adb push update.zip /sdcard/update.zip

      This uploads the file directly onto the SD Card via USB. It takes a minute or two so do be patient.

  3. Now you just need to follow steps 11 though 20 from the instructions above and you should be golden.

Updated (21 June 2009) – Putting the original Vodacom ROM back

If for some or other reason you want to go back to the old version of the Vodacom ROM, you can do so quite easily.

Nandroid writes a selection of files to the SD Card:

2.5M  boot.img
339K  cache.img
60M   data.img
256K  misc.img
265    nandroid.md5
5.0M  recovery.img
78M   system.img

You should really make a copy of them on your local machine just in case. Once you’ve got those files on your local machine you can restore your phone using fastboot like so:

  1. Copy the nandroid files (*.img) from your backup into your SDK Tools Directory.
  2. Boot your phone into Fastboot mode (Steps 11, 12, 13) from the Step by Step guide above.
  3. Run the following commands from your computer:
    fastboot erase system -w
    fastboot erase boot
    fastboot flash system system.img
    fastboot flash userdata data.img
    fastboot flash boot boot.img
    fastboot reboot
  4. Congratulations, you now have your old Vodacom phone back. :/

Hope this helps, feel free to ask questions.

The joys of a non-tech girlfriend.

Lynnae, as you may know, is a food nerd. She teaches me stuff about how broccoli is from the Brassica family and therefore not suitable for stocks if you want a clear stock and that toast smells nice because of the Maillard reaction. So she’s a nerd, but she’s definitely not a computer nerd and it’s kinda funny when she’s trying to understand what I do all day or tell me about some or other computer problem she has at work. She has a windows box and a mac… no prizes for guessing which one “flashes on the one window and then the other one goes orange and starts flashing too and then you click on the tab for the first one and it starts flashing too so I had to work the whole day while the screen flashed at me“.

The real gems seem to pop out of nowhere:

Who pays for the network waves?
– Asked while I was trying to explain the concept of a wug.

Maybe it’s a glitch.
– Pretty much anything that goes wrong is “possibly a glitch”.

Something with a megabyte.
– When asked what size the SD card in her phone was.

What’s an aggregator, is it an angry alligator?
– Ok, admittedly she’s trying to be funny.

But she’s learning… We have a shopping list wiki, which she thinks is arbiwikiwiki.com (or something) but she runs firefox and is starting to understand why open source is better… that, and she’s the most awesome friend I’ve ever had, so I’ll tolerate her noobness.

Firefox 3 is almost upon us…

The latest in a long line of awesomeness and yet another brilliant example of Open Source kicking proprietary ass, Firefox 3 is coming out later today (at 7pm for those in the plus 2 timezone). In an attempt to get more rah rah, whizbang, Mozilla has put together a little Guinness book of records attempt to set the world record for the biggest number of software downloads in a day. I have no idea what the previous record was for, especially since Britney’s Limo Flash pictures can’t be considered “software”.

Anyways, hop along here and get it later on tonight. I will also be drinking that last Guinness in the fridge as part of my own personal celebration. Lynnae, you can have some too as long as you promise to upgrade at work tomorrow.

Download Day 2008

5 Reasons Android will kill the iPhone (or Assimilate it)

Firstly let me just say that it’s pretty clear that Apple will sell about a gajillion iPhones in the next few years. I’m not debating that. What I am debating though is whether Apple’s stranglehold on the “actually practical and cool smart phone” market will live forever more.

Quite to the contrary, I predict that one day, Apple will switch to using Android… like they switched to using Intel when the realised that the Intel chips were unquestionably better than the PowerPC chips and they were bound to lose market share if they didn’t jump ship… and JUST like they switched to a unix kernel when they realised how crappy their own one was.

That’s right, I said it.

1. Open Source

Open Source will win any programming battle, eventually. Open Source doesn’t mean a bunch of long haired, unwashed hippies sitting in their basements coding up the next version of sendmail… These days it’s some of the worlds finest developers working at Google and a plethora of the rest of the world’s finest developers working at the various handset manufacturers. All of them with a single goal in life. To make the best mobile platform ever.

2. Innovation.

Android brings the promise of a truly open platform. Apple doesn’t think this way. They like to limit, enclose, encapsulate and encase anything they possibly can. While Apple’s approach has historically worked for them, the sheer innovative power of the masses will mean that Android phones will be doing things that will make Steve Jobs simultaneously cringe and salivate with jealousy.

Innovation doesn’t just mean software innovation. Personally I like the idea of a querty keyboard on my phone. I find it easier to work with than the iPhone interface, but if there’s one thing we know about Steve Jobs it’s that he hates buttons. For the most part (ipod etc) Steve’s button hating ways are correct, but there are 6 billion different ways people will be wanting to use their phones. Steve wants the world to be all be like him… Not all of us are.

3. Open Markets

Probably the most powerful force in the upcoming battle for smartphone supremacy is who can knock out good quality, powerful phones at affordable prices. The iPhone does not cost $199 dollars. It costs a hell of a lot more, they’re just letting you pay it off over 2 years. In Europe an unsubsidised iPhone 3G is going to start selling at 499 Euros… that’s $769 when you convert it back to dollars. That’s almost 4 times the subsedised price, which means that even if you factor in the fact that Apple products cost more in Europe than they do in the US, the real cost of the iPhone is still nowhere near $199. This is the era of EEE PCs

4. Greed.

Android is not greedy. Apple is. Apple specifically excludes functionality on the iPhone in order to increase the amount of money you spend with your carrier. For example, there is not SIP (or VOIP) client on the iPhone so you’re “forced” to pay your carrier’s voice rates rather than being able to make the call via SIP over a wifi link.

Android will have no vested interests and will be available to all manufacturers for free. This will mean that Android phones will have all kinds of cool functionality built into them that Apple, for sheer economic reasons, will resist putting in the iPhone.

The other interesting greed factor is what I like to call the “Windows Vista” shuffle. The idea is that everyone upgrades because there are all these cool new features you just *have to have*. The more we learn about Windows Vista the more we realise how blatantly it was an attempt from Microsoft to convert all those millions of 8 year old Windows XP owners into fresh revenue. Open Source’s approach has always been to squeeze every last ounce of performance out hardware and to support that hardware for as long as possible. This means you only need to upgrade when you really want or need to… not when someone else decides they want your money.

Greed is also the reason that Steve Job’s version of “worldwide” is actually only 30 countries… Every time Apple wants to start selling the iPhone in a particular country they have to go through a process of trying to find a mobile carrier in that country willing to sell their souls and rip off it’s customers. (Aapprently this isn’t hard but it does take time)

5. Google

Google has a lot of money and they’re on a mission to change the world. From search engines to Solar Panels, they’re trying their best to make the world a better place for as long as they have the power to do so. Whether you love them or hate them they have a track record of rocking the boat and Android might just be the depth charge that roundhouse kicks the iPhone into a brick wall.

As for the assimilation… lets just say that I wont be suprised if I one day in the not too distant future get to read “iPhone Touch – Now Powered By Android”

J.

Blog title goes here

So much going on, so little time to blog about it.

  • Going away this weekend… super stoked about that.
  • HPT is moving along slowly. A comrade set up a bazaar repository which we’re now using to share code. Probably going to sprint a large portion of it out next weekend. If you’re a python coder, know stuff about making facebook apps and are looking for something fun to do let me know.
  • Uploaded my sailing pics a few days ago…
  • Because everyone seems to think that other people care about the slides they used for a presentation, here are my slides on the talk I gave at the last GeekDinner called “Five ways to live like a Capetonian” [pdf]
  • If anyone knows any good way to repeatedly mount SMB or NFS shares in OSX (after reboot) please let me know.
  • My site will be moving to a new box in Europre soon… ZA bandwidth costs are a pain.
  • My friend rolled his had his landy rolled by someone else over the weekend but has yet to give me any more details other than a link to the pictures… Which means he’s probably pissed off with himself and doesn’t want to talk about it waiting to calm down before talking about it. Eish.
  • Started using Alarm Clock 2 to wake me up in the morning. It’s a neat little app because it can wake up your macbook and does the whole fade in the music thing. Funny how the commercial 50MB competitor doesn’t seem to allow me to set up a repeat alarm or specify which days of the week I want it to run on.
  • “Installed” a UPS on my friends office server a few days ago. Ubuntu detected it and Gnome Power Manager let me set what I wanted to happen when… without installing anything. Toit.
  • I tend to spend more time in my virtual Ubuntu on my macbook than I do in OSX. Attempted to install ubuntu natively over the weekend but it was such a ball-ache I stopped. Dear Lazy-Web, please make an Ubuntu distro tailored for MacBooks.

kthanksbye.

Converting to FUPPES

I have a dirty little secret.

I have an XBOX 360… yes, a Microsoft product in my house!

Anyways, in order to play movies which are stored on my fileserver, on my xbox, over the network, I needed to run what is called a UPnP server which shares all the files. However, Microsoft being Microsoft, decided to slightly pervert the UPnP protocol and only support their own special version. ie. They want you to run a windows computer to stream the files from.

I don’t do windows… but months ago, in desperation, I installed TVersity (a windows program) on my VMWared XP box that I use for website testing in Internet Explorer.

TVersity, like a lot of windows software was buggy and bloated and did stuff I didn’t give it permission to do, like download 9 gigs of podcasts and free online movies for me. Anywhere else in the world that would just be annoying. Here it’s R600 worth of bandwidth you never realised you had used.

Anyway. There are basically 2 open source equivalents. I’d tried both before upgrading to Ubuntu Gutsy and both gave me issues when I didn’t have time for issues so I gave up.

Recently I had time to spare (a rarity) so I decided to tackle them.

First up was uShare. A great little app which installed relatively easily on gutsy. uShare’s flaw though is that it doesn’t do transcoding so you can’t watch movies that aren’t in one of the formats that the xbox understands AND it doesn’t support virtual folders based on mp3 tags.

Next up was Fuppes, (Free UPnP Entertainment Service) which can be a tricky install but once I had it installed and had correctly configured my virtual folder markup syntax it worked flawlessly… and it’s written well and can be quite fascinating to see in action because it exposes the UPnp protocol allowing you to see what’s going on behind the scenes. Being a nerd that’s a bonus.

The other problem with TVersity is that it used to crash every night. (remember, it’s Windows software)

Fuppes has been running for a few days now and is working flawlessly.

VIVA OPEN SOURCE!

L10n, gettext, php5 and Afrikaans

I’ve been playing with the concept of creating a multilingual site and after polling the experts I started toying with php’s implementation of gettext. What a ball-ache.

Ok, so some things you need to know before we start.

  • L10n stands for Localisation or Localization… the abbreviation is therefore obvious due to the words length and different spellings.
  • gettext is a GNU standard. I shouldn’t need to explain why standards are cool, but needless to say, there are plenty of tools to make using gettext a lot easier than a “roll-your-own” solution.
  • The PHP implementation of gettext is good when it’s working. But when it’s not working it’s like a scorned girlfriend — it will *not* tell you what is wrong. You have to figure it out yourself.
  • Your solution might not be *exactly* the same as mine because gettext relies on system locales, which in turn are structured differently on pretty much every linux distribution… having said that I’m sure the stuff you’ll read here will get you going.

Firstly, why gettext? Besides the fact that it’s a standard, gettext is good because:

  • gettext’s database files (.mo) are indexed and compiled.
  • PHP’s implementation is written in C or C++ by programmers who are probably better than you and I at writing efficient searches.
  • The gettext domain (your strings) are cached by the implementation so it really is quite fast.

Ok, so let me run through quickly how it’s meant to work, starting with some code

<?
setlocale(LC_ALL, 'af_ZA');
bindtextdomain('messages', '/var/www/myapp/locale');
textdomain('messages');
echo gettext("Hello World!");
?>

Line for line:

  • setlocale tells php which locale to use; in this example I’m using af_ZA (Afrikaans)
  • bindtextdomain tells php which domain to look for and where your gettext locale folders are. A “domain” is really just a collection. In this case we’re arbitrarily calling it “messages”.
  • textdomain is telling php which domain to use from now on. (seems redundant I know but I assume you might be able to bind multiple text domains)
  • gettext will look to see if it can find a translation for the locale you set earlier (af_ZA) for this index. gettext’s index is the original string in the original language. In this case we originally used the phrase “Hello World!”. If the locale can’t be found or there isn’t a translation for this index in the locale, gettext will return the original language — in this case “Hello World!”.
  • What is really important to note at this point is that there are a million or so variations of this code on the Internet. This is the stripped down version that works fine under PHP5 and Apache2.

Next we have to create our locale directory structure. This is what it looks like:

/locale
    /af_ZA
        /LC_MESSAGES
            messages.po (You'll create these later)
            messages.mo (You'll create these later)

Obviously you would create one per language you are wanting to support. I therefore have one for en_ZA and one for af_ZA.

Next we need to create our .po file. The .po file is the unindexed, uncompiled “language” file. Basically it has human readable plaintext in it. If you had a hoard of translators working for you you would send them your .po file/s, which they would add their translations to and then send back.

The important stuff in a .po file is:

msgid "Hello World!"
msgstr ""

Now, the cool thing about using gettext is that there are tools to generate a messages.po file from .php files automatically. The following command will scan all php files looking for references to gettext and will generate messages.po file for you.

xgettext -n *.php

The next step is to copy your messages.po file into your LC_MESSAGES folders. Once you’ve copied them you can make the change to the af_ZA one.

msgid "Hello World!"
msgstr "Hello Wêreld!"

Next you need to compile both your messages.po files by running the following command in the respective directories. This command will output a binary, indexed messages.mo file.

msgfmt messages.po

Once you’ve done that you should be able to rerun your code and it should give you the afrikaans version… although it won’t because here’s what you don’t get told. gettext will only work for locales it recognises, and even though you selected South Africa when you installed your ubuntu, it still doesn’t know what Afrikaans is. Enter the myriad of confusion solved, as usual, by one simple command.

locale-gen af_ZA

This will, if you’re on a newish debian-ish box, enable the af_ZA locale and then, after restarting Apache (remember it caches), your app should be speaking Afrikaans fluently.

Frugal Fennel

GeekDinner Time.

Ok, so if you’ve never been to a GeekDinner, why not make 2008 the year when you start going. It’s hugely fun and can possibly maybe be a little bit educational… Did I mention that at the last GeekDinner we discussed what to do if the Queen of England happens to start flirting with you on Facebook?

31 January 2008, Sloppy Sams, 51a Somerset Road, Greenpoint. 19:00ish

It’s open to everyone, all ages, all genders and all levels of technical innefficiency.

As usual, sign up on the wiki.