4.4 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:38:39.050309+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 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 inProvideValue. -
object ProvideValue(IServiceProvider serviceProvider)
OverridesMarkupExtension.ProvideValue. Performs the resource lookup:- If
_keyisnullor 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
IServiceProviderparameter is unused; the method does not interact with the WPF service provider beyond satisfying the interface contract. - If
3. Invariants
_keyis immutable after construction (stored in areadonlyfield).- 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
_keybeyond null/empty checks; invalid keys (e.g., containing special characters) are passed directly toResourceManager.GetString, which may returnnull. - Thread-safety: Relies on
StringResources.ResourceManager, which is documented as thread-safe for concurrent reads (standardResourceManagerbehavior).
4. Dependencies
- Depends on:
System.Windows.Markup(forMarkupExtensionandMarkupExtensionReturnType).ExtraProperties.Resources.StringResources(strongly-typed resource class).System.Resources.ResourceManager(viaStringResources.ResourceManager).
- Depended on by:
- XAML files in the
ExtraPropertiesmodule (inferred from usage as aMarkupExtension). - No direct programmatic callers are visible in the provided source; usage is exclusively via XAML markup.
- XAML files in the
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 viaResourceManager.GetString(string)(which respectsThread.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
ProvideValuecall performs a freshResourceManager.GetStringlookup. WhileResourceManagercaches internally, repeated use in dynamic UI (e.g., data templates) may incur minor overhead. - Auto-generated resource class:
StringResourcesis auto-generated; manual edits toStringResources.Designer.cswill be overwritten. Resource keys must match those in the corresponding.resxfile(s). - No null-safety for
ResourceManager.GetString: The null-coalescing operator (??) handlesnullresults, but ifResourceManageritself is misconfigured (e.g., wrong base name),GetStringmay 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.,
InvalidLineexpects{0}placeholders to be filled elsewhere, not via this extension).