Retrieve Content/Page Data in All Languages

We're in the process of building a large multi-lingual site (30+ individual cultures) and one of the requirements from the clients is to implement hreflang tags to show alternate URLs for all cultures across the whole site.

When working with K12 and the legacy DocumentQuery API you could either use .AllCultures() or .Cultures("x", "y", "z") to retrieve all the data as part of a single query.

However from what I can tell with the XbyK this doesn't seem possible as the .InLanguage("x") function only takes a single parameter, and omitting it entirely only returns the current preferred culture. Is is possible to return all the items in a single query rather than having to loop over a collection of cultures, effectively causing 30+ SQL queries on page load.

Thanks,
Steven

Tags:
Content querying

Answers

Hi Steven,

We were doing this for a customer, and came up with a query like this (but see below).

Example query

var languages = await GetAllAsync();
var languageMap = languages.ToDictionary(
    l => l.ContentLanguageID,
    l => l.ContentLanguageCultureFormat);

var builder = new ContentItemQueryBuilder()
    .ForContentTypes(p => p.ForWebsite(siteName))
    .Parameters(p => p
        .UrlPathColumns()
        .Where(w => w.WhereEquals(nameof(WebPageFields.WebPageItemID), page.SystemFields.WebPageItemID)));

var pages = await contentQueryExecutor.GetMappedWebPageResult<IWebPageFieldsSource>(
    builder,
    new ContentQueryExecutionOptions { ForPreview = false });


However...

We double checked Google's requirements for associating language variants and verified it is not necessary to add hreflang tags to every page's head, if the variants are associated correctly in sitemap.xml. This allowed us to remove the potential expensive query from every page, and isolate the cost in the sitemap.

According to Google, there are

There are three ways to indicate multiple language/locale versions of a page to Google:

The three methods are equivalent from Google's perspective and you can choose the method that's the most convenient for your site. While you can use all three methods at the same time, there's no benefit in Search (in fact, it maybe be much harder to manage three implementations instead of just picking one).

To response this discussion, you have to login first.