3.4 KiB
3.4 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:36:34.790176+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 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 stringkeycorresponding to a resource name inStringResources. Stores the key for later lookup. -
object ProvideValue(IServiceProvider serviceProvider)
OverridesMarkupExtension.ProvideValue. Performs resource lookup at XAML load time:- Returns
"#stringnotfound#"if_keyisnullor 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.
- Returns
3. Invariants
_keyis 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(forMarkupExtensionbase class).AddEditHardware.Resources.StringResources(strongly-typed resource class generated from.resx).System.Resources.ResourceManager(used internally viaStringResources.ResourceManager).
- Used by:
- XAML files in the
AddEditHardwaremodule (e.g.,AddEditHardwareView.xaml) to localize UI elements. - No direct runtime dependencies beyond WPF framework and the
StringResourcesassembly.
- XAML files in the
5. Gotchas
- Static resource resolution:
ProvideValueis called once at XAML load time, not at runtime. ChangingStringResources.Cultureafter 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 validateserviceProvider, 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 withinTranslateExtension(thoughResourceManageritself caches resources internally). - Auto-generated resource class:
StringResourcesis auto-generated; manual edits will be overwritten. Resource keys must match exactly (case-sensitive) with entries in the.resxfile.
None identified beyond the above.