4.4 KiB
4.4 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:37:17.643250+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 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# <key>), 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
public TranslateExtension(string key)
- Parameters:
key(string): The resource key used to look up a localized string inStringResources.
- Behavior: Stores the key for later resolution during
ProvideValue.
Override
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
_keyisnullor empty → returns"#stringnotfound#". - Otherwise, calls
StringResources.ResourceManager.GetString(_key).- If a matching resource exists → returns the string value.
- If no match → returns
"#stringnotfound# " + _key.
- If
Note
: The
MarkupExtensionReturnTypeattribute indicates the extension always returns astring.
3. Invariants
_keyis immutable after construction (no setter or mutation).StringResources.ResourceManager.GetString(key)is assumed to be thread-safe (as per .NETResourceManagerbehavior).- 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.csbut the underlying.resxfile lacks the entry,GetStringreturnsnull, triggering the fallback.
4. Dependencies
Dependencies on this module:
- WPF Framework: Requires
System.Windows.MarkupandSystem(forResourceManager,IServiceProvider). HardwareList.Resources.StringResources: This module depends on the auto-generatedStringResourcesclass for resource lookup.- XAML Parser: Used via
MarkupExtensionmechanism in XAML (e.g.,{local:Translate KeyName}).
Dependencies of this module:
- None beyond standard .NET libraries (
System,System.Windows). StringResourcesis generated from.resxfiles (not visible here), but the module assumes its existence and structure.
5. Gotchas
- No caching of resolved values: Each
ProvideValuecall re-queriesResourceManager.GetString, which is acceptable but could be optimized if performance becomes critical. - Fallback format is not user-friendly: The fallback
"#stringnotfound# <key>"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 viaResourceManager.GetString, unlessStringResources.Cultureis explicitly set (which is possible but not done here). - No null-safety for
serviceProvider: WhileserviceProvideris unused,ProvideValuedoes not guard against null—though WPF guarantees non-null in normal usage. - Auto-generated resource class:
StringResources.Designer.csis auto-generated; manual edits will be overwritten. Ensure keys used inTranslateExtensionexist in the.resxfile.
None identified from source alone.