I'm using the RouteLocalization.Mvc library for translations on my site and having some difficulty when not using the default culture (English).
My site is localized to Spanish and all routes work correctly when my browser's language is set to English - I get un-prefixed routes in English and prefixed /es/ routes in Spanish as expected.
However, when I set my browser's culture to Spanish I get the translated pages on un-prefixed routes, but the prefixes I really want are (using my about page as an example):
I planned to redirect to the correct prefix but can't seem to figure out how to do it.
var config = new Configuration()
{
DefaultCulture = "en",
AcceptedCultures = new HashSet<string> { "en", "es" },
AddCultureAsRoutePrefix = true,
AddTranslationToSimiliarUrls = true,
AttributeRouteProcessing = AttributeRouteProcessing.AddAsNeutralRoute
};
// translations omitted
CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate = Localization.DetectCultureFromBrowserUserLanguages(acceptedCultures, defaultCulture);
GlobalFilters.Filters.Add(new CultureSensitiveActionFilterAttribute());
Any help will be much appreciated!!!
The setup you want is the following:
I think your English routes are neutral routes in fact (because of AttributeRouteProcessing.AddAsNeutralRoute
).
Try setting AddCultureAsRoutePrefix
to false
, AttributeRouteProcessing
to AddAsDefaultCultureRoute
. Then call TranslateInitialAttributeRoutes()
.
This would add your attribute routes without prefixes as localized English routes. Some info from the documentation:
First you have to decide how the initial attribute routes, which are intercepted by RouteLocalization, should be processed. There are a few possibilities that can be choosen via the Configuration.AttributeRouteProcessing property.
If your attribute routes for example are English be default, you could define that every attribute route should be added as localized route for English culture. You would therefore set the DefaultCulture to "en" and AttributeRouteProcessing to AddAsDefaultCultureRoute.
After calling TranslateInitialAttributeRoutes
you can set AddCultureAsRoutePrefix
back to true
and then start translating your Spanish routes.