Saturday, May 3, 2008

Globalization Tips for Web Programmers (ASP.NET)

The most important thing when you develop an ASP.NET website which interface language is portuguese, is to use the following Web.Config entry:
<globalization uiculture="pt-BR" culture="pt-BR" fileencoding="iso-8859-1" responseencoding="utf-8" requestencoding="utf-8">
The newbiest programmers usualy forget the that his application users could insert special characters into a form. When sending parameter via QueryString the browsers always convert those special characters in an encoded form via one of these functions below:
  1. escape(), example: @ is escaped to %7E
  2. encodeURI(), example: % is encoded to %25
  3. encodeURIComponent(), example: @ is encoded to %40
I strongly recommend to use explicitly EncodeURIComponent() when passing parameters via URL. If you do as I say, the ASP.NET will implictly decode it when you call:
Request.Params["parameter's name"];
But if you forget the explict encode, none of the functions below will be able to retrieve correctly those special chars:
  1. Microsoft.JScript.GlobalObject.unescape(), complemented by escape()
  2. Microsoft.JScript.GlobalObject.decodeURI(), complemented by encodeURI()
  3. Microsoft.JScript.GlobalObject.decodeURIComponent(), complemented by encodeURIComponent()
  4. Server.UrlDecode(), complemented by Server.UrlEncode()
Nota: In order to use Microsoft.JScript at MS Visual Studio, you must manually Add Reference.

Javascript (and also JScript) also has
escape(), encodeURI() and encodeURIComponent() and their complementary functions, that have a slightly different results from ASP.NET ones.

No comments: