Skip to content

Commit 7252a91

Browse files
Update adding-language-variants.md
Added some support for setting cultures in API calls
1 parent 20e862f commit 7252a91

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

14/umbraco-cms/tutorials/creating-a-basic-website/adding-language-variants.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,40 @@ To view the language variant on the browser, follow these steps:
8888
![Viewing the Language Variant Link](images/viewing-langvariant-browser-v14.png)
8989
5. Click on the link to view the new language node in the browser.
9090
6. Alternatively, you can add the domain name to your localhost in the browser. For example: [http://localhost:xxxx/da/](http:/localhost:xxxx/da/)
91+
92+
## Using Muli languages across APIs
93+
94+
When requesting content over an API, the culture will fallback to the default, unless explicitly set.
95+
96+
To do this, we can use the IVariationContextAccessor.
97+
98+
```csharp
99+
public class TimelineController : SurfaceController
100+
{
101+
private readonly ILocalizationService _localizationService;
102+
private readonly IVariationContextAccessor _variationContextAccessor;
103+
104+
public TimelineController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider, ILocalizationService localizationService, IVariationContextAccessor variationContextAccessor) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
105+
{
106+
_localizationService = localizationService;
107+
_variationContextAccessor = variationContextAccessor;
108+
}
109+
110+
public IActionResult LoadTimelineItems(string culture = null)
111+
{
112+
IEnumerable<ILanguage> UmbracoLanguages = _localizationService.GetAllLanguages(); //a helpful method to get all configured languages
113+
var requestedCulture = UmbracoLanguages.FirstOrDefault(l => l.IsoCode == culture);
114+
115+
if (requestedCulture != null)
116+
{
117+
_variationContextAccessor.VariationContext = new VariationContext(requestedCulture.IsoCode);
118+
}
119+
120+
//this will now be in the requested culture
121+
var content = UmbracoContext.Content.GetAtRoot();
122+
123+
//Content requested in this View Component will now be in the requested culture
124+
return ViewComponent("TimelineItems");
125+
}
126+
}
127+
```

0 commit comments

Comments
 (0)