Skip to content
Cigano Morrison Mendez edited this page Jul 25, 2016 · 4 revisions

Welcome to the i18n wiki! The i18n library is designed to replace the use of .NET resources in favor of an easier, globally recognized standard for localizing ASP.NET-based web applications.

Platforms Supported

i18n itself targets .NET Framework 4, and works with websites and web applications based on ASP.NET v4 and above, including:

  • ASP.NET MVC
  • ASP.NET Web API
  • ASP.NET WebMatrix / Web Pages
  • ASP.NET Web Forms

Features

  • Leverages the GetText / PO ecosystem: localize like the big kids
  • Localize everything: HTML, Razor, C#, VB, JavaScript, .NET attributes and data annotations, ...
  • SEO-friendly: language selection varies the URL, and Content-Language is set appropriately
  • Automatic: no URL/routing changes required in the app
  • High performance, minimal overhead and minimal heap allocations
  • Unit testing support
  • Smart: knows when to hold them, fold them, walk away, or run, based on i18n best practices.

Installation

In your Web project, open Package Manager Console (View > Other Windows > Package Manager Console) and type:

    PM> Install-Package i18N

This will install i18n in your solution. You can use also the Manage NuGet Packages option to install i18n (right-click on your project > Manage NuGet Packages).

Configuration

Add the following into your web.config file:

<configuration>
  ...
  <appSettings>
    ...
    <add key="i18n.DirectoriesToScan" value=".." /> <!-- Rel to web.config file -->
    <add key="i18n.WhiteList" value="*.cs;*.cshtml;*.sitemap" />
    <add key="i18n.BlackList" value=".\js\kendo;.\js\angular" />
    <add key="i18n.AvailableLanguages" value="en-US;fr-FR;pt-BR" />
    ...
  </appSettings>
  ...
  <system.web>
    ...
    <httpModules>
      ...
      <add name="i18n.LocalizingModule" type="i18n.LocalizingModule, i18n" />
      ...
    </httpModules>
  ...
  </system.web>
  ...
  <system.webServer> <!-- IIS7 'Integrated Mode'-specific config -->
    ...
    <modules>
      ...
      <add name="i18n.LocalizingModule" type="i18n.LocalizingModule, i18n" />
      ...
    </modules>
    ...
  </system.webServer>
  ...
</configuration>

Note: The <system.web> element is added for completeness and may not be required.

Add the following to your Global.asax.cs file:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        // Change from the default of 'en'.
        i18n.LocalizedApplication.Current.DefaultLanguage = "pt";

        // Change from the of temporary redirects during URL localization
        i18n.LocalizedApplication.Current.PermanentRedirects = true;

        // This line can be used to disable URL Localization.
        //i18n.UrlLocalizer.UrlLocalizationScheme = i18n.UrlLocalizationScheme.Void;

        // Change the URL localization scheme from Scheme1.
        i18n.UrlLocalizer.UrlLocalizationScheme = i18n.UrlLocalizationScheme.Scheme2;

        // Blacklist certain URLs from being 'localized' via a callback.
        i18n.UrlLocalizer.IncomingUrlFilters += delegate (Uri url) {
            if (url.LocalPath.EndsWith("sitemap.xml", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            return true;
        };

        // Blacklist certain URLs from being translated using a regex pattern. The default setting is:
        i18n.LocalizedApplication.Current.UrlsToExcludeFromProcessing = new Regex(@"(?:\.(?:less|css)(?:\?|$))|(?i:i18nSkip|glimpse|trace|elmah)");

        // Whitelist content types to translate. The default setting is:
        i18n.LocalizedApplication.Current.ContentTypesToLocalize = new Regex(@"^(?:(?:(?:text|application)/(?:plain|html|xml|javascript|x-javascript|json|x-json))(?:\s*;.*)?)$");

        // Change the types of async postback blocks that are localized
        i18n.LocalizedApplication.Current.AsyncPostbackTypesToTranslate = "updatePanel,scriptStartupBlock,pageTitle";
    }
}
Clone this wiki locally