Language variants allow you to have different variations of content based on the language culture. Learn how to use them in this section.
Language Variation allows you to have different variations of content based on a language culture. In the documentation there are other useful articles about the feature:
IPublishedContent contains all language variations of a node, and when rendering it out it will then use the Culture you are currently on. This can then be overridden on an individual property level if you want, like this:
Here we would attempt to render the pageTitle property in the French variant. We want to fallback to the current culture language if it can't find it in French.
The challenge arises when trying to display all values of an IPublishedContent model in a specific culture from a "current culture"-less context, like a SurfaceController.
You will get the root node in the default culture. However you can set a new VariationContext like this:
usingMicrosoft.AspNetCore.Mvc;usingUmbraco.Cms.Core.Cache;usingUmbraco.Cms.Core.Logging;usingUmbraco.Cms.Core.Models.PublishedContent;usingUmbraco.Cms.Core.Routing;usingUmbraco.Cms.Core.Services;usingUmbraco.Cms.Core.Web;usingUmbraco.Cms.Infrastructure.Persistence;usingUmbraco.Cms.Web.Website.Controllers;usingUmbraco.Extensions;namespaceTestStuff;publicclassTestController:SurfaceController{privatereadonlyIVariationContextAccessor _variationContextAccessor;publicTestController(IUmbracoContextAccessor umbracoContextAccessor,IUmbracoDatabaseFactory databaseFactory,ServiceContext services,AppCaches appCaches,IProfilingLogger profilingLogger,IPublishedUrlProvider publishedUrlProvider,IVariationContextAccessor variationContextAccessor) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider) { _variationContextAccessor = variationContextAccessor; }publicIActionResultIndex() {conststring culture ="af"; // Afrikaans // This is how the culture is set for the context we are in_variationContextAccessor.VariationContext=newVariationContext(culture);var culturedRootNode =CurrentPage.Root();TempData.Add("CulturedRootNode", culturedRootNode);returnView(); }}
So we access the IVariationContextAccessor.VariationContext and set it to a new one with our own specified culture (remember using Umbraco.Core.Models.PublishedContent; at the top to get access to it).
The elements you get afterward will be in the culture you have specified.