4.3 KiB
4.3 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:51:19.579629+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 9ca0cf52c1c54e32 |
Resources
Documentation: TranslateExtension Markup Extension
1. Purpose
The TranslateExtension class is a WPF MarkupExtension used to enable localized string binding in XAML. It resolves resource keys (e.g., "Name", "Description") to their corresponding localized string values at runtime by querying the strongly-typed StringResources class. This allows UI elements (e.g., TextBlock.Text, HeaderedContentControl.Header) to display culture-specific text without hardcoding strings, supporting internationalization of the TestSetupsList module.
2. Public Interface
TranslateExtension class
-
Constructor
public TranslateExtension(string key)- Parameters:
key: The resource key (e.g.,"Name","LastModifiedBy") used to look up a localized string inStringResources.
- Behavior: Stores the key for later resolution during
ProvideValue.
- Parameters:
-
ProvideValuemethodpublic override object ProvideValue(IServiceProvider serviceProvider)- Returns:
- If
_keyisnullor empty →"#stringnotfound#" - If
StringResources.ResourceManager.GetString(_key)returns a non-null value → that string - Otherwise →
"#stringnotfound# " + _key(e.g.,"#stringnotfound# UnknownKey")
- If
- Behavior: Performs the actual resource lookup. Does not use the
serviceProviderparameter (per source).
- Returns:
3. Invariants
_keyis immutable after construction (no setter, no modification inProvideValue).- Resource keys are expected to match exact keys in the
.resxfile (e.g.,"Name","PreTriggerSeconds"), case-sensitive. - The fallback string
"#stringnotfound#"is hardcoded and used for both missing keys and empty keys. StringResources.ResourceManager.GetString(_key)is called with the current UI culture (viaCultureInfostored inStringResources.Culture), not thread culture directly.StringResourcesis auto-generated; manual edits toStringResources.Designer.cswill be overwritten.
4. Dependencies
Dependencies of TranslateExtension:
System.Windows.Markup.MarkupExtension(WPF base class)TestSetupsList.Resources.StringResources(strongly-typed resource class)- Relies on
System.Resources.ResourceManagerandSystem.Globalization.CultureInfo - Requires the
StringResources.resxfile to be compiled into theTestSetupsListassembly (implied by"TestSetupsList.Resources.StringResources"inResourceManagerconstructor).
- Relies on
Dependencies on TranslateExtension:
- XAML files in the
TestSetupsListmodule that use{local:Translate KeyName}syntax (wherelocalmaps toTestSetupsListnamespace). - No direct runtime dependencies beyond WPF and the resource assembly.
5. Gotchas
- No null-safety for missing keys: A key like
"NonExistent"yields"#stringnotfound# NonExistent"— notnull, not an exception. This may cause visible artifacts in UI if not handled. - Empty key handling: Passing
""ornullto the constructor returns"#stringnotfound#"silently — no warning or exception. - No caching of resolved values:
ProvideValuecallsResourceManager.GetString(_key)on every invocation (e.g., if used in aDataTemplatewith many items). This is likely acceptable for typical UI strings but could be optimized. - No support for format strings or parameters: Unlike
String.Format, this extension does not support placeholders (e.g.,"Hello {0}"). Resource keys must resolve to fully formed strings. - Designer.cs auto-generation warning: Manual changes to
StringResources.Designer.cswill be lost on rebuild. Resource keys must be added/modified via the.resxdesigner or file. - No
serviceProviderusage: TheProvideValuemethod ignores the WPF-providedIServiceProvider, meaning it cannot resolve culture dynamically per context (e.g., per element). Relies solely onStringResources.Culture.
None identified beyond the above.