This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
RouteConfig.cs
109 lines (94 loc) · 4.68 KB
/
RouteConfig.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
namespace RouteLocalization.Mvc.Sample
{
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using RouteLocalization.Mvc.Extensions;
using RouteLocalization.Mvc.Setup;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// New workflow since 5.2
// This provider wraps generated attribute routes with LocalizationCollectionRoute routes
////LocalizationDirectRouteProvider provider
//// = new LocalizationDirectRouteProvider(new DefaultDirectRouteProvider());
// For less code preparation use the static provider stored in Localization class
routes.MapMvcAttributeRoutes(Localization.LocalizationDirectRouteProvider);
const string defaultCulture = "en";
ISet<string> acceptedCultures = new HashSet<string>() { defaultCulture, "en-US", "de", "de-AT" };
// Add translations
// You can translate every specific route that contains default Controller and Action (which MapMvcAttributeRoutes does)
routes.Localization(configuration =>
{
// Important: Set the route collection from LocalizationDirectRouteProvider if you specify your own
//// configuration.LocalizationCollectionRoutes = provider.LocalizationCollectionRoutes;
configuration.DefaultCulture = defaultCulture;
configuration.AcceptedCultures = acceptedCultures;
// Define how attribute routes should be processed:
// * None: There will be no routes except the ones you explicitly define in Translate()
// * AddAsNeutralRoute: Every attribute route will be added as neutral route
// * AddAsDefaultCultureRoute: Every attribute route will be added as localized route for defined default culture
// * AddAsNeutralAndDefaultCultureRoute: Every attribute route will be added as neutral route and
// as localized route for defined default culture
// * AddAsNeutralRouteAndReplaceByFirstTranslation: Every attribute route will be added as neutral route first, but when
// you add a translation for a route, the neutral route will be removed
configuration.AttributeRouteProcessing = AttributeRouteProcessing.AddAsNeutralAndDefaultCultureRoute;
// Uncomment if you do not want the culture (en, de, ...) added to each translated route as route prefix
configuration.AddCultureAsRoutePrefix = true;
configuration.AddTranslationToSimiliarUrls = true;
}).TranslateInitialAttributeRoutes().Translate(localization =>
{
// Use extension methods if you want to separate route configuration
localization.AddDefaultRoutesTranslation();
localization.AddAreaRoutesTranslation();
// DefaultRoutes.cs
////localization.ForCulture("de")
//// .ForController<HomeController>()
//// .ForAction(x => x.Index())
//// .AddTranslation("Willkommen")
//// .ForAction(x => x.Book())
//// .AddTranslation("Buch/{chapter}/{page}");
////localization.ForCulture("de")
//// .ForController<HomeWithRoutePrefixAttributeController>()
//// .SetRoutePrefix("RoutePrefixDE")
//// .ForAction(x => x.Index())
//// .AddTranslation("Willkommen")
//// .ForAction(x => x.Book())
//// .AddTranslation("Buch/{chapter}/{page}");
////localization.ForCulture("de")
//// .SetAreaPrefix("AreaPrefixDE")
//// .ForController<HomeWithRouteAreaAttributeController>()
//// .ForAction(x => x.Index())
//// .AddTranslation("Willkommen")
//// .ForAction(x => x.Book())
//// .AddTranslation("Buch/{chapter}/{page}");
// AreaRoutes.cs
////localization.ForCulture("de")
//// .SetAreaPrefix("Area")
//// .ForController<Areas.Area.Controllers.HomeController>()
//// .ForAction(x => x.Index())
//// .AddTranslation("Willkommen")
//// .ForAction(x => x.Book())
//// .AddTranslation("Buch/{chapter}/{page}");
});
// Optional
// Setup CultureSensitiveHttpModule
// This Module sets the Thread Culture and UICulture from http context
// Use predefined DetectCultureFromBrowserUserLanguages delegate or implement your own
CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate =
Localization.DetectCultureFromBrowserUserLanguages(acceptedCultures, defaultCulture);
// Optional
// Add culture sensitive action filter attribute
// This sets the Culture and UICulture when a localized route is executed
GlobalFilters.Filters.Add(new CultureSensitiveActionFilterAttribute()
{
// Set this options only if you want to support detection of region dependent cultures
// Supports this use case: https://github.com/Dresel/RouteLocalization/issues/38#issuecomment-70999613
AcceptedCultures = acceptedCultures,
TryToPreserverBrowserRegionCulture = true
});
}
}
}