Dresel / RouteLocalization

RouteLocalization is an MVC and Web API package that allows localization of your attribute routes.
MIT License
67 stars 13 forks source link

Difficulty redirecting to correct culture/prefix from non-default culture #60

Closed ajsgray closed 8 years ago

ajsgray commented 8 years ago

Forgive me if this has been covered before but I can't seem to figure it out.

My default culture is English with a Spanish localization. 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.

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):

Accept-language: English

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!!! http://stackoverflow.com/questions/39392265/mvc-routelocalization-difficulty-redirecting-to-correct-prefix

Dresel commented 8 years ago

If I understood you correctly (correct me if I'm wrong), you want the following setup:

Since you skipped the translations part in your code sample I can only guess, but 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.

Hope this helps.

EDIT:

I reread your description, I think I now got what you mean. You want to redirect to the localized route if route culture and browser culture don't match?

ajsgray commented 8 years ago

@Dresel That worked perfectly, thank you very much for your time!

If you want, post this as the answer to my stackoverflow question and I'll mark it as the answer.

Thanks again.

Dresel commented 8 years ago

Great, wasn't sure if I'm on the right track. Glad I could help.