The other day I had to work with some date formatting on iOS and Android and I end up using the great http://nsdateformatter.com website. I immediately missed the option to change the locale and seeing that it was open source I took my chance to contribute.
It was an interesting mini project, Swift in the server! How cool is that?!
The first challenge was to make the project compile and run. Having a project open sourced doesn’t mean that compiling it is easy. This is a thing that the people at Artsy do really well. In this case I was surprised how straightforward it can be thanks to Vagrant.
Vagrant allows you to describe in a file how the environment to run your project should look so you can reproduce it without effort in any machine. This kind of VM technologies are specially used with server applications to make sure that deploying and scaling up the app is easier. In my case it was the first time that I used this so it took me some trial and error to understand how it worked and troubleshoot some problems.
The main issue that I had is that the VirtualBox version that I installed had a different version of the Guest Additions and the VM didn’t have access to the host resources like shared folders and even internet. That made the installation script fail :(
Luckily I found a plugin for Vagrant that fixes it. After that the installation of Swift and the rest of dependencies just worked. As a tip, read more than once the
Vagrantfile to understand the settings of the box. I lost some time trying to look for the
/vagrant shared folder until I saw that it wasn’t using the default path because it was specified on the config file. Silly me.
When the Vagrant box was working and I had access to the project directory I was able to start messing around. There is a deploy.sh script that is used to deploy the project to Digital Ocean. I could ignore that as I just had to run the server locally but it was useful to check if there was special options for the project to run.
But easy enough, just using
swift build fetches all the dependencies and compiles the app. Or not.
There is (or was in the Swift version used) a bug with SPM and some libraries that makes the installation of dependencies fail if it finds a
Tests folder. Luckily a quick search gave me the solution. Just remove the Tests folder.
swift build again and... boom.
It started throwing errors at me but, thankfully, following the Swift development closely give me the chance to detect that all the errors were due to recent changes on the language. So I just had to look for which library was complaining and find the correct version.
swift build again and... it works!
The changes on the Swift part were straightforward. Just fetch the available locales and send them to the client. Then receive a new parameter and use that locale to format the date. Easy.
Working with the open source version of Foundation was fun. On the version used in the project
setLocalizedDateFormatFromTemplate was not implemented so I had to ged rid of the idea of using it, which was a shame because using format templates is when the full potential of the locales is uncovered.
Apart from that the formatting doesn’t seem to be complete. For example in Catalan the preposition “de” before the month is ignored. So instead of “d’Abril” it returns just “Abril”. Something that in the Apple version of Foundation works. Maybe is fixed in a most recent release. (I opened SR-1325)
The change was really simple and quick to do. From not knowing how Vagrant worked to having a PR ready. And if it weren’t for crazy pants things like the CSS not reloading it could have been an even better experience.
Still, being a simple change it served me to learn a new technology that will probably be useful on my next pet project. That’s more than good enough for me :D
PS: Ben has merged PR #4 and it’s now deployed! He also included the much needed improvements on the CSS changes that I made.