Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for localizable routes #1824

Merged
merged 8 commits into from
Aug 1, 2024
Merged

Conversation

tomasherceg
Copy link
Member

This PR adds support to define multiple route URLs for the same route that will apply in different request cultures.
A default route URL, which is applied for any unmatched language, is required.
The primary motivation for this is using the same route name for all language versions of the same page. Without that, the route name for each language would have to be different and thus dynamically computed for all RouteLink controls in the application).

config.RouteTable.Add("FeatureSamples_Localization_LocalizableRoute", "FeatureSamples/Localization/LocalizableRoute/{lang?}", "Views/FeatureSamples/Localization/LocalizableRoute.dothtml",
    localizedUrls: new LocalizedRouteUrl[] {
            new("cs-CZ", "cs/FeatureSamples/Localization/lokalizovana-routa"),
            new("de", "de/FeatureSamples/Localization/lokalisierte-route"),
    });

The RouteLink control was extended with the Culture property to generate a link to the specified route for a different culture (to allow easy language switching).

The route matching assumes that the request culture is already set.
Detection of the language must be done at the beginning of the request pipeline:

  • In ASP.NET Core, the easiest way is to use the standard Request Localization middleware.
  • In OWIN, there is no built-in mechanism. It is sufficient to write a middleware, which sets CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture, but you must also call context.Set(HostingConstants.OwinDoNotSetRequestCulture, true); to prevent DotVVM from overwriting the current culture to configuration.DefaultCulture.

LocalizablePresenter is not supported for this feature as it detects the request culture too late.

Also, the localization is currently unavailable for URL segments defined in route groups.

public bool Contains(string routeName)
{
return dictionary.ContainsKey(routeName);
}

public bool TryGetValue(string routeName, out RouteBase? route)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public bool TryGetValue(string routeName, out RouteBase? route)
public bool TryGetValue(string routeName, [MaybeNullWhen(false)] out RouteBase? route)

@tomasherceg tomasherceg force-pushed the feature/route-localization branch from e1951ab to 9812574 Compare July 14, 2024 07:49
@tomasherceg tomasherceg force-pushed the feature/route-localization branch from 8c5123a to b247302 Compare August 1, 2024 15:46
@tomasherceg tomasherceg force-pushed the feature/route-localization branch from b247302 to 2695c3b Compare August 1, 2024 16:49
@tomasherceg tomasherceg merged commit d824028 into main Aug 1, 2024
14 of 15 checks passed
@tomasherceg tomasherceg deleted the feature/route-localization branch August 1, 2024 19:34
@exyi exyi added this to the Version 4.3 milestone Aug 16, 2024
@exyi exyi restored the feature/route-localization branch August 17, 2024 20:05
@exyi exyi deleted the feature/route-localization branch August 18, 2024 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants