google hreflang tag and bing content-language

3 years ago
As google search console requests a hreflang tag I've modified _Root.Head.cshtml in the following way.
You can use it as a starting point. Maybe there are better ways.

add first the following references
@using Nop.Core.Domain.Localization;
@using System.Text;
@using Nop.Services.Seo;

...
then add the following code after

//custom tag(s)
    var seoSettings = EngineContext.Current.Resolve<SeoSettings>();
    if (!string.IsNullOrEmpty(seoSettings.CustomHeadTags))
    {
        Html.AppendHeadCustomParts(seoSettings.CustomHeadTags);
    }


add this code


    //Hhreflang and Bing content-language tags
    RouteData data = Request.RequestContext.RouteData;
    if (data != null)
    {
        var languageService = EngineContext.Current.Resolve<Nop.Services.Localization.ILanguageService>();
        var storeContext = EngineContext.Current.Resolve<Nop.Core.IStoreContext>();
        var urlRecordService = EngineContext.Current.Resolve<IUrlRecordService>();
        var workContext = EngineContext.Current.Resolve<Nop.Core.IWorkContext>();
        var slug = data.Values["generic_se_name"] as string;
        var urlRecord = urlRecordService.GetBySlugCached(slug);
        string pageURL = "";
        var result = new StringBuilder();


        //bing lang
        result.AppendFormat("<meta http-equiv=\"content-language\" content=\"{0}\" >", workContext.WorkingLanguage.LanguageCulture.ToLower());
        result.Append(Environment.NewLine);

        //base URL
        if (urlRecord != null)
        {
            pageURL = urlRecordService.GetActiveSlug(urlRecord.EntityId, urlRecord.EntityName, 0);
        }
        else
        {   //non SEO URL
            pageURL = Request.Path.Substring(1);
        }
        result.AppendFormat("<link rel=\"alternate\" href=\"{0}{1}\"  hreflang=\"x-default\" />", storeContext.CurrentStore.Url, pageURL);
        result.Append(Environment.NewLine);

        //SEO Lang Url
        var localizationSettings = EngineContext.Current.Resolve<LocalizationSettings>();
        if (localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
        {
            foreach (var language in languageService.GetAllLanguages(storeId: storeContext.CurrentStore.Id))
            {
                if (urlRecord != null) //SEO URL
                {
                    pageURL = SeoExtensions.GetSeName(urlRecord.EntityId, urlRecord.EntityName, language.Id);

                }
                result.AppendFormat("<link rel=\"alternate\" href=\"{0}{1}/{2}\"  hreflang=\"{1}\" />", storeContext.CurrentStore.Url, language.UniqueSeoCode, pageURL);
                result.Append(Environment.NewLine);
            }
        }
        Html.AppendHeadCustomParts(result.ToString());
    } //Hhreflang
2 years ago
Thanks a lot falkao ... working well
2 years ago
Thanks a lot! Working fine at 3.60
2 years ago
Thank you for sharing! It works also in 3.90.
2 years ago
great to hear that everything works fine.
2 years ago
Hello,

this code work fine for my site in 4 languages, but I see a problem with hreflang value=x-default, language=default, Region=default, please see my screenshot, is this bug? how can I fix it?

PS: I use english as default language in my site.

Thank you!
1 year ago
Hi,
I'm using nop 4.00, does this code works also for my version?
If yes - in which parts of the code I need to put that pice of code (I'm notcodign nop... so my apologies...),
Any help is most welcome...

Regards,
Lior.
3 weeks ago
Very useful, thankyou! Minor changes to work on 4.0 for anyone looking at this in the future:

@using Nop.Core.Domain.Localization;
@using System.Text;
@using Nop.Services.Seo;
@using Nop.Core.Infrastructure;
@using Microsoft.AspNetCore.Routing



    //Hhreflang and Bing content-language tags
    //RouteData data = Request.RequestContext.RouteData;
    RouteData data = Context.GetRouteData();
    if (data != null)
    {
        var languageService = EngineContext.Current.Resolve<Nop.Services.Localization.ILanguageService>();
        var storeContext = EngineContext.Current.Resolve<Nop.Core.IStoreContext>();
        var urlRecordService = EngineContext.Current.Resolve<IUrlRecordService>();
        var workContext = EngineContext.Current.Resolve<Nop.Core.IWorkContext>();
        var slug = data.Values["SeName"] as string;
        var urlRecord = urlRecordService.GetBySlug(slug); //Cached
        string pageURL = "";
        var result = new StringBuilder();
        

        //bing lang
        result.AppendFormat("<meta http-equiv=\"content-language\" content=\"{0}\" >", workContext.WorkingLanguage.LanguageCulture.ToLower());
        result.Append(Environment.NewLine);

        //base URL
        if (urlRecord != null)
        {
            pageURL = urlRecordService.GetActiveSlug(urlRecord.EntityId, urlRecord.EntityName, 0);
        }
        else
        {   //non SEO URL
            //pageURL = Request.Path.Substring(1);
            pageURL = Context.Request.Path.Value.Substring(1);
        }
        result.AppendFormat("<link rel=\"alternate\" href=\"{0}{1}\"  hreflang=\"x-default\" />", storeContext.CurrentStore.Url, pageURL);
        result.Append(Environment.NewLine);

        //SEO Lang Url
        var localizationSettings = EngineContext.Current.Resolve<LocalizationSettings>();
        if (localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
        {
            foreach (var language in languageService.GetAllLanguages(storeId: storeContext.CurrentStore.Id))
            {
                if (urlRecord != null) //SEO URL
                {
                    pageURL = SeoExtensions.GetSeName(urlRecord.EntityId, urlRecord.EntityName, language.Id);

                }
                result.AppendFormat("<link rel=\"alternate\" href=\"{0}{1}/{2}\"  hreflang=\"{1}\" />", storeContext.CurrentStore.Url, language.UniqueSeoCode, pageURL);
                result.Append(Environment.NewLine);
            }
        }
        Html.AppendHeadCustomParts(result.ToString());
    } //Hhreflang