5.5 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||
|---|---|---|---|---|---|---|
|
2026-04-16T04:55:02.133936+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | b181762eeb96b72d |
Resources
Documentation: TranslateExtension Module
1. Purpose
This module provides a WPF MarkupExtension (TranslateExtension) to enable declarative, localized string resolution in XAML. It allows UI elements (e.g., TextBlock.Text, Header, etc.) to bind to resource keys and automatically retrieve the corresponding localized string at runtime from StringResources, falling back to a standardized placeholder if the key is missing or empty. Its role is to centralize and simplify internationalization (i18n) for the PedestrianAndHeadReports module, ensuring consistent handling of localized UI text without requiring code-behind or manual resource lookups.
2. Public Interface
TranslateExtension class
Namespace: PedestrianAndHeadReports.Resources
Inherits: System.Windows.Markup.MarkupExtension
Constructor
public TranslateExtension(string key)
- Purpose: Initializes a new instance with the specified resource key.
- Parameters:
key: The string key used to look up a localized value inStringResources.ResourceManager.
ProvideValue method
public override object ProvideValue(IServiceProvider serviceProvider)
- Purpose: Returns the localized string corresponding to
_key, or a fallback value if lookup fails. - 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→ returns"#stringnotfound# " + _key(i.e., the placeholder followed by a space and the original key).
- If
- Return type:
object(always astringper[MarkupExtensionReturnType(typeof(string))]).
3. Invariants
- Key must be non-null/non-empty for meaningful lookup:
Empty ornullkeys are explicitly handled and return"#stringnotfound#"—no exception is thrown. - Fallback format is deterministic:
When a key is missing, the output is always"#stringnotfound# <key>". This makes missing keys visually identifiable in the UI. - Thread-safety of resource access is assumed:
StringResources.ResourceManager.GetString(...)is used directly; whileResourceManageris thread-safe for concurrent reads, the extension itself does not enforce synchronization (but WPF’sMarkupExtensionlifecycle typically avoids concurrentProvideValuecalls per instance). - Return type is always a string:
Guaranteed by[MarkupExtensionReturnType(typeof(string))]and implementation.
4. Dependencies
Dependencies of this module:
System.Windows.Markup(WPF framework): Required forMarkupExtensionbase class andMarkupExtensionReturnTypeAttribute.PedestrianAndHeadReports.Resources.StringResources:- Used via
StringResources.ResourceManager.GetString(...). StringResourcesis auto-generated from.resxfiles and contains strongly-typed properties (e.g.,Acceleration,TestDate) backed by theResourceManager.
- Used via
Dependencies on this module:
- XAML files in the
PedestrianAndHeadReportsmodule (inferred):- Likely used in XAML as
{local:Translate KeyName}to localize UI elements. - Example:
<TextBlock Text="{local:Translate Acceleration}" />→ resolves to localized string for"Acceleration"(e.g.,"加速度"in Japanese).
- Likely used in XAML as
External dependencies:
- .NET Framework 4.0+ (inferred from runtime version in
StringResources.Designer.csheader).
5. Gotchas
- No caching of resolved values per instance:
Each call toProvideValueperforms a freshResourceManager.GetString(_key)lookup. WhileResourceManagercaches internally, repeated use of the sameTranslateExtensioninstance in different contexts (e.g., data templates) may re-resolve unnecessarily. - Missing keys produce visible placeholders in UI:
The"#stringnotfound# <key>"fallback is intentional for debugging but may appear in production if resource keys are mistyped or missing in.resxfiles. - Case sensitivity of keys:
Resource keys (e.g.,"Acceleration","Acceleration ") are case-sensitive. A mismatch in casing (e.g.,"acceleration") will result in anulllookup and fallback. - No support for parameterized strings (e.g.,
string.Format):
The extension only supports simple key → string lookup. It does not handle placeholders (e.g.,"Hello {0}") or arguments. - Auto-generated
StringResourcesclass:
Changes to the underlying.resxfile require regeneration ofStringResources.Designer.cs. If keys are renamed/removed in.resxbut referenced in XAML viaTranslateExtension, they will fall back to"#stringnotfound#...". - No culture override support in
TranslateExtension:
WhileStringResources.Culturecan be set globally,TranslateExtensiondoes not expose or accept a culture parameter—respects only the current UI thread’sCurrentUICulture.
None identified from source alone.
(Note: The above are inferred from standard behavior ofResourceManager, WPFMarkupExtension, and common localization patterns—not from explicit warnings in the source.)