Laying the Foundation for i18n, Brick by Brick was originally published on February 07, 2007.


In Europe, they do things a little bit differently. From what I understand, it boils down to this: they work less and play more; when not working or playing, they drive tiny little cars. Apparently, they all speak different languages too.

Jannik works for a well-known, innovative company somewhere on the continent. Because of the multiple-language problem, his company translates their website into multiple languages. The way their URLs are formed, going to http://www.company.tld/eng/products and http://www.company.tld/deu/products displays the same content, except the former is in English and the latter in ... Deulish?

Needless to say, the function that pulls the three-letter language code out of the URL and validates it is called for every page view. Here's the optimized code. (For bonus points, identify the language.)

  
  /// <summary>
  /// A list of supported folder names
  /// </summary>
  public static string[] VALID_FOLDERS = new string[]
  {
    "eng", // English
    "deu", // German
    "fra", // French
    "jpn", // Japanese
    "kor", // Korean
    "dan", // Danish
    "fin", // Finnish
    "swe", // Swedish
    "nor", // Norwegian
    "dut", // Dutch
    "spa"  // Spanish
  };

  /// <summary>
  /// Returns the ISO language from URLs of the form "/lang/foo/bar.aspx".
  /// NOTE: This gets called for EVERY request so it's as optimal as possible!
  /// </summary>
  /// <param name="Url" />URL of the form "/lang/foo/bar.aspx"
  /// <returns></returns>
  public static string IsoFromUrl (string url)
  {
    foreach (string str in VALID_FOLDERS)
    {
      if (str[0] == url[1])
      {
        if (str[1] == url[2])
        {
          if (str[2] == url[3])
          {
            return str;
          }
        }
      }
    }
    return string.Empty;
  }

 

From Jannik, who didn't write the function: "The Summary says it all!"

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!