--- source_files: - DataPRO/Modules/Hardware/HardwareList/Resources/TranslateExtension.cs - DataPRO/Modules/Hardware/HardwareList/Resources/StringResources.Designer.cs generated_at: "2026-04-16T04:37:17.643250+00:00" model: "Qwen/Qwen3-Coder-Next-FP8" schema_version: 1 sha256: "41df486c1d53cd9a" --- # Resources ## Documentation: `TranslateExtension` Markup Extension --- ### 1. Purpose The `TranslateExtension` class is a WPF `MarkupExtension` used to enable localized string binding in XAML. It resolves a given string key at runtime by querying the `StringResources` strongly-typed resource class (which wraps the application’s `.resx`-based resources), returning the corresponding localized value. If the key is missing or empty, it returns a consistent fallback string (`#stringnotfound#` or `#stringnotfound# `), allowing UI elements to gracefully handle missing translations. This extension is essential for internationalization of the HardwareList module’s UI. --- ### 2. Public Interface #### `TranslateExtension` class - **Namespace**: `HardwareList` - **Base Type**: `System.Windows.Markup.MarkupExtension` ##### Constructor ```csharp public TranslateExtension(string key) ``` - **Parameters**: - `key` (`string`): The resource key used to look up a localized string in `StringResources`. - **Behavior**: Stores the key for later resolution during `ProvideValue`. ##### Override ```csharp public override object ProvideValue(IServiceProvider serviceProvider) ``` - **Parameters**: - `serviceProvider` (`IServiceProvider`): WPF service provider (unused in current implementation). - **Returns**: - `string`: The localized string corresponding to `_key`, or a fallback string if the key is null/empty or not found. - **Behavior**: - If `_key` is `null` or empty → returns `"#stringnotfound#"`. - Otherwise, calls `StringResources.ResourceManager.GetString(_key)`. - If a matching resource exists → returns the string value. - If no match → returns `"#stringnotfound# " + _key`. > **Note**: The `MarkupExtensionReturnType` attribute indicates the extension always returns a `string`. --- ### 3. Invariants - `_key` is immutable after construction (no setter or mutation). - `StringResources.ResourceManager.GetString(key)` is assumed to be thread-safe (as per .NET `ResourceManager` behavior). - The fallback string `#stringnotfound#` is **hardcoded** and must not be localized itself. - Empty or null keys are treated identically and return the base fallback string (no key suffix). - If a key exists in `StringResources.Designer.cs` but the underlying `.resx` file lacks the entry, `GetString` returns `null`, triggering the fallback. --- ### 4. Dependencies #### Dependencies *on* this module: - **WPF Framework**: Requires `System.Windows.Markup` and `System` (for `ResourceManager`, `IServiceProvider`). - **`HardwareList.Resources.StringResources`**: This module depends on the auto-generated `StringResources` class for resource lookup. - **XAML Parser**: Used via `MarkupExtension` mechanism in XAML (e.g., `{local:Translate KeyName}`). #### Dependencies *of* this module: - None beyond standard .NET libraries (`System`, `System.Windows`). - `StringResources` is generated from `.resx` files (not visible here), but the module assumes its existence and structure. --- ### 5. Gotchas - **No caching of resolved values**: Each `ProvideValue` call re-queries `ResourceManager.GetString`, which is acceptable but could be optimized if performance becomes critical. - **Fallback format is not user-friendly**: The fallback `"#stringnotfound# "` may appear in the UI if keys are missing—ensure translators or developers catch these during testing. - **Case sensitivity**: Resource keys are case-sensitive (e.g., `"Add"` ≠ `"add"`). A mismatch yields the fallback string. - **Culture handling**: Uses the current UI culture (`CultureInfo.CurrentUICulture`) implicitly via `ResourceManager.GetString`, unless `StringResources.Culture` is explicitly set (which is possible but not done here). - **No null-safety for `serviceProvider`**: While `serviceProvider` is unused, `ProvideValue` does not guard against null—though WPF guarantees non-null in normal usage. - **Auto-generated resource class**: `StringResources.Designer.cs` is auto-generated; manual edits will be overwritten. Ensure keys used in `TranslateExtension` exist in the `.resx` file. > **None identified from source alone.**