Files
2026-04-17 14:55:32 -04:00

4.0 KiB
Raw Permalink Blame History

source_files, generated_at, model, schema_version, sha256
source_files generated_at model schema_version sha256
DataPRO/Modules/TestSetups/Diagnostics/Resources/TranslateExtension.cs
DataPRO/Modules/TestSetups/Diagnostics/Resources/StringResources.Designer.cs
2026-04-16T04:50:59.405186+00:00 Qwen/Qwen3-Coder-Next-FP8 1 8602aa2c75632a3b

Resources

Documentation: TranslateExtension Markup Extension


1. Purpose

The TranslateExtension class is a WPF MarkupExtension used to resolve localized string resources at XAML runtime. It enables declarative binding to localized strings in UI markup by accepting a resource key and returning the corresponding localized value from the StringResources strongly-typed resource class. This allows UI text to be localized without hardcoding strings in XAML, supporting multi-language deployments of the diagnostics module.


2. Public Interface

TranslateExtension(string key)

  • Signature: public TranslateExtension(string key)
  • Behavior: Constructor. Stores the provided resource key for later lookup. Does not perform validation beyond null/empty checks during ProvideValue.

ProvideValue(IServiceProvider serviceProvider)

  • Signature: public override object ProvideValue(IServiceProvider serviceProvider)
  • Behavior:
    • If _key is null or empty, returns "#stringnotfound#".
    • Otherwise, attempts to retrieve the string value from StringResources.ResourceManager.GetString(_key).
    • If the resource is not found (i.e., GetString returns null), returns "#stringnotfound# " + _key (note the trailing space).
    • Returns a string (as indicated by [MarkupExtensionReturnType(typeof(string))]).

3. Invariants

  • _key is immutable after construction (stored in a readonly field).
  • The returned value is always a string.
  • Missing or empty keys always yield "#stringnotfound#" (no concatenation with key).
  • Missing resources always yield "#stringnotfound# <key>" (with a space between the prefix and key).
  • No exceptions are thrown during ProvideValue—errors are silently handled via fallback strings.
  • Resource lookup uses the default ResourceManager instance and current UI culture (unless overridden via StringResources.Culture).

4. Dependencies

Dependencies of TranslateExtension:

  • System.Windows.Markup.MarkupExtension (base class, part of WPF).
  • Diagnostics.Resources.StringResources (specifically its ResourceManager property and GetString(string) method).
  • System (for string.IsNullOrEmpty, object.ReferenceEquals, etc.).

Dependencies on TranslateExtension:

  • XAML files in the diagnostics module (e.g., .xaml files) that use {local:Translate KeyName} syntax to bind UI text.
  • StringResources.Designer.cs must be compiled and available at runtime (auto-generated; relies on corresponding .resx file, not visible here).

5. Gotchas

  • No null-safety for _key beyond emptiness: Passing null as _key is allowed in the constructor but results in "#stringnotfound#" at runtime.
  • Ambiguous fallbacks: Both missing keys and empty keys produce "#stringnotfound#", making debugging harder without inspecting the key itself.
  • Hardcoded prefix: The "#stringnotfound#" string is hardcoded and not configurable. If this prefix appears in actual resources, it may cause confusion.
  • No caching of resolved values: Each ProvideValue call re-invokes ResourceManager.GetString, which may have performance implications in high-frequency scenarios (though ResourceManager itself caches internally).
  • Culture dependency: Behavior depends on the current UI threads Culture (via StringResources.Culture). If the UI culture changes dynamically, existing bindings may not update unless re-evaluated.
  • No compile-time key validation: Typos in XAML resource keys (e.g., {local:Translate Channles}) will only surface at runtime as "#stringnotfound# Channles".

None identified beyond the above.