Skip to content

Commit 31acd31

Browse files
committed
Add #6360
1 parent 2a54416 commit 31acd31

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ 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+
## More Information
93+
94+
Further information on multi-language setups can be found in the [Multilanguage Setup tutorial](../multilanguage-setup.md).

15/umbraco-cms/tutorials/multilanguage-setup.md

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ For this tutorial, we will create the following document types:
8181

8282
* Home Page
8383

84-
![Home Page](../../../10/umbraco-cms/tutorials/images/home-page.png)
84+
![Home Page](images/home-page.png)
8585
* Blogs
8686

87-
![Blogs](../../../10/umbraco-cms/tutorials/images/Blogs.png)
87+
![Blogs](images/Blogs.png)
8888
* Contact Us
8989

90-
![Contact Us](../../../10/umbraco-cms/tutorials/images/Contact-us.png)
90+
![Contact Us](images/Contact-us.png)
9191

9292
## Enabling Language Variants on Document Types and Properties
9393

@@ -257,8 +257,111 @@ For viewing purposes, I've added a stylesheet to my website. The final result sh
257257

258258
Danish Version:
259259

260-
<figure><img src="../../../10/umbraco-cms/tutorials/images/final-result-dk.png" alt=""><figcaption></figcaption></figure>
260+
<figure><img src="images/final-result-dk.png" alt=""><figcaption></figcaption></figure>
261261

262262
German Version:
263263

264-
<figure><img src="../../../10/umbraco-cms/tutorials/images/final-result-da.png" alt=""><figcaption></figcaption></figure>
264+
<figure><img src="images/final-result-da.png" alt=""><figcaption></figcaption></figure>
265+
266+
## Using Multiple languages across APIs
267+
268+
When requesting content over an API, the culture will fall back to the default, unless explicitly set.
269+
270+
To do this, you can use the IVariationContextAccessor.
271+
272+
```csharp
273+
public class ExampleController : SurfaceController
274+
{
275+
private readonly ILanguageService _languageService;
276+
private readonly IVariationContextAccessor _variationContextAccessor;
277+
278+
public ExampleController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider, ILocalizationService localizationService, IVariationContextAccessor variationContextAccessor) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
279+
{
280+
_localizationService = localizationService;
281+
_variationContextAccessor = variationContextAccessor;
282+
}
283+
284+
public IActionResult Index(string culture = null)
285+
{
286+
IEnumerable<ILanguage> UmbracoLanguages = _languageService.GetAllAsync().Result; //a helpful method to get all configured languages
287+
var requestedCulture = UmbracoLanguages.FirstOrDefault(l => l.IsoCode == culture);
288+
289+
if (requestedCulture != null)
290+
{
291+
_variationContextAccessor.VariationContext = new VariationContext(requestedCulture.IsoCode);
292+
}
293+
294+
//this will now be in the requested culture
295+
var content = UmbracoContext.Content.GetAtRoot();
296+
297+
//Content requested in this View Component will now be in the requested culture
298+
return ViewComponent();
299+
}
300+
}
301+
```
302+
303+
### Creating a Language Switching Navigation
304+
305+
To navigate between languages, you need to do two key things:
306+
307+
1. Get all the languages that the site can provide
308+
2. Identify the language used on the current page
309+
310+
Once you have these, you need to loop through the languages and provide links to each home node.
311+
312+
### Getting all the languages for a site
313+
314+
There are three ways to achieve this. The best one is to use `languageService.GetAllAsync();` which retrieves items from the cache.
315+
316+
Another is to use `localizationService.GetAllLanguages();` to call the database, which is expensive and ideally includes caching. This should only be done if you cannot use the ILanguage service. This service is marked as obsolete.
317+
318+
The alternative is to get the Home node and find all of the cultures associated with it. This has a few benefits including speed and providing us with a link to show the user. It is the process you will use when following this guide.
319+
320+
### Identify the language for the current page
321+
322+
This is achieved in `cs.html` files using `umbracoHelper.AssignedContentItem.GetCultureFromDomains();`
323+
324+
#### Steps
325+
326+
Now that you have what you need, take the following steps to create a working example.
327+
328+
1. Create a new view called `Navigation.cshtml`
329+
2. Paste in the following code:
330+
331+
```cshtml
332+
@using Umbraco.Cms.Web.Common
333+
@inject IUmbracoHelperAccessor _umbracoHelperAccessor;
334+
@{
335+
_umbracoHelperAccessor.TryGetUmbracoHelper(out var umbracoHelper);
336+
var homePage = umbracoHelper.ContentAtRoot().FirstOrDefault(c => c.ContentType.Alias == "{{homeNodeContentAlias}}");
337+
var cultures = homePage?.Cultures;
338+
}
339+
@if (cultures.Count > 1)
340+
{
341+
<ul aria-label="Language switcher">
342+
@foreach (var cult in cultures)
343+
{
344+
//get the settings for this culture
345+
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cult.Key);
346+
//if the current page has a language variant, otherwise link to the homepage language variant
347+
string langUrl = umbracoHelper.AssignedContentItem.Url(cult.Key, UrlMode.Relative) ?? homePage.Url(cult.Key, UrlMode.Relative);
348+
<li>
349+
@if (cult.Key.ToLower() == umbracoHelper.AssignedContentItem.GetCultureFromDomains().ToLower())
350+
{
351+
<span aria-current="true" >@culture.NativeName</span>
352+
}
353+
else
354+
{
355+
<a href="@langUrl" hreflang="@cult.Key" lang="@cult.Key" >@culture.NativeName</a>
356+
}
357+
</li>
358+
}
359+
</ul>
360+
}
361+
```
362+
363+
3. Replace `{{homeNodeContentAlias}}` with the Document Type alias of your Home node.
364+
365+
This will render links to either the language variant of the current page or the home node for the language variant.
366+
367+
Additionally, `System.Globalization.CultureInfo` is used to obtain the native name of the language being rendered. This is useful if a user does not speak the default language.

0 commit comments

Comments
 (0)