51 lines
3.4 KiB
Markdown
51 lines
3.4 KiB
Markdown
|
|
---
|
|||
|
|
source_files:
|
|||
|
|
- DataPRO/Modules/Hardware/AddEditHardware/Resources/TranslateExtension.cs
|
|||
|
|
- DataPRO/Modules/Hardware/AddEditHardware/Resources/StringResources.Designer.cs
|
|||
|
|
generated_at: "2026-04-16T04:36:34.790176+00:00"
|
|||
|
|
model: "Qwen/Qwen3-Coder-Next-FP8"
|
|||
|
|
schema_version: 1
|
|||
|
|
sha256: "df6887e304a35dc7"
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# Resources
|
|||
|
|
|
|||
|
|
## Documentation: `TranslateExtension` Markup Extension
|
|||
|
|
|
|||
|
|
### 1. Purpose
|
|||
|
|
This module provides a WPF `MarkupExtension` (`TranslateExtension`) that enables declarative localization of UI strings in XAML. It allows UI elements (e.g., `TextBlock.Text`, `Button.Content`) to bind to localized string resources defined in `StringResources` by specifying a resource key. Its role is to abstract away manual resource lookup and support runtime culture switching in the `AddEditHardware` module’s UI.
|
|||
|
|
|
|||
|
|
### 2. Public Interface
|
|||
|
|
- **`TranslateExtension(string key)`**
|
|||
|
|
Constructor. Accepts a string `key` corresponding to a resource name in `StringResources`. Stores the key for later lookup.
|
|||
|
|
|
|||
|
|
- **`object ProvideValue(IServiceProvider serviceProvider)`**
|
|||
|
|
Overrides `MarkupExtension.ProvideValue`. Performs resource lookup at XAML load time:
|
|||
|
|
- Returns `"#stringnotfound#"` if `_key` is `null` or empty.
|
|||
|
|
- Returns the localized string from `StringResources.ResourceManager.GetString(_key)` if found.
|
|||
|
|
- Returns `"#stringnotfound# <key>"` (e.g., `"#stringnotfound# MyKey"`) if the key is non-empty but no matching resource exists.
|
|||
|
|
|
|||
|
|
### 3. Invariants
|
|||
|
|
- `_key` is immutable after construction (no setter or mutation).
|
|||
|
|
- The extension **always returns a `string`** (per `[MarkupExtensionReturnType(typeof(string))]`).
|
|||
|
|
- Lookup is **case-sensitive** (relies on `ResourceManager.GetString(string)` behavior).
|
|||
|
|
- If a resource key is missing, the fallback string explicitly includes the original key for debugging visibility.
|
|||
|
|
|
|||
|
|
### 4. Dependencies
|
|||
|
|
- **Depends on**:
|
|||
|
|
- `System.Windows.Markup` (for `MarkupExtension` base class).
|
|||
|
|
- `AddEditHardware.Resources.StringResources` (strongly-typed resource class generated from `.resx`).
|
|||
|
|
- `System.Resources.ResourceManager` (used internally via `StringResources.ResourceManager`).
|
|||
|
|
- **Used by**:
|
|||
|
|
- XAML files in the `AddEditHardware` module (e.g., `AddEditHardwareView.xaml`) to localize UI elements.
|
|||
|
|
- No direct runtime dependencies beyond WPF framework and the `StringResources` assembly.
|
|||
|
|
|
|||
|
|
### 5. Gotchas
|
|||
|
|
- **Static resource resolution**: `ProvideValue` is called **once at XAML load time**, not at runtime. Changing `StringResources.Culture` *after* XAML is parsed will **not** update already-bound UI elements.
|
|||
|
|
- **Fallback format**: Missing keys yield `"#stringnotfound# <key>"`, which may appear in UI if keys are misspelled or missing from `.resx`. This is intentional for debugging but may confuse end users.
|
|||
|
|
- **No null-safety for `serviceProvider`**: The implementation does not validate `serviceProvider`, though WPF typically provides a valid instance.
|
|||
|
|
- **Hardcoded fallback**: The `"#stringnotfound#"` prefix is hardcoded and not configurable.
|
|||
|
|
- **No async or caching optimization**: Each usage triggers a `ResourceManager.GetString()` call; no caching is applied within `TranslateExtension` (though `ResourceManager` itself caches resources internally).
|
|||
|
|
- **Auto-generated resource class**: `StringResources` is auto-generated; manual edits will be overwritten. Resource keys must match exactly (case-sensitive) with entries in the `.resx` file.
|
|||
|
|
|
|||
|
|
None identified beyond the above.
|