3.6 KiB
3.6 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T02:52:05.025997+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 73358136e74291bd |
Strings
1. Purpose
TranslateExtension is a WPF MarkupExtension that enables declarative string localization in XAML by resolving resource keys to localized strings at runtime. It acts as a bridge between XAML markup and the application’s resource management system (Strings.ResourceManager), allowing UI elements to bind to localized text without requiring code-behind or view model properties. Its role is to simplify internationalization by enabling direct use of translation keys in XAML attributes (e.g., Text="{local:Translate MyButtonLabel}").
2. Public Interface
TranslateExtension(string key)
Constructor. Stores the resource key to be resolved. Throws no exceptions fornull/empty keys—these are handled at runtime.ProvideValue(IServiceProvider serviceProvider)(overridesMarkupExtension.ProvideValue)
Resolves the stored_keyto a localized string usingStrings.ResourceManager.GetString(_key). Returns the localized string if found; otherwise, returns"#stringnotfound#"(if_keyis null/empty) or"#stringnotfound# " + _key(if_keyis non-empty but not found).
3. Invariants
_keyis immutable after construction (stored in areadonlyfield).- The extension always returns a
string(per[MarkupExtensionReturnType(typeof(string))]). - If
_keyisnullor empty, the return value is exactly"#stringnotfound#". - If
_keyis non-empty butStrings.ResourceManager.GetString(_key)returnsnull, the return value is"#stringnotfound# " + _key(note the trailing space). - No validation is performed on
_keybeyond null/empty checks—invalid keys (e.g., containing spaces, special characters) are passed directly toResourceManager.GetString.
4. Dependencies
- Depends on:
System.Windows.Markup.MarkupExtension(base class)System(forstring.IsNullOrEmpty)DTS.Common.Strings.Strings.ResourceManager(assumed to be a staticResourceManagerinstance; not imported in this file but referenced in source).
- Used by:
- XAML parsers (e.g.,
XamlReader, design-time/runtime XAML compilation) when encountering{local:Translate ...}markup extensions. - UI components that consume localized strings via XAML (e.g.,
TextBlock.Text,Button.Content).
- XAML parsers (e.g.,
5. Gotchas
- No fallback for missing keys: The extension does not throw, log, or alert on missing keys—only returns a placeholder string. This may silently break UI text if keys are misspelled or resources are missing.
- Trailing space in error message: The placeholder for missing keys includes a trailing space (
" #key"), which may cause unintended whitespace in UI if not handled. - No culture support exposed: Uses
ResourceManager.GetString(string)without specifying aCultureInfo, relying on the default behavior (typically the current UI culture). No mechanism to override culture per extension call. - No caching: Each
ProvideValuecall re-invokesResourceManager.GetString, which may be inefficient if the same key is resolved frequently (thoughResourceManageritself caches internally). - No null-safety for
Strings.ResourceManager: IfStrings.ResourceManagerisnullat runtime (e.g., due to initialization order), aNullReferenceExceptionwill occur. - None identified from source alone for additional edge cases beyond the above.