Smart internationalization for ASP.NET page
The benefit of this approach is that you don't have to clutter controllers and other classes with code to look up values from .resx files. You simply surround text in [[[triple brackets.]]] (The delimiter is configurable.) An HttpModule looks for a translation in your .po file to replace the delimited text. If a translation is found, the HttpModule substitutes the translation. If no translation is found, it removes the triple brackets and renders the page with the original untranslated text.
.po files are a standard format for supplying translations for applications, so there are a number of applications available for editing them. It's easy to send a .po file to a non-technical user so that they can add translations.
i18n.LocalizingModule to your <httpModules> or <modules> section.<!-- IIS 6 -->
<httpModules>
<add name="i18n.LocalizingModule" type="i18n.LocalizingModule, i18n" />
</httpModules>
<!-- IIS 7 -->
<system.webServer>
<modules>
<add name="i18n.LocalizingModule" type="i18n.LocalizingModule, i18n" />
</modules>
</system.webServer>
/locale/fr/.messages.po.messages.po file:#: Translation test
msgid "Hello, world!"
msgstr "Bonjour le monde!"
using System.Web.Mvc;
namespace I18nDemo.Controllers
{
public class DefaultController : Controller
{
public ActionResult Index()
{
// Text inside [[[triple brackets]]] must precisely match
// the msgid in your .po file.
return Content("[[[Hello, world!]]]");
}
}
}
/en/ in the URL with /fr/ (or whatever culture you've selected.) The page should now display the translated version of your text./default again. Observe that the URL is changed to reflect your alternate culture and the translated text appears.locale folder.<!-- IIS 6 -->
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
</system.web>
<!-- IIS 7 -->
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
</handlers>
</system.webServer>