I think you will have to make this functionality yourself. You will most likely have to hook into the httpRequestBegin
pipeline and also extend and replace the default LinkProvider
.
Here's an (untested) example of how this can be approached.
Start by hooking into the httpRequestBegin
pipeline and check if the language matches the language setting on the site definition.
public class SiteLanguageProcessor : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
var language = Sitecore.Context.Language;
var siteLanguage = Language.Parse(Sitecore.Context.Site.Language);
// If languages match, don't do anything
if (language == siteLanguage)
return;
// TODO: Find correct site for current language and redirect to it
}
}
This will take care of redirecting incoming requests to the correct site.
Next up we need to make sure generated links are also correct. For this we can extend the default LinkProvider
and the LinkBuilder
it uses. Here we can override the ResolveTargetSite(Item)
method. If the site found by the base method doesn't match the requested language then we can go ahead and try to find a matching site.
public class SiteLanguageLinkProvider : Sitecore.Links.LinkProvider
{
protected override LinkBuilder CreateLinkBuilder(UrlOptions options)
{
return new SiteLanguageLinkBuilder(options);
}
}
public class SiteLanguageLinkBuilder : Sitecore.Links.LinkProvider.LinkBuilder
{
private readonly UrlOptions _options;
public SiteLanguageLinkBuilder(UrlOptions options) : base(options)
{
_options = options;
}
protected override SiteInfo ResolveTargetSite(Item item)
{
var targetSite = base.ResolveTargetSite(item);
var targetLanguage = Language.Parse(targetSite.Language);
// Site is correct if language match
if (targetLanguage == _options.Language)
return targetSite;
// TODO: Find matching site
}
}
This will take care of generating links to the correct site.
Here is a config file to patch all this in.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<!-- Change default provider to our custom link provider -->
<linkManager set:defaultProvider="custom">
<providers>
<!-- Add our custom link provider - set additional settings to your preferences -->
<add name="custom" type="YourAssembly.SiteLanguageLinkProvider, YourAssembly"
addAspxExtension="false"
alwaysIncludeServerUrl="false"
encodeNames="true"
languageLocation="filePath"
languageEmbedding="always"
lowercaseUrls="true"
shortenUrls="true"
useDisplayName="false" />
</providers>
</linkManager>
<pipelines>
<httpRequestBegin>
<!-- Patch after the language resolver (and after site resolver) -->
<processor type="YourAssembly.SiteLanguageProcessor, YourAssembly" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.LanguageResolver, Sitecore.Kernel'"/>
</httpRequestBegin>
</pipelines>
</sitecore>
</configuration>
You then need to have a site definition for each site/language with the language
attribute set to the language supported for that site.
EDIT: There are actually some settings for site resolving which probably doesn't require you to extend the link provider as long as you make sure to set the targetHostName
attribute on your site definitions.
<!--
SITE RESOLVING
While rendering item links, some items may belong to different site. Setting this to true
make LinkManager try to resolve target site in order to use the right host name.
Default value: true
-->
<setting name="Rendering.SiteResolving" value="true"/>
<!--
RENDERING - SITE RESOLVING MATCH CURRENT LANGUAGE
Affects how cross-site links are rendered when Rendering.SiteResolving is enabled.
If true, the link provider will take the language attribute of the site definitions into consideration when resolving
which site/hostname to use when rendering a cross-site link.
Default value: true
-->
<setting name="Rendering.SiteResolvingMatchCurrentLanguage" value="true"/>
<!--
RENDERING - SITE RESOLVING MATCH CURRENT SITE
Affects how cross-site links are rendered when Rendering.SiteResolving is enabled.
If true, the link provider will check if the target item is located under the start item for the current site before it
tries to find a match in the full list of site definitions. This ensures that when the target item can be resolved using
the current site, the target link will not change to a different site/hostname.
Default value: true
-->
<setting name="Rendering.SiteResolvingMatchCurrentSite" value="true"/>