4.0 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:35:33.501804+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 79b93425a9c97e71 |
Resources
Documentation: TranslateExtension Markup Extension
1. Purpose
The TranslateExtension class is a WPF MarkupExtension that enables declarative localization of UI text directly in XAML. It resolves string resources by key at runtime using the StringResources strongly-typed resource class, returning localized strings or a standardized fallback for missing keys. This module exists to support multi-language UIs in the DatabaseServices module by abstracting resource lookup into a XAML-friendly syntax (e.g., {local:Translate KeyName}), decoupling UI text from code and enabling runtime culture switching.
2. Public Interface
-
TranslateExtension(string key)
Constructor. Accepts a resource key (e.g.,"ClearingLocalDb") to look up. Throws no exceptions on invalid input—invalid keys are handled at lookup time. -
override object ProvideValue(IServiceProvider serviceProvider)
WPF framework method invoked during XAML parsing. Returns the localized string corresponding to_key, or a fallback value if the key is missing/empty.- If
_keyisnullor empty: returns"#stringnotfound#". - If
StringResources.ResourceManager.GetString(_key)returnsnull: returns"#stringnotfound# " + _key. - Otherwise: returns the localized string.
- If
3. Invariants
_keyis immutable after construction (stored in areadonlyfield).- The returned value is always a
string(per[MarkupExtensionReturnType(typeof(string))]). - No exceptions are thrown during
ProvideValue—all error cases return a fallback string. - Resource lookup uses the current UI culture (via
StringResources.Culture), which may be set externally (e.g., by WPF’sFrameworkElement.Languageor manual assignment). - The
StringResourcesclass is auto-generated and must be kept in sync with.resxfiles; changes to.resxrequire regeneration.
4. Dependencies
- Depends on:
System.Windows.Markup(forMarkupExtensionandMarkupExtensionReturnType).DatabaseServices.Resources.StringResources(strongly-typed resource class).System.Resources.ResourceManager(viaStringResources.ResourceManager).
- Used by:
- XAML files in the
DatabaseServicesmodule (e.g.,Window.xaml,UserControl.xaml) to bind localized text to UI elements. - No direct programmatic callers in the provided source—usage is exclusively via XAML markup.
- XAML files in the
5. Gotchas
- Hardcoded fallback prefix: The
"#stringnotfound#"string is hardcoded; changing it requires updating bothTranslateExtensionand any tests/scripts expecting this pattern. - No caching of lookups:
ResourceManager.GetString(_key)is called on everyProvideValueinvocation. WhileResourceManagercaches internally, repeated lookups for the same key in high-frequency UI scenarios (e.g., data-bound lists) may incur overhead. - Silent failure on missing keys: Missing keys produce visible text like
"#stringnotfound# ClearingLocalDb"in the UI, which may confuse end users. No logging or telemetry is performed. - No null-safety for
serviceProvider: TheserviceProviderparameter is unused, but if future changes require it (e.g., for service resolution), this could break. - Auto-generated resource class:
StringResources.Designer.csis auto-generated—manual edits are unsafe and will be overwritten. Resource keys must match exactly (case-sensitive) with entries in the.resxfile. - No support for pluralization/formatting: The extension only supports simple key→string lookups; composite strings (e.g.,
"Connected to: {0}") require manual formatting in code-behind or XAML.
None identified beyond these.