Skip to content

Commit 0616be0

Browse files
committed
Updated the multilanguage-setup.md for v14 to include the language service
Updated the v10 docs to use the original example, as language service is not available
1 parent 2151348 commit 0616be0

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,105 @@ For viewing purposes, I've added a stylesheet to my website. The final result sh
226226
**German Version:**
227227

228228
<figure><img src="images/final-result-da.png" alt=""><figcaption></figcaption></figure>
229+
230+
231+
## Using Muli languages across APIs
232+
233+
When requesting content over an API, the culture will fallback to the default, unless explicitly set.
234+
235+
To do this, we can use the IVariationContextAccessor.
236+
237+
```csharp
238+
public class ExampleController : SurfaceController
239+
{
240+
private readonly ILocalizationService _localizationService;
241+
private readonly IVariationContextAccessor _variationContextAccessor;
242+
243+
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)
244+
{
245+
_localizationService = localizationService;
246+
_variationContextAccessor = variationContextAccessor;
247+
}
248+
249+
public IActionResult Index(string culture = null)
250+
{
251+
IEnumerable<ILanguage> UmbracoLanguages = _localizationService.GetAllLanguages(); //a helpful method to get all configured languages
252+
var requestedCulture = UmbracoLanguages.FirstOrDefault(l => l.IsoCode == culture);
253+
254+
if (requestedCulture != null)
255+
{
256+
_variationContextAccessor.VariationContext = new VariationContext(requestedCulture.IsoCode);
257+
}
258+
259+
//this will now be in the requested culture
260+
var content = UmbracoContext.Content.GetAtRoot();
261+
262+
//Content requested in this View Component will now be in the requested culture
263+
return ViewComponent();
264+
}
265+
}
266+
```
267+
268+
##Creating a Language Switching Navigation
269+
270+
To navigate between languages, we need to do two key things:
271+
272+
1. Get all the languages that the site can provide
273+
2. Identify the language used on the current page
274+
275+
Once we have these, we need to loop through the languages, and provide links to each home node.
276+
277+
#Getting all the languages for a site
278+
279+
There are two ways to achive this. One is to use ```localizationService.GetAllLanguages();``` to call the database, which is expensive and ideally includes caching.
280+
281+
The alternative is to get the Home node, and find all of the cultures associated to it. This has a few benifits including speed and providing us with a link to show the user. It is the process we will use.
282+
283+
#Identify the language for the current page
284+
285+
This is achived in ```cs.html``` files using ```umbracoHelper.AssignedContentItem.GetCultureFromDomains();```
286+
287+
#Steps
288+
289+
Now we have what we need, create a view called ```Navigation.cshtml``` , and paste in the following:
290+
291+
```cshtml
292+
@using Umbraco.Cms.Web.Common
293+
@inject IUmbracoHelperAccessor _umbracoHelperAccessor;
294+
295+
@{
296+
_umbracoHelperAccessor.TryGetUmbracoHelper(out var umbracoHelper);
297+
298+
var homePage = umbracoHelper.ContentAtRoot().FirstOrDefault(c => c.ContentType.Alias == "{{homeNodeContentAlias}}");
299+
var cultures = homePage?.Cultures;
300+
}
301+
302+
@if (cultures.Count > 1)
303+
{
304+
<ul aria-label="Language switcher">
305+
@foreach (var cult in cultures)
306+
{
307+
//get the settings for this culture
308+
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cult.Key);
309+
//if the current page has a langauage variant, otherwise link to the homepage language variant
310+
string langUrl = umbracoHelper.AssignedContentItem.Url(cult.Key, UrlMode.Relative) ?? homePage.Url(cult.Key, UrlMode.Relative);
311+
312+
<li>
313+
@if (cult.Key.ToLower() == umbracoHelper.AssignedContentItem.GetCultureFromDomains().ToLower())
314+
{
315+
<span aria-current="true" >@culture.NativeName</span>
316+
}
317+
else
318+
{
319+
<a href="@langUrl" hreflang="@cult.Key" lang="@cult.Key" >@culture.NativeName</a>
320+
}
321+
</li>
322+
}
323+
</ul>
324+
}
325+
```
326+
327+
You will need to replace ```{{homeNodeContentAlias}}``` with the Document Type alias of your Home node.
328+
329+
This will look at all the cultures available on the home node, and render links to either the language variant of the current page, or the home node for the language variant. If the home node for a language variant is removed, it will not appear in the list.
330+
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 of the site.

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ To do this, we can use the IVariationContextAccessor.
273273
```csharp
274274
public class ExampleController : SurfaceController
275275
{
276-
private readonly ILocalizationService _localizationService;
276+
private readonly ILanguageService _languageService;
277277
private readonly IVariationContextAccessor _variationContextAccessor;
278278

279279
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)
@@ -284,7 +284,7 @@ public class ExampleController : SurfaceController
284284

285285
public IActionResult Index(string culture = null)
286286
{
287-
IEnumerable<ILanguage> UmbracoLanguages = _localizationService.GetAllLanguages(); //a helpful method to get all configured languages
287+
IEnumerable<ILanguage> UmbracoLanguages = _languageService.GetAllAsync().Result; //a helpful method to get all configured languages
288288
var requestedCulture = UmbracoLanguages.FirstOrDefault(l => l.IsoCode == culture);
289289

290290
if (requestedCulture != null)
@@ -312,7 +312,9 @@ Once we have these, we need to loop through the languages, and provide links to
312312

313313
#Getting all the languages for a site
314314

315-
There are two ways to achive this. One is to use ```localizationService.GetAllLanguages();``` to call the database, which is expensive and ideally includes caching.
315+
There are three ways to achive this. The best one is to use ```languageService.GetAllAsync();``` which retrives items from the cache.
316+
317+
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.
316318

317319
The alternative is to get the Home node, and find all of the cultures associated to it. This has a few benifits including speed and providing us with a link to show the user. It is the process we will use.
318320

0 commit comments

Comments
 (0)