Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/TOC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
href: helpers/weakeventmanagert.md
- name: "WeakEventManagerExtensions"
href: helpers/weakeventmanagerextensions.md
- name: "LocalizedString"
href: helpers/localizedstring.md
- name: "ObjectModel"
items:
- name: "AsyncCommand"
Expand Down
4 changes: 2 additions & 2 deletions docs/extensions/translateextension.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
title: "Xamarin Community Toolkit TranslateExtension"
author: sthewissen
ms.author: joverslu
description: "The TranslateExtension allows users to handle multi-language support at runtime."
description: "The TranslateExtension allows users to handle multi-language support in XAML at runtime."
ms.date: 10/09/2020
---

# Xamarin Community Toolkit TranslateExtension

The TranslateExtension allows users to handle multi-language support at runtime. It uses the built-in `LocalizationResourceManager` helper to retrieve the correct translation resource for the current active [CultureInfo](xref:System.Globalization.CultureInfo).
The TranslateExtension allows users to handle multi-language support in XAML at runtime. It uses the built-in `LocalizationResourceManager` helper to retrieve the correct translation resource for the current active [CultureInfo](xref:System.Globalization.CultureInfo).

## Syntax

Expand Down
65 changes: 65 additions & 0 deletions docs/helpers/localizedstring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: "Xamarin Community Toolkit LocalizedString"
author: maxkoshevoi
ms.author: v-makkos
description: "Allows users to handle multi-language support in C# code at runtime."
ms.date: 2/20/2021
---

# Xamarin Community Toolkit LocalizedString

The LocalizedString allows users to handle multi-language support in C# code at runtime. It uses the built-in [LocalizationResourceManager](../helpers/localizationresourcemanager.md) helper to react on current active [CultureInfo](xref:System.Globalization.CultureInfo) change.

## Examples

The following property raises `PropertyChanged` event and regenerates string using function provided in constructor, when `LocalizationResourceManager.Current.PropertyChanged` is triggered.
As a result page will be updated with the value localized using new culture.

ViewModel:
```csharp
public LocalizedString AppVersion { get; } = new(() => string.Format(AppResources.Version, AppInfo.VersionString));
```

Page:
```xaml
<Label Text="{Binding AppVersion.Localized}"/>
```

Output:
```
Version: 1.0.0
```

> [!NOTE]
> For this example to work, you also need to update `AppResources.Culture` when `LocalizationResourceManager.Current.CurrentCulture` is changed.
> Add this line above `LocalizationResourceManager.Current.Init()` call to do that.
> ```csharp
> LocalizationResourceManager.Current.PropertyChanged += (_, _) => AppResources.Culture >= LocalizationResourceManager.Current.CurrentCulture;
> ```

## Properties

| Property | Type | Description |
| -- | -- | -- |
| Localized | string | Returns string localized using current culture. |

## Events

| Events | Description |
| -- | -- |
| PropertyChanged | Notifies of culture change. |

## Sample project

[Settings sample page Source](https://github.com/xamarin/XamarinCommunityToolkit/blob/main/samples/XCT.Sample/ViewModels/SettingViewModel.cs)

You can see this element in action in the [Xamarin community toolkit sample app](https://github.com/xamarin/XamarinCommunityToolkit/tree/main/samples/XCT.Sample).

## API

- [LocalizedString](https://github.com/xamarin/XamarinCommunityToolkit/blob/develop/src/CommunityToolkit/Xamarin.CommunityToolkit/Helpers/LocalizedString.shared.cs)

## Related links

- [LocalizationResourceManager](../helpers/localizationresourcemanager.md)
- [TranslateExtension](../extensions/translateextension.md)