---
title: Setting up multilingual MVC projects
related:
  - https://docs.kentico.com/k12sp/multilingual-websites/setting-up-multilingual-websites/localizing-content-on-mvc-sites.md
  - https://docs.kentico.com/k12sp/multilingual-websites/setting-up-a-multilingual-user-interface/localizing-mvc-builder-components.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

To serve content in multiple languages on [MVC sites](https://docs.kentico.com/k12sp/developing-websites/mvc-development-overview.md), you first need to set up functionality that detects and sets the current culture for each request. You can then [localize the content](https://docs.kentico.com/k12sp/multilingual-websites/setting-up-multilingual-websites/localizing-content-on-mvc-sites.md) displayed on the site's pages.

If you want to have a multilingual MVC site, you need to set up culture recognition in the MVC project. On the beginning of every request, you need to:

1. Retrieve or determine the correct culture to be used in the current request. The way you detect the culture depends on the implementation of your multilingual site. For example, possible options are culture prefixes in URLs, culture-specific domains, custom cookies, etc.
2. Set the **Thread.CurrentThread.CurrentUICulture** and **Thread.CurrentThread.CurrentCulture** properties of the current thread (available in the _System.Threading_ namespace).

   > **Note:** **Note**: _Thread.CurrentThread.CurrentUICulture_ and _Thread.CurrentThread.CurrentCulture_ are properties of the .NET framework and use the _System.Globalization.CultureInfo_ type. They are not directly comparable with the Kentico _CMS.Localization.CultureInfo_ type, or the _CurrentUICulture_ and _CurrentCulture_ properties of the _CMS.Localization.LocalizationContext_ class.

The Kentico localization API then automatically works with the given culture (for example when resolving [resource strings](https://docs.kentico.com/k12sp/multilingual-websites/setting-up-a-multilingual-user-interface/working-with-resource-strings.md)).

### Example

One common way to determine the culture of page requests is to use culture prefixes in your site's routes. For example:

- English – _www.example.com/**en-us**/path_
- Spanish – _www.example.com/**es-es**/path_

The following code examples showcase how to parse the culture from the route prefix and set the current culture for the MVC application. You can modify and adjust snippets from this example for use in your own projects.

```csharp title="Example - RouteConfig"

using System.Web.Mvc;
using System.Web.Routing;

...

public static void RegisterRoutes(RouteCollection routes)
{
    ...

    // Parses a URL containing a culture route prefix
    var route = routes.MapRoute(
        name: "Default",
        url: "{culture}/{controller}/{action}",
        defaults: new { controller = "Home", action = "Index" },
        constraints: new { culture = new SiteCultureConstraint() }
    );

    // Assigns a custom route handler to the route
    route.RouteHandler = new MultiCultureMvcRouteHandler();
}


```

```csharp title="Example - MultiCultureMvcRouteHandler"

using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

public class MultiCultureMvcRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // Retrieves the requested culture from the route
        var cultureName = requestContext.RouteData.Values["culture"].ToString();

        try
        {
            // Creates a CultureInfo object from the culture code
            var culture = new CultureInfo(cultureName);

            // Sets the current culture for the MVC application
            Thread.CurrentThread.CurrentUICulture = culture;
            Thread.CurrentThread.CurrentCulture = culture;
        }
        catch
        {
            // Handles cases where the culture parameter of the route is invalid
            // Returns a 404 status in this case, but you can also log an error, set a default culture, etc.
            requestContext.HttpContext.Response.StatusCode = 404;
        }

        return base.GetHttpHandler(requestContext);
    }
}

```

```csharp title="Example - SiteCultureConstraint"

using System.Web;
using System.Web.Routing;

using CMS.SiteProvider;

// Constraint that restricts culture parameter values
// Only allows the codes of cultures assigned to the current site in Kentico
public class SiteCultureConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext,
                    Route route,
                    string parameterName,
                    RouteValueDictionary values,
                    RouteDirection routeDirection)
    {
        string cultureCodeName = values[parameterName]?.ToString();
        return CultureSiteInfoProvider.IsCultureOnSite(cultureCodeName, SiteContext.CurrentSiteName);
    }
}

```
