76 lines
4.1 KiB
Markdown
76 lines
4.1 KiB
Markdown
|
|
---
|
||
|
|
source_files:
|
||
|
|
- DataPRO/Modules/SystemSettings/DBImportExport/Resources/TranslateExtension.cs
|
||
|
|
- DataPRO/Modules/SystemSettings/DBImportExport/Resources/StringResources.Designer.cs
|
||
|
|
generated_at: "2026-04-16T04:40:36.604143+00:00"
|
||
|
|
model: "Qwen/Qwen3-Coder-Next-FP8"
|
||
|
|
schema_version: 1
|
||
|
|
sha256: "4daa568e9b05fe61"
|
||
|
|
---
|
||
|
|
|
||
|
|
# Resources
|
||
|
|
|
||
|
|
## Documentation: `TranslateExtension` Markup Extension
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
### 1. Purpose
|
||
|
|
|
||
|
|
This module provides a WPF `MarkupExtension` (`TranslateExtension`) to support localization of UI strings in XAML. It enables declarative binding of localized text resources (from `StringResources`) directly in XAML markup, using a string key. The extension acts as a bridge between the strongly-typed `StringResources` class and XAML, returning localized strings at runtime or a fallback marker when a key is missing or invalid.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
### 2. Public Interface
|
||
|
|
|
||
|
|
#### `TranslateExtension` class
|
||
|
|
**Namespace:** `DBImportExport.Resources`
|
||
|
|
**Inherits:** `MarkupExtension`
|
||
|
|
|
||
|
|
- **Constructor**
|
||
|
|
```csharp
|
||
|
|
public TranslateExtension(string key)
|
||
|
|
```
|
||
|
|
Initializes a new instance with the specified resource key. The key is stored in the private readonly field `_key`.
|
||
|
|
|
||
|
|
- **`ProvideValue` method**
|
||
|
|
```csharp
|
||
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
||
|
|
```
|
||
|
|
Returns the localized string corresponding to `_key`, or a fallback value if the key is null/empty or the string is not found.
|
||
|
|
- If `_key` is `null` or empty → returns `"#stringnotfound#"`.
|
||
|
|
- If `StringResources.ResourceManager.GetString(_key)` returns `null` → returns `"#stringnotfound# " + _key`.
|
||
|
|
- Otherwise → returns the localized string.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
### 3. Invariants
|
||
|
|
|
||
|
|
- `_key` is immutable after construction (declared `readonly`).
|
||
|
|
- The extension **always** returns a `string` (per `[MarkupExtensionReturnType(typeof(string))]`).
|
||
|
|
- If a resource key is missing or invalid, the return value is guaranteed to begin with `"#stringnotfound#"` — no exceptions are thrown.
|
||
|
|
- The `StringResources.ResourceManager.GetString(...)` call is assumed to be thread-safe (standard for `ResourceManager`), but no explicit synchronization is present in `TranslateExtension`.
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
### 4. Dependencies
|
||
|
|
|
||
|
|
#### **Dependencies *of* this module:**
|
||
|
|
- `System.Windows.Markup` — for `MarkupExtension` base class and `MarkupExtensionReturnTypeAttribute`.
|
||
|
|
- `DBImportExport.Resources.StringResources` — specifically its `ResourceManager` property and generated strongly-typed accessors (e.g., `ExportFileBrowse_Filter`, `ImportView_Browse`, etc.).
|
||
|
|
|
||
|
|
#### **Dependencies *on* this module:**
|
||
|
|
- XAML files in the `DBImportExport` module (e.g., views for Import/Export) likely use `{local:Translate KeyName}` syntax to bind localized strings to UI elements (e.g., `TextBlock.Text`, `Button.Content`).
|
||
|
|
*(Inferred from the presence of `StringResources.Designer.cs` and usage of `TranslateExtension` in a `Resources` folder.)*
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
### 5. Gotchas
|
||
|
|
|
||
|
|
- **No null-safety for `serviceProvider`**: The `ProvideValue` method does not validate `serviceProvider` — though WPF typically provides it, misuse outside WPF may cause issues.
|
||
|
|
- **Hardcoded fallback**: The `"#stringnotfound#"` prefix is hardcoded and not configurable. This may cause visual artifacts if not handled in UI design.
|
||
|
|
- **No caching of lookups**: Each call to `ProvideValue` performs a fresh `ResourceManager.GetString(...)` lookup. While `ResourceManager` caches internally, repeated use in dynamic UIs (e.g., data templates) may incur minor overhead.
|
||
|
|
- **No culture switching support**: The extension uses the current UI culture implicitly via `StringResources.Culture`, but does not expose a way to override it per extension instance.
|
||
|
|
- **Auto-generated resource class**: `StringResources.Designer.cs` is auto-generated — manual edits will be lost. Resource keys must match exactly (case-sensitive) with entries in the `.resx` file.
|
||
|
|
- **No compile-time key validation**: Passing an incorrect key (e.g., `"ExportView_Browsse"`) will silently fall back to `"#stringnotfound# ExportView_Browsse"` at runtime.
|
||
|
|
|
||
|
|
> **None identified from source alone.** *(Note: The above are inferred based on common WPF localization patterns and the provided code structure.)*
|