53 lines
1.8 KiB
Markdown
53 lines
1.8 KiB
Markdown
|
|
---
|
||
|
|
source_files:
|
||
|
|
- Common/DTS.Common/Strings/TranslateExtension.cs
|
||
|
|
generated_at: "2026-04-17T15:40:47.154014+00:00"
|
||
|
|
model: "zai-org/GLM-5-FP8"
|
||
|
|
schema_version: 1
|
||
|
|
sha256: "5ba9d7fe887a2bf3"
|
||
|
|
---
|
||
|
|
|
||
|
|
# Documentation: TranslateExtension.cs
|
||
|
|
|
||
|
|
## 1. Purpose
|
||
|
|
|
||
|
|
This module provides a XAML markup extension for runtime string localization in WPF applications. It enables declarative resource lookup directly in XAML markup, allowing UI elements to reference localized strings by key rather than hardcoding values. The extension bridges the application's resource management system with XAML data binding infrastructure.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 2. Public Interface
|
||
|
|
|
||
|
|
### `TranslateExtension` Class
|
||
|
|
|
||
|
|
**Inherits from:** `MarkupExtension`
|
||
|
|
**Attribute:** `[MarkupExtensionReturnType(typeof(string))]`
|
||
|
|
|
||
|
|
#### Constructor
|
||
|
|
|
||
|
|
```csharp
|
||
|
|
public TranslateExtension(string key)
|
||
|
|
```
|
||
|
|
|
||
|
|
Creates a new instance of the extension with the specified resource key. The key is stored in a private readonly field `_key` and cannot be modified after construction.
|
||
|
|
|
||
|
|
#### Method: `ProvideValue`
|
||
|
|
|
||
|
|
```csharp
|
||
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
||
|
|
```
|
||
|
|
|
||
|
|
Returns the localized string for the key provided at construction.
|
||
|
|
|
||
|
|
**Behavior:**
|
||
|
|
- If `_key` is `null` or empty, returns the constant `NotFound` (`"#stringnotfound#"`).
|
||
|
|
- Otherwise, calls `Strings.ResourceManager.GetString(_key)` to retrieve the localized resource.
|
||
|
|
- If the resource lookup returns `null`, returns `NotFound + " " + _key` (e.g., `"#stringnotfound# MyKey"`).
|
||
|
|
- If the resource is found, returns the localized string value.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 3. Invariants
|
||
|
|
|
||
|
|
- **Key immutability:** The `_key` field is `readonly` and set exactly once at construction.
|
||
|
|
- **Non-null return:** `ProvideValue` always returns a non-null `string` (never returns `null` itself).
|
||
|
|
- **Fallback format:** Missing resources are always identifiable by the `#stringnotfound
|