5.4 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:55:56.339798+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 3fc30f8d93dc5d3b |
Resources
Documentation: TranslateExtension Markup Extension
Purpose
The TranslateExtension class provides a XAML markup extension for localized string lookup within the ChannelCodes module. It enables declarative binding of UI text to resource keys defined in StringResources.resx, supporting internationalization by resolving keys at runtime using the .NET ResourceManager. This allows UI elements (e.g., TextBlock.Text, Button.Content) to display culture-specific strings without hardcoding values or writing code-behind.
Public Interface
TranslateExtension(string key)
Constructor
- Parameter:
key(string) – The resource key to look up (e.g.,"ChannelCode","DuplicateISOChannelCode"). - Behavior: Stores the key for later resolution. No validation is performed at construction time.
public override object ProvideValue(IServiceProvider serviceProvider)
Markup Extension Entry Point
- Parameter:
serviceProvider(IServiceProvider) – Provides services to the markup extension (unused in current implementation). - Returns:
string: The localized string corresponding to_key, orNotFound("#stringnotfound#"): If_keyisnullor empty,NotFound + " " + _key: If_keyis non-empty but no matching resource string is found.
- Behavior:
- Checks if
_keyisnullor empty → returns"#stringnotfound#". - Calls
StringResources.ResourceManager.GetString(_key)to retrieve the localized string. - If
GetStringreturnsnull, appends the key to theNotFoundprefix (e.g.,"#stringnotfound# MissingISOCode").
- Checks if
Note
: The
MarkupExtensionReturnTypeattribute indicates this extension always returns astring.
Invariants
- Key Handling:
- Empty or
nullkeys are treated as invalid and always resolve to"#stringnotfound#". - Non-empty keys that lack a corresponding resource entry resolve to
"#stringnotfound# <key>".
- Empty or
- Resource Lookup:
- Uses
StringResources.ResourceManager.GetString(key)with the current UI culture (viaCultureInfo). - No fallback logic beyond the
nullcheck inProvideValue.
- Uses
- Thread Safety:
StringResources.ResourceManageris lazily initialized and cached (perStringResourcesclass), butTranslateExtensionitself is stateful per instance (stores_key). Reuse across threads is safe only if the same instance is not shared concurrently (standard for markup extensions).
Dependencies
Depends On
System.Windows.Markup.MarkupExtension(WPF framework)ChannelCodes.Resources.StringResources(strongly-typed resource class)- Specifically relies on
StringResources.ResourceManagerand itsGetString(string)method.
- Specifically relies on
System(forstring.IsNullOrEmpty,ResourceManager,CultureInfo)
Used By
- XAML files in the
ChannelCodesmodule (e.g.,*.xamlpages) where localized text is declared via{local:Translate KeyName}. - Example usage:
<TextBlock Text="{local:Translate ChannelCode}" />
Gotchas
-
No Resource Key Validation at Compile Time:
- Typos in resource keys (e.g.,
"ChannelCodee"instead of"ChannelCode") will silently resolve to"#stringnotfound# ChannelCodee"at runtime. - No build-time or design-time warning is generated for missing keys.
- Typos in resource keys (e.g.,
-
Hardcoded
NotFoundPrefix:- The
"#stringnotfound#"prefix is hardcoded and may be visible in the UI if a key is missing. This is likely intentional for debugging but could confuse end users.
- The
-
No Caching of Resolved Strings:
ProvideValuecallsResourceManager.GetString(_key)on every invocation. WhileResourceManagercaches resources internally, repeated use of the same key in a UI (e.g., in aDataTemplate) may incur unnecessary lookups. Consider caching if performance is critical.
-
Culture Handling:
- Respects
StringResources.Culture(settable viaStringResources.Culture = ...). IfCultureisnull, the UI thread’sCurrentUICultureis used. - No explicit handling for fallback cultures (e.g.,
en-US→en).
- Respects
-
Auto-Generated Resource Class:
StringResources.Designer.csis auto-generated. Manual edits will be overwritten. New strings must be added via.resxfiles and regenerated.
-
No Support for Format Strings:
TranslateExtensiondoes not support parameterized resources (e.g.,"Error: {0} is missing"). WhileStringResourceshas entries likeDuplicateISOChannelCode(which uses{0}), the extension itself does not accept or apply formatting arguments.- To use formatted strings, the caller must manually construct the key (e.g.,
"DuplicateISOChannelCode"), but the resolved string will remain unformatted unless the caller appliesstring.Format(not done here).
-
WPF-Specific:
- Depends on
System.Windows.Markup, making it unusable outside WPF (e.g., in MAUI, WinUI, or console apps).
- Depends on
Documentation generated from source files dated to .NET Framework 4.0 and WPF. No external dependencies beyond standard .NET libraries.