<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Alebo's World</title>
	<atom:link href="http://www.alebo.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alebo.de</link>
	<description></description>
	<pubDate>Thu, 27 Aug 2009 20:59:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET MVC URLs localization example</title>
		<link>http://www.alebo.de/2009/08/aspnet-mvc-urls-localization-example/</link>
		<comments>http://www.alebo.de/2009/08/aspnet-mvc-urls-localization-example/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 20:56:00 +0000</pubDate>
		<dc:creator>Alebo</dc:creator>
		
		<category><![CDATA[Allgemein]]></category>

		<category><![CDATA[ASP.Net MVC]]></category>

		<category><![CDATA[ASP.NET Routing]]></category>

		<guid isPermaLink="false">http://www.alebo.de/?p=86</guid>
		<description><![CDATA[I have been asked to provide a working example of the technics I showed in my last two posts.
This example is about localization with the ASP.NET MVC framework using routing, a custom MvcRouteHandler, an IRouteConstraint and cultures. It demonstrates an approach how to work with URLs like mysite.com/en-US/Search.
Post #1
Post #2
Download
]]></description>
			<content:encoded><![CDATA[<p>I have been asked to provide a working example of the technics I showed in my last two posts.<br />
This example is about localization with the ASP.NET MVC framework using routing, a custom MvcRouteHandler, an IRouteConstraint and cultures. It demonstrates an approach how to work with URLs like mysite.com/en-US/Search.</p>
<p><a href="http://www.alebo.de/2009/03/localization-with-aspnet-mvc-using-routing/" onclick="">Post #1</a><br />
<a href="http://www.alebo.de/2009/04/localization-of-urls-using-aspnet-mvc-routing-and-constraints/" onclick="">Post #2</a></p>
<p><a href="http://www.alebo.de/downloads/ASP.NET MVC URL localization example.rar" onclick="">Download</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alebo.de/2009/08/aspnet-mvc-urls-localization-example/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Localization of URLs using ASP.NET MVC, Routing &amp; Constraints</title>
		<link>http://www.alebo.de/2009/04/localization-of-urls-using-aspnet-mvc-routing-and-constraints/</link>
		<comments>http://www.alebo.de/2009/04/localization-of-urls-using-aspnet-mvc-routing-and-constraints/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 19:26:28 +0000</pubDate>
		<dc:creator>Alebo</dc:creator>
		
		<category><![CDATA[Allgemein]]></category>

		<guid isPermaLink="false">http://www.alebo.de/?p=66</guid>
		<description><![CDATA[Developing a multilingual website, besides localization of the page&#8217;s content it also makes sense to localize the URLs. In my first post dealing with localization I explained my basic approach how to localize pages using a derived MvcRouteHandler and a subtype of Route. In this post I am going to show a possibility to localize [...]]]></description>
			<content:encoded><![CDATA[<p>Developing a multilingual website, besides localization of the page&#8217;s content it also makes sense to localize the URLs. <a href="http://www.alebo.de/2009/03/localization-with-aspnet-mvc-using-routing/" onclick="" target="_blank">In my first post</a> dealing with localization I explained my basic approach how to localize pages using a derived MvcRouteHandler and a subtype of Route. In this post I am going to show a possibility to localize URLs by using routes and constraints.</p>
<h3>What I want</h3>
<p>1) I am going to have a search on my new site. I want this search page to have nice localized URLs for the different languages. The term &#8220;search&#8221; of the URL should be translated to each supported language.</p>
<p>Examples:</p>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="127" valign="top">USA</td>
<td width="273" valign="top">mysite.com/en-US/Search</td>
</tr>
<tr>
<td width="127" valign="top">Great Britain</td>
<td width="273" valign="top">mysite.com/en-GB/Search</td>
</tr>
<tr>
<td width="127" valign="top">Germany</td>
<td width="273" valign="top">mysite.com/de-DE/Suche</td>
</tr>
<tr>
<td width="127" valign="top">Switzerland</td>
<td width="273" valign="top">mysite.com/de-CH/Suche</td>
</tr>
<tr>
<td width="127" valign="top">Italy</td>
<td width="273" valign="top">mysite.com/it-IT/Ricerca</td>
</tr>
</tbody>
</table>
<p>2) Furthermore I want to have URLs with the same ending for different languages. This ist usefull for URLs, which do not have to be translated.</p>
<p>Examples:</p>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="127" valign="top">USA</td>
<td width="273" valign="top">mysite.com/en-US/FAQ</td>
</tr>
<tr>
<td width="127" valign="top">Great Britain</td>
<td width="273" valign="top">mysite.com/en-GB/FAQ</td>
</tr>
<tr>
<td width="127" valign="top">Germany</td>
<td width="273" valign="top">mysite.com/de-DE/FAQ</td>
</tr>
<tr>
<td width="127" valign="top">Switzerland</td>
<td width="273" valign="top">mysite.com/de-CH/FAQ</td>
</tr>
<tr>
<td width="127" valign="top">Italy</td>
<td width="273" valign="top">mysite.com/it-IT/FAQ</td>
</tr>
</tbody>
</table>
<h3>Approach</h3>
<p>1) This can be done by registering different routes and using custom constraints. I did it like this:</p>
<pre name="code" class="csharp">

public static void RegisterRoutes(RouteCollection routes)
{
    LanguageConstraint enLanguageConstraint = new LanguageConstraint(&quot;en&quot;);
    LanguageConstraint deLanguageConstraint = new LanguageConstraint(&quot;de&quot;);

    routes.IgnoreRoute(&quot;{resource}.axd/{*pathInfo}&quot;);

    routes.Add(
        new MultiLingualRoute(&quot;FAQ&quot;,
        new RouteValueDictionary(new { controller = &quot;CMS&quot;, action = &quot;Show&quot;, Id = 1 }),
        new RouteValueDictionary(new { validCountry = new CulturesConstraint() }),
        new MvcApplication1.MultiLingualMvcRouteHandler())
    );

    routes.Add(
        new MultiLingualRoute(&quot;Suche&quot;,
        new RouteValueDictionary(new { controller = &quot;Search&quot;, action = &quot;AdvancedSearch&quot; }),
        new RouteValueDictionary(new { validLanguage = deLanguageConstraint }),
        new MvcApplication1.MultiLingualMvcRouteHandler())
    );

    routes.Add(
        new MultiLingualRoute(&quot;Search&quot;,
        new RouteValueDictionary(new { controller = &quot;Search&quot;, action = &quot;AdvancedSearch&quot; }),
        new RouteValueDictionary(new { validLanguage = enLanguageConstraint }),
        new MvcApplication1.MultiLingualMvcRouteHandler())
    );

    routes.MapRoute(
        &quot;Default&quot;,                                              // Route name
        &quot;{controller}/{action}/{id}&quot;,                           // URL with parameters
        new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = &quot;&quot; }  // Parameter defaults
    );            

    foreach (Route r in routes)
    {
        if (r.GetType() == typeof(MultiLingualRoute))
        {
            r.Url = &quot;{culture}/&quot; + r.Url;
            r.Defaults.Add(&quot;culture&quot;, &quot;en-US&quot;);
        }
    }
}
</pre>
<p>At the first lines two LanguageConstraints are created. These constraints make sure, that this route is only used, if the given language is used.</p>
<pre name="code" class="csharp">

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

    /// &lt;summary&gt;
    /// Creates a new lanugage constraint
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;Language&quot;&gt;Allowed lanuage&lt;/param&gt;
    public LanguageConstraint(string language)
    {
        Language = language;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        String CultureFromRoute = values[&quot;culture&quot;].ToString();

        try
        {
            if (CultureFromRoute.Length &gt; 2 &amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp; CultureFromRoute.StartsWith(Language))
            {
                //validate, that the given culture from routeData is a valid culture
                System.Globalization.CultureInfo.GetCultureInfo(CultureFromRoute);
                return true;
            }

        }
        catch (Exception) { }
        return false;
    }
}
</pre>
<p>2) For those URLs with the same ending I crated a CulturesConstraint. This makes sure, that the page is only showed for given cultures</p>
<pre name="code" class="csharp">

public class CulturesConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        List&lt;String&gt; CultureListe = new List&lt;String&gt;();
        CultureListe.Add(&quot;en-GB&quot;);
        CultureListe.Add(&quot;en-US&quot;);
        CultureListe.Add(&quot;de-DE&quot;);
        CultureListe.Add(&quot;it-IT&quot;);
        return CultureListe.Contains(values[&quot;culture&quot;].ToString());
    }
}
</pre>
<p>
Feel free to drop a line, Alebo</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alebo.de/2009/04/localization-of-urls-using-aspnet-mvc-routing-and-constraints/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Localization with ASP.NET MVC using Routing</title>
		<link>http://www.alebo.de/2009/03/localization-with-aspnet-mvc-using-routing/</link>
		<comments>http://www.alebo.de/2009/03/localization-with-aspnet-mvc-using-routing/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 23:10:56 +0000</pubDate>
		<dc:creator>Alebo</dc:creator>
		
		<category><![CDATA[Allgemein]]></category>

		<category><![CDATA[ASP.Net MVC]]></category>

		<category><![CDATA[ASP.NET Routing]]></category>

		<guid isPermaLink="false">http://www.alebo.de/?p=12</guid>
		<description><![CDATA[In this article I am showing how to localize pages with ASP.NET MVC. Therefor I create a new Type of Route and a special RouteHandler.
Perhaps I will use ASP.NET for a new multilingual website, therefore I had to figure out how to create localized pages with ASP.NET MVC. It took a lot of time to [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I am showing how to localize pages with ASP.NET MVC. Therefor I create a new Type of Route and a special RouteHandler.</p>
<p>Perhaps I will use ASP.NET for a new multilingual website, therefore I had to figure out how to create localized pages with ASP.NET MVC. It took a lot of time to search for some implementations, but I could not find any useable code for my problem, so here is my approach.</p>
<h3>What I want</h3>
<p>For my site the URL schema should look like this in general:</p>
<p>/{culture}/{site}</p>
<p>Imagine there is a page called FAQ, which is available in different languages. Here are some sample URLs for these pages:</p>
<p>/en-US/FAQ<br />
/de-DE/FAQ<br />
/de-CH/FAQ</p>
<p>Additional I want to have some pages which are in a single language. The backend to modify the FAQ pages for example just needs to be in English.</p>
<h3>My approach</h3>
<p>My idea was to automatically add the {culture} parameter to those routes, which should be multilingual. I created a new class “MultiLingualMvcRouteHandler”. This Routehandler gets the culture from RouteData and sets CurrentCulture as well as CurrentUICulture. You can use this informations in custom HTMLHelpers.</p>
<pre name="code" class="csharp">

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1
{
    public class MultiLingualMvcRouteHandler : MvcRouteHandler
    {
        protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
        {
            String Culture = requestContext.RouteData.Values[&quot;culture&quot;].ToString();
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(Culture);
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(Culture);
            return base.GetHttpHandler(requestContext);
        }
    }
}
</pre>
<p>I also created a new Subtype of Route. Mainly this new Route is to discern between non multilingual and multilingual Routes. As you can see there are no additional methods or attributes yet.</p>
<pre name="code" class="csharp">

public class MultiLangualRoute : Route
{
    public MultiLangualRoute(string url, IRouteHandler routeHandler) : base(url, routeHandler) { }
    public MultiLangualRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) { }
    public MultiLangualRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) : base(url, defaults, constraints, routeHandler) { }
    public MultiLangualRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, constraints, dataTokens, routeHandler) { }
}
</pre>
<p>After that I registered my FAQ page like in the global.asax. At the bottom of the method I check the RoutesCollection for Routes of my new type MultilingualRoute. The URLs of those types are expanded at the beginning with {culture}. The default culture is set to en-US.</p>
<pre name="code" class="csharp">

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute(&quot;{resource}.axd/{*pathInfo}&quot;);          

    routes.Add(
        new Route(&quot;Backend/Edit/FAQ&quot;,
        new RouteValueDictionary(new { controller = &quot;CMS&quot;, action = &quot;Edit&quot;, Id = 1}),
        new System.Web.Mvc.MvcRouteHandler())
    );

    routes.Add(
        new MultiLangualRoute(&quot;FAQ&quot;,
        new RouteValueDictionary(new { controller = &quot;CMS&quot;, action = &quot;Show&quot;, Id = 1 }),
        new MvcApplication1.MultiLingualMvcRouteHandler())
    );            

    routes.MapRoute(
        &quot;Default&quot;,                                              // Route name
        &quot;{controller}/{action}/{id}&quot;,                           // URL with parameters
        new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = &quot;&quot; }  // Parameter defaults
    );            

    foreach (Route r in routes)
    {
        if (r.GetType() == typeof(MultiLangualRoute))
        {
            r.Url = &quot;{culture}/&quot; + r.Url;
            r.Defaults.Add(&quot;culture&quot;, &quot;en-US&quot;);
        }
    }
}
</pre>
<p>You can create links like this:</p>
<pre name="code" class="c#">

&lt;%= Html.ActionLink(&quot;Show me the FAQ in German&quot;, &quot;Show&quot;, &quot;CMS&quot;, new {id = 1, culture=&quot;de-DE&quot;}) %&gt;
</pre>
<p>
If you want to generate a link to a page with the actual culture, you can use the ActionLink helper without setting the culture.</p>
<pre name="code" class="c#">

&lt;%= Html.ActionLink(&quot;Show me the FAQ&quot;, &quot;Show&quot;, &quot;CMS&quot;, new {id = 1}) %&gt;
</pre>
<p>That&#8217;s it so far. Hope this is helpfull for someone else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alebo.de/2009/03/localization-with-aspnet-mvc-using-routing/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

