-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1840 from riganti/feature/route-localized-versions
Added AlternateCultureLinks control
- Loading branch information
Showing
5 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using DotVVM.Framework.Configuration; | ||
using DotVVM.Framework.Hosting; | ||
using DotVVM.Framework.Routing; | ||
|
||
namespace DotVVM.Framework.Controls | ||
{ | ||
/// <summary> | ||
/// Renders a <c><link rel=alternate</c> element for each localized route equivalent to the current route. | ||
/// On non-localized routes, it renders nothing (the control is therefore safe to use in a master page). | ||
/// The href must be an absolute URL, so it will only work correctly if <c>Context.Request.Url</c> contains the corrent domain. | ||
/// </summary> | ||
/// <seealso href="https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#alternate" /> | ||
/// <seealso href="https://developers.google.com/search/docs/specialty/international/localized-versions#html" /> | ||
/// <seealso cref="DotvvmRouteTable.Add(string, string, Type, object, LocalizedRouteUrl[])"/> | ||
public class AlternateCultureLinks : CompositeControl | ||
{ | ||
/// <param name="routeName">The name of the route to generate alternate links for. If not set, the current route is used. </param> | ||
public IEnumerable<DotvvmControl> GetContents(IDotvvmRequestContext context, string? routeName = null) | ||
{ | ||
var route = routeName != null ? context.Configuration.RouteTable[routeName] : context.Route; | ||
if (route is LocalizedDotvvmRoute localizedRoute) | ||
{ | ||
var currentCultureRoute = localizedRoute.GetRouteForCulture(CultureInfo.CurrentUICulture); | ||
|
||
foreach (var alternateCultureRoute in localizedRoute.GetAllCultureRoutes()) | ||
{ | ||
if (alternateCultureRoute.Value == currentCultureRoute) continue; | ||
|
||
var languageCode = alternateCultureRoute.Key == "" ? "x-default" : alternateCultureRoute.Key.ToLowerInvariant(); | ||
var alternateUrl = context.TranslateVirtualPath(alternateCultureRoute.Value.BuildUrl(context.Parameters!)); | ||
var absoluteAlternateUrl = BuildAbsoluteAlternateUrl(context, alternateUrl); | ||
|
||
yield return new HtmlGenericControl("link") | ||
.SetAttribute("rel", "alternate") | ||
.SetAttribute("hreflang", languageCode) | ||
.SetAttribute("href", absoluteAlternateUrl); | ||
} | ||
|
||
} | ||
} | ||
|
||
protected virtual string BuildAbsoluteAlternateUrl(IDotvvmRequestContext context, string alternateUrl) | ||
{ | ||
return new Uri(context.HttpContext.Request.Url, alternateUrl).AbsoluteUri; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
<head> | ||
<meta charset="utf-8" /> | ||
<title></title> | ||
|
||
<dot:AlternateCultureLinks /> | ||
</head> | ||
<body> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters