Santiago Palladino

Santiago Palladino

Santiago, also known as Palla, is a highly pragmatic dev lead, perfectionist and efficient, motivated mostly by back-end challenges. In Computer Science at UBA, he is known for being a hard TA to his numerical methods students. His preferred topic, and the subject of his master thesis, is linear programming and operations research.

Thesis in Computer Science

After a long time, I have finally finished my thesis to get the MSc in Computer Science degree, from FCEN UBA. It has been almost a year since I started working in the thesis in the context of Manas Research, and before that I had been working on it for over another year within the computer science department.

The text of the thesis, called A branch and cut algorithm for the Partitioned Coloring Problem, can be downloaded in English here. The slides (in Spanish) that I’ll be using tomorrow to present the thesis are also available here.

A sincere thank you to everyone who helped me during all this time with this work, and to Manas for sponsoring it!

Careful when truncating strings

One of our Rails applications has to consume an RSS. Nothing fancy here, we simply wanted to extract some fields from each item and store them in the PostgreSQL DB (app was hosted on heroku).

Simply slicing the string seemed to work at first:

summary = entry.summary[0...255]

But soon we started obtaining PGError (incomplete multibyte character) when trying to persist our records in the DB. It seemed that the summary we were obtaining had a multibyte char in that position, and slicing the string seemed to slice the char itself.

Luckily, we were working on rails, and there is a helpful TextHelper truncate function to perform precisely the task we wanted.

include ActionView::Helpers::TextHelper
summary = truncate(entry.summary, :length => 255)

As a side note, trying to save the same sliced string in MySql, produced no errors whatsoever. Apparently, PostgreSQL is stricter than its counterpart.

Translating route names in ASP NET MVC

One of the web applications we develop has support for multiple languages, and we got the request to have also the route names translated. As such, a user visiting the site in english and navigating to the stores page should be redirected to example.com/stores, whereas a spanish customer should see example.com/comercios in the URL bar.

The main challenge we faced was how to update every ActionLink invocation to reflect the current user’s language. This would require a huge amount of refactoring throughout the application. Luckily, the ActionLink method (as well as Url.Action and any other route-related method) make use of not only the RouteCollection defined in the MvcApplication, but also of their defined constraints.

For those of you not familiar with them, constraints are additional conditions evaluated when the engine determines the current route based on the URL. They can be plain strings, representing regex patterns:

routes.MapRoute(
    "Product",
    "Product/{productId}",
    new {controller="Product", action="Details"},
    new {productId = @"d+" }
 );

Or even custom classes implementing IRouteConstraint. This allows you to specify any condition to match a specific route, such as the current language:

    public class LanguageRouteConstraint : IRouteConstraint
    {
        public string Language { get; set; }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return CurrentLanguage == this.Language;
        }
    }

(you may use whatever method you please to check the current language, such as checking the thread’s CurrentUICulture; you may also want to implement a slightly better way to compare different cultures, such as one supporting fallbacks to neutral cultures.)

And now we can use this class to constraint the routes for different languages when building the route collection in the RegisterRoutes method:

routes.MapRoute("stores-en", "stores", new { controller = "Stores", action = "Index" }, new { lang = new LanguageRouteConstraint { Language = "en" }});
routes.MapRoute("stores-es", "comercios", new { controller = "Stores", action = "Index" }, new { lang = new LanguageRouteConstraint { Language = "es" }});

Why is this useful? So far, what we have only achieved seems to be forbidding a spanish user from accessing the /stores route.

But as we said, all MVC methods that resolve the URL from the action/controller pair, such as Url.Action, make use of these constraints as well. Therefore, when rendering the page for a spanish user, an invocation to:

Url.Action("Index", "Stores")

Will yield /comercios as we wanted, without needing to modify all of our aspx code to adapt to the new requirement.

Note that if you want to keep english (neutral) names accessible for all languages, you simply have to add them at the bottom without any constraint:

routes.MapRoute("stores-es", "comercios", new { controller = "Stores", action = "Index" }, new { lang = new LanguageRouteConstraint { Language = "es" }});
routes.MapRoute("stores", "stores", new { controller = "Stores", action = "Index" });

Since the route lookup is done in order, returning the first one that matches, a spanish user will first match the first route, correctly returning /comercios, whereas any user will match the last one. This will allow a spanish user navigating to /stores to actually view the page.

After a while, with some helper methods for more declarative code, we can end up writing our routing configuration like this:

routes.MapInternationalizedRoute("stores", new { controller = "Stores", action = "Index" })
      .MapLanguage("comercios", "es")
      .MapLanguage("magasines", "fr")
      .Default("stores")

Gettext C# Utilities

The Gettext C# Utilities GNU project, containing convenient classes, templates, scripts and tools for easily implementing gettext i18n support for C# and ASP NET applications, has been polished up and version 1.0 is ready for download.

I’ve written walkthroughs in the wiki pages detailing how to add gettext support for a simple console project, using a database as a resource storage instead of po files, and adding supoort to an ASP NET MVC application.

I’ll try to add a few more examples and documentation in the following days if possible, but meanwhile all feedback is appreciated!