KenticoDevTrev / DynamicRouting

Dynamic Routing in Kentico using a Routing Table and Assembly Attribute
15 stars 5 forks source link

Shows always the default culture page #25

Closed gorisken closed 4 years ago

gorisken commented 4 years ago

We have setup the dynamic routing with multi culture suppot by using the recommended URL pattern /{% DocumentCulture %}{% NodeAliasPath %}.

In the implementation we used 2 ways:

We can visit the different languages, so that part works, but the content of the page is always the default language.

Is this a bug, or are there additional settings necessary to solve this?

KenticoDevTrev commented 4 years ago

The "GetPage" attempts to get the current culture in this order:

  1. If preview mode, the culture of the preview.
  2. The LocalizationContext.CurrentCulture.CultureName
  3. The System.Globalization.CultureInfo.CurrentCulture.Name.

If these context are not set based on the URL Prefix, then you will get the default culture. You will need to tell the MVC site "this is the culture" based on your pattern (culture prefix). You can do this through a couple ways:

  1. You can try to create your own route handler that handles the logic similar to how Kentico recommends (setting the Thread.Current.CurrentUICulture and Thread.Current.CurrentCulture), but you'll need to make sure to still return the DynamicRouteHandler
  2. Much easier, you can add a global event hook to the DynamicRoutingEvents.GetCulture.Before events to check the URL for the culture, then set the GetCultureEventArgs.Culture to the culture you see in the GetCultureEventArgs.Request

I recommend #2 personally, however be aware that #2 will ONLY affect the GetPage operation, and not anything else, so you will still need to somehow set the CurrentUICulture and CurrentCulture so other elements have the context of what language to select.