4.7 KiB
4.7 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:47:55.535596+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 036a24899acb6b7e |
Resources
Documentation: TranslateExtension Class
1. Purpose
The TranslateExtension class provides a XAML markup extension for localized string resolution within the RealtimeModule UI layer. It enables declarative binding of localized text in XAML (e.g., {Translate SomeKey}) by delegating string lookup to the StringResources strongly-typed resource class. This allows UI elements to support multi-language scenarios without hardcoding strings, and centralizes fallback behavior for missing translations.
2. Public Interface
TranslateExtension class
- Inherits:
System.Windows.Markup.MarkupExtension - Namespace:
RealtimeModule - Attributes:
[MarkupExtensionReturnType(typeof(string))]— Indicates the extension returns astring.
Constructor
TranslateExtension(string key)- Parameters:
key: The resource key (e.g.,"AvailableChannels","Search") used to look up the localized string.
- Behavior: Stores the key in a private readonly field
_key. No validation is performed on the key itself (e.g., null/empty allowed at construction).
- Parameters:
Override
public override object ProvideValue(IServiceProvider serviceProvider)- Parameters:
serviceProvider: XAML infrastructure service provider (unused in implementation).
- Returns:
string: The localized string if found; otherwise, a fallback string.
- Behavior:
- If
_keyisnullor empty → returns"#stringnotfound#". - Otherwise, calls
StringResources.ResourceManager.GetString(_key).- If the result is non-null → returns the localized string.
- If the result is
null(key not found) → returns"#stringnotfound# " + _key(note: space appended after#stringnotfound#).
- If
- Parameters:
3. Invariants
- Key lookup behavior:
- The extension always returns a
string. - A missing key (i.e.,
ResourceManager.GetString(key) == null) results in a deterministic fallback:"#stringnotfound# " + _key.
- The extension always returns a
- Null/empty key handling:
_key == nullor_key == ""→ returns"#stringnotfound#"(no key suffix).
- No side effects:
ProvideValueis pure (no mutation of state, no I/O beyond resource lookup).
- Thread-safety assumption:
- Relies on
ResourceManager.GetString, which is documented as thread-safe for read operations in .NET. No additional synchronization is implemented inTranslateExtension.
- Relies on
4. Dependencies
- Direct dependencies:
System.Windows.Markup(forMarkupExtensionandMarkupExtensionReturnTypeAttribute).RealtimeModule.Resources.StringResources(strongly-typed resource class).
- Indirect dependencies:
System.Resources.ResourceManager(viaStringResources.ResourceManager).System.Globalization.CultureInfo(used byStringResources.Cultureproperty, though not directly accessed here).
- Consumers:
- XAML files in the RealtimeModule (e.g.,
*.xamlviews) that use{Translate ...}syntax. - No other C# code depends on this class — it is exclusively for XAML usage.
- XAML files in the RealtimeModule (e.g.,
5. Gotchas
- Fallback format is literal and unlocalized:
- The fallback string
"#stringnotfound# "is hardcoded and includes a trailing space. This may cause visual artifacts (e.g., extra whitespace) in UI if keys are missing.
- The fallback string
- No validation of key validity:
- Passing an invalid key (e.g.,
"NonExistentKey") does not throw; it silently falls back to"#stringnotfound# NonExistentKey". Developers may not immediately notice missing keys in development.
- Passing an invalid key (e.g.,
- Case sensitivity:
- Key lookup is case-sensitive (via
ResourceManager.GetString)."AvailableChannels"works;"availablechannels"does not.
- Key lookup is case-sensitive (via
- Auto-generated resource class:
StringResourcesis auto-generated from.resxfiles. Changes require regenerating the class (via.resxedits and rebuild). The extension will returnnull-equivalent fallback if keys are renamed/removed in the.resx.
- No culture override support:
- The extension does not expose or use
StringResources.Culture. It relies on the current UI thread’s culture (standard WPF behavior).
- The extension does not expose or use
- No caching of resolved values:
- Each
ProvideValuecall performs a freshResourceManager.GetStringlookup. WhileResourceManagercaches internally, repeated use in dynamic UI (e.g., data templates) may have minor overhead.
- Each
None identified beyond the above.