Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dwndland/chapter.net.wpf.localization
Provides objects and helpers for an easy localization of the WPF application.
https://github.com/dwndland/chapter.net.wpf.localization
csharp localization translation wpf
Last synced: about 15 hours ago
JSON representation
Provides objects and helpers for an easy localization of the WPF application.
- Host: GitHub
- URL: https://github.com/dwndland/chapter.net.wpf.localization
- Owner: dwndland
- License: mit
- Created: 2024-12-01T17:23:01.000Z (about 2 months ago)
- Default Branch: master
- Last Pushed: 2024-12-01T17:23:45.000Z (about 2 months ago)
- Last Synced: 2025-01-01T20:13:15.048Z (16 days ago)
- Topics: csharp, localization, translation, wpf
- Language: C#
- Homepage:
- Size: 31.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Chapter.Net.WPF.Localization Library
## Overview
Provides objects and helpers for an easy localization of the WPF application.## Features
- **Maintain current culture:** Use the Localizer to set and get the CurrentCulture and CurrentLanguage.
- **Culture and language change events:** After set CurrentCulture and CurrentLanguage will raise also Changing and Changed events.
- **Load translations:** Using the Translator to load translations by a key from the application resources.
- **Names placeholders:** For proper localization the translations can contain named placeholders the Translator can format.
- **FormatterTextBlock:** Formats the given translation.
- **FormatterConverter:** Puts the given bound values to the Translator.Format.
- **BBTextBlock:** This classes splits text with BB code into formatted parts.## Getting Started
1. **Installation:**
- Install the Chapter.Net.WPF.Localization library via NuGet Package Manager:
```bash
dotnet add package Chapter.Net.WPF.Localization
```2. **Current Culture:**
- Set the current culture.
```csharp
Localizer.CurrentCulture = new CultureInfo("de-DE");
```
- Get informed about that be changed.
```csharp
Localizer.CultureChanged += OnCultureChanged;private void OnCultureChanged(CultureChangeEventArgs e)
{
//e.OldCulture
//e.NewCulture
}
```3. **Current Culture:**
- Set the current language.
```csharp
Localizer.CurrentLanguage = new CultureInfo("de-DE");
```
- Get informed about that be changed.
```csharp
Localizer.LanguageChanged += OnLanguageChanged;private void OnLanguageChanged(CultureChangeEventArgs e)
{
//e.OldCulture
//e.NewCulture
}
```4. **Load translations:**
- In XAML, use the build in WPF features
```xaml
```
- In C# code, use the translator
```csharp
var title = Translator.GetString(TransKeys.Demo.Title);
```5. **Names placeholders:**
- For proper translations, give the translator a hint what a placeholder stands for:
```xaml
Do you want to open the {fileName}?
```
```csharp
var title = Translator.GetString(TransKeys.Demo.Title, "{fileName}", fileName);
```
- If you have the string loaded already and want to replace a placeholder multiple times, it can be optimized
```csharp
var translation = Translator.GetString(TransKeys.Demo.Title);
var title1 = translation.Format(translation, "{fileName}", fileName1);
var title2 = translation.Format(translation, "{fileName}", fileName2);
var title3 = translation.Format(translation, "{fileName}", fileName3);
```6. **FormatterTextBlock:**
- Takes the given string and executes placeholder formating on it:
```xaml
```7. **FormatterConverter:**
- Puts the given bound values to the Translator.Format:
```xaml
{firstName}
{lastName}
```8. **BBTextBlock:**
- This classes splits text with BB code into formatted parts:
```xaml
```## Example
- Create dictionary (best in an own dll) with the translation
```xamlsomething [VERSION]
```
- Use the dictionary in your application
```xaml
```
or
```csharp
var dic = Application.Current.Resources.MergedDictionaries;
dic.Insert(0, new ResourceDictionary { Source = new Uri("/MyApplication;component/Translations.xaml", UriKind.RelativeOrAbsolute) });
```
- Use the translation with formatting
```csharp
Translator.GetString("Something", "[VERSION]", version.ToString(3))
```## FAQ
**Q:** Whats the difference between CurrentCulture and CurrentLanguage?
**A:** CurrentCulture set the CultureInfo.CurrentCulture and more. That culture is to use for like formatting decimal places, date formats, time formats and time zones.
The CurrentLanguage sets the CultureInfo.CurrentUICulture and defines for which language the translations shall be shown.**Q:** Why CurrentCulture and CurrentLanguage are separated from each other?
**A:** It can happen that the application runs on a English Windows System with its format, but the user logging in on the application sees the texts in Japanese.
In that cases its a best practice to show the culture like in windows, English US and change only the translations.
Localizer.CurrentCulture = new CultureInfo("en-US");
Localizer.CurrentLanguage = new CultureInfo("ja-JP");**Q:** I loaded my translation.dll into passolo but no string is shown from the Translation.xaml. Whats wrong?
**A:** Passolo checkes the translations using their ID, so beside the x:Key, set also the x:Uid, then all translations are shown.**Q:** Why should I use named placeholders?
**A:** You can use placeholders like {0} of course, but its better for the translator to know what {0} can be, to find the proper translation in the right grammar.
In that case a named placeholder gives pretty nice hints what it can be. Like {number}, {fileName}, {userName} or such.## Links
* [NuGet](https://www.nuget.org/packages/Chapter.Net.WPF.Localization)
* [GitHub](https://github.com/dwndland/Chapter.Net.WPF.Localization)## License
Copyright (c) David Wendland. All rights reserved.
Licensed under the MIT License. See LICENSE file in the project root for full license information.