6.0 KiB
6.0 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:42:50.978478+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 2a6f478ba4a248f4 |
Resources
Documentation: TranslateExtension and StringResources
1. Purpose
This module provides localized string support for the RealtimeSettings UI layer in a WPF application. It consists of two tightly coupled components:
TranslateExtension, a XAML markup extension enabling declarative localization in XAML (e.g.,{local:Translate KeyName}), andStringResources, a strongly-typed, auto-generated resource class that wraps access to embedded string resources (.resx-backed).
Together, they allow UI elements to display culture-sensitive text without hardcoding strings, supporting runtime language switching viaStringResources.Culture.
2. Public Interface
TranslateExtension class
- Namespace:
RealtimeSettings.Resources - Base class:
System.Windows.Markup.MarkupExtension - Attributes:
[MarkupExtensionReturnType(typeof(string))]
| Member | Signature | Behavior |
|---|---|---|
| Constructor | public TranslateExtension(string key) |
Stores the resource key (_key) for later lookup. Does not validate key format or existence. |
| ProvideValue | public override object ProvideValue(IServiceProvider serviceProvider) |
Returns the localized string for _key using StringResources.ResourceManager.GetString(_key). If _key is null/empty or lookup fails, returns a fallback string (see Invariants). |
StringResources class
- Namespace:
RealtimeSettings.Resources - Attributes:
[GeneratedCode],[DebuggerNonUserCode],[CompilerGenerated] - Visibility:
internal(not public)
| Member | Signature | Behavior |
|---|---|---|
| ResourceManager | internal static ResourceManager ResourceManager { get; } |
Lazily initializes and returns a ResourceManager for the RealtimeSettings.Resources.StringResources resource file (embedded in the assembly). |
| Culture | internal static CultureInfo Culture { get; set; } |
Gets/sets the UI culture used for resource lookups. Affects all subsequent GetString calls. |
| Static properties | e.g., internal static string DelayBetweenPolls { get; } |
Each property performs a ResourceManager.GetString("KeyName") call. Property names match resource keys (e.g., "DelayBetweenPolls"). |
Notable string keys defined in StringResources:
DelayBetweenPolls,DelayBetweenPollsHelpHighDelay,HighDelayHelpLowDelay,LowDelayHelpRealtimeSampleRate,RealtimeSampleRates,RealtimeSampleRateSliceIP,RealtimeSampleRateSliceUSB,RealtimeSampleRateTDASG5SampleRate,SampleRateHelpUseMeterModeHelp,UseMeterModeTableStyle
3. Invariants
- Key validation:
- If
_keypassed toTranslateExtensionisnullor empty (""),ProvideValuereturns"#stringnotfound#". - If
StringResources.ResourceManager.GetString(_key)returnsnull(key not found),ProvideValuereturns"#stringnotfound# " + _key(e.g.,"#stringnotfound# MissingKey").
- If
- Resource key consistency:
StringResourcesproperties are generated from keys in the.resxfile. The key used inTranslateExtension(key)must exactly match the resource name (e.g.,"DelayBetweenPolls"), including case.
- Thread-safety:
StringResources.ResourceManageris thread-safe (usesLazy<T>-like pattern viaReferenceEqualscheck).StringResources.Cultureis not thread-safe; concurrent writes may cause race conditions.
- Fallback behavior:
- No exception is thrown for missing keys—only fallback strings. This avoids runtime crashes but may result in visible placeholder text.
4. Dependencies
Depends on
System.Windows.Markup(forMarkupExtensionbase class)System.Resources(viaResourceManager)System.Globalization(forCultureInfo)System.CodeDom.Compiler,System.Diagnostics,System.Runtime.CompilerServices(auto-generated attributes)- Embedded resource:
RealtimeSettings.Resources.StringResources.resources(compiled fromStringResources.resx)
Depended on by
- WPF XAML files in the
RealtimeSettingsmodule (e.g.,*.xamlpages) using{local:Translate KeyName}syntax. - UI code-behind may directly access
StringResourcesproperties (e.g.,StringResources.DelayBetweenPolls) for non-XAML localization. - No other modules appear to depend on this (based on namespace isolation:
RealtimeSettings.Resources).
5. Gotchas
- Hardcoded fallback prefix: The
"#stringnotfound#"prefix is hardcoded inTranslateExtension—not configurable or localized itself. - No null-safety for
serviceProvider:ProvideValuedoes not validateserviceProvider(though WPF always provides one during XAML parsing). - Case sensitivity: Resource keys are case-sensitive. A typo like
"delaybetweenpolls"(lowercase) will fail lookup. - Designer file warnings:
StringResources.Designer.csexplicitly warns that manual changes are lost on regeneration. Modifying.resxrequires rebuilding to syncStringResources. - Culture persistence:
StringResources.Cultureonly affects new lookups; existingTranslateExtensioninstances retain the culture at instantiation time (sinceProvideValueis called at XAML parse time). - No support for pluralization/formatting:
TranslateExtensiononly returns raw strings. Placeholders (e.g.,{0}) are possible in.resxbut must be manually handled in XAML (e.g.,String.Formatin code-behind). - No localization for non-string resources: Only strings are supported; no support for images, icons, etc.
None identified beyond these.