53 lines
4.4 KiB
Markdown
53 lines
4.4 KiB
Markdown
|
|
---
|
|||
|
|
source_files:
|
|||
|
|
- DataPRO/Modules/ISO/ExtraProperties/Resources/TranslateExtension.cs
|
|||
|
|
- DataPRO/Modules/ISO/ExtraProperties/Resources/StringResources.Designer.cs
|
|||
|
|
generated_at: "2026-04-16T04:38:39.050309+00:00"
|
|||
|
|
model: "Qwen/Qwen3-Coder-Next-FP8"
|
|||
|
|
schema_version: 1
|
|||
|
|
sha256: "d990e5734e630810"
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
# Resources
|
|||
|
|
|
|||
|
|
## 1. Purpose
|
|||
|
|
`TranslateExtension` is a WPF `MarkupExtension` that enables declarative localization of UI strings in XAML by resolving resource keys to localized string values at runtime. It acts as a bridge between XAML markup and the application’s strongly-typed resource system (`StringResources`), allowing developers to bind localized text directly in XAML using `{Translate key}` syntax. Its role is to centralize and simplify string localization, avoiding hardcoded strings and supporting culture-specific display via the underlying `ResourceManager`.
|
|||
|
|
|
|||
|
|
## 2. Public Interface
|
|||
|
|
- **`TranslateExtension(string key)`**
|
|||
|
|
Constructor. Initializes the extension with a resource key (`_key`) to look up. Throws no exceptions on invalid input; invalid keys are handled in `ProvideValue`.
|
|||
|
|
|
|||
|
|
- **`object ProvideValue(IServiceProvider serviceProvider)`**
|
|||
|
|
Overrides `MarkupExtension.ProvideValue`. Performs the resource lookup:
|
|||
|
|
- If `_key` is `null` or empty → returns `"#stringnotfound#"`.
|
|||
|
|
- Otherwise, calls `StringResources.ResourceManager.GetString(_key)`.
|
|||
|
|
- If a matching string is found → returns it.
|
|||
|
|
- If not found → returns `"#stringnotfound# " + _key` (note the trailing space).
|
|||
|
|
|
|||
|
|
*Note:* The `IServiceProvider` parameter is unused; the method does not interact with the WPF service provider beyond satisfying the interface contract.
|
|||
|
|
|
|||
|
|
## 3. Invariants
|
|||
|
|
- `_key` is immutable after construction (stored in a `readonly` field).
|
|||
|
|
- The returned value is always a `string`.
|
|||
|
|
- A missing or empty key **always** yields `"#stringnotfound#"`.
|
|||
|
|
- A key with no corresponding resource entry **always** yields `"#stringnotfound# <key>"` (with a space before the key).
|
|||
|
|
- No validation is performed on `_key` beyond null/empty checks; invalid keys (e.g., containing special characters) are passed directly to `ResourceManager.GetString`, which may return `null`.
|
|||
|
|
- Thread-safety: Relies on `StringResources.ResourceManager`, which is documented as thread-safe for concurrent reads (standard `ResourceManager` behavior).
|
|||
|
|
|
|||
|
|
## 4. Dependencies
|
|||
|
|
- **Depends on:**
|
|||
|
|
- `System.Windows.Markup` (for `MarkupExtension` and `MarkupExtensionReturnType`).
|
|||
|
|
- `ExtraProperties.Resources.StringResources` (strongly-typed resource class).
|
|||
|
|
- `System.Resources.ResourceManager` (via `StringResources.ResourceManager`).
|
|||
|
|
- **Depended on by:**
|
|||
|
|
- XAML files in the `ExtraProperties` module (inferred from usage as a `MarkupExtension`).
|
|||
|
|
- No direct programmatic callers are visible in the provided source; usage is exclusively via XAML markup.
|
|||
|
|
|
|||
|
|
## 5. Gotchas
|
|||
|
|
- **Hardcoded placeholder:** The `"#stringnotfound#"` string is a literal constant—no external configuration or localization for this fallback exists.
|
|||
|
|
- **No culture override support:** The extension does not expose or respect `StringResources.Culture`; it uses the current UI culture implicitly via `ResourceManager.GetString(string)` (which respects `Thread.CurrentThread.CurrentUICulture`).
|
|||
|
|
- **Inconsistent error formatting:** Missing keys yield `"#stringnotfound# <key>"`, which may be visually jarring in UI (e.g., `"#stringnotfound# MyKey"`). This is likely intentional for debugging but could confuse end users.
|
|||
|
|
- **No caching of resolved values:** Each `ProvideValue` call performs a fresh `ResourceManager.GetString` lookup. While `ResourceManager` caches internally, repeated use in dynamic UI (e.g., data templates) may incur minor overhead.
|
|||
|
|
- **Auto-generated resource class:** `StringResources` is auto-generated; manual edits to `StringResources.Designer.cs` will be overwritten. Resource keys must match those in the corresponding `.resx` file(s).
|
|||
|
|
- **No null-safety for `ResourceManager.GetString`:** The null-coalescing operator (`??`) handles `null` results, but if `ResourceManager` itself is misconfigured (e.g., wrong base name), `GetString` may throw—though this is unlikely in practice.
|
|||
|
|
- **No support for parameterized strings:** The extension only resolves *static* keys. It does not support string formatting (e.g., `InvalidLine` expects `{0}` placeholders to be filled elsewhere, not via this extension).
|