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

4.9 KiB
Raw Permalink Blame History

source_files, generated_at, model, schema_version, sha256
source_files generated_at model schema_version sha256
DataPRO/Modules/Groups/GroupImport/Resources/TranslateExtension.cs
DataPRO/Modules/Groups/GroupImport/Resources/StringResources.Designer.cs
2026-04-16T04:45:43.131858+00:00 Qwen/Qwen3-Coder-Next-FP8 1 d8e7eb58224237bd

Resources

Documentation: TranslateExtension Markup Extension


1. Purpose

This module provides a WPF MarkupExtension (TranslateExtension) to enable declarative, localized string resolution in XAML. It allows UI elements to bind to localized resources using a string key, falling back to a visible placeholder if the key is missing or the resource is unavailable. Its role is to support internationalization (i18n) of the Group Import modules UI by abstracting resource lookup away from code-behind and into XAML markup.


2. Public Interface

TranslateExtension class

Namespace: DBImportExport.Resources
Base class: System.Windows.Markup.MarkupExtension
Attribute: [MarkupExtensionReturnType(typeof(string))]

Constructor
public TranslateExtension(string key)
  • Parameters:
    • key: The resource key (e.g., "GroupTags", "Import_Importing") used to look up a localized string in StringResources.
  • Behavior: Stores the key for later use in ProvideValue.
ProvideValue method
public override object ProvideValue(IServiceProvider serviceProvider)
  • Returns: string
  • Behavior:
    • If _key is null or empty → returns "#stringnotfound#".
    • Otherwise, attempts to retrieve the string via StringResources.ResourceManager.GetString(_key).
      • If found → returns the localized string.
      • If not found (null) → returns "#stringnotfound# " + _key (e.g., "#stringnotfound# Import_Importing").
  • Note: The serviceProvider parameter is unused.

3. Invariants

  • _key is immutable after construction (no setter, no mutation).
  • StringResources.ResourceManager.GetString(key) is the only source of localized strings; no fallback logic beyond the NotFound constant is implemented.
  • The NotFound constant ("#stringnotfound#") is used consistently for both missing keys and missing values.
  • If StringResources.ResourceManager fails to initialize (e.g., due to assembly/resource loading issues), GetString may throw — but this is not handled in the extension and would result in a runtime exception.

4. Dependencies

Internal Dependencies

  • GroupImport.Resources.StringResources:
    • Strongly-typed resource class generated from .resx files.
    • Provides access to localized strings via ResourceManager.GetString(key).
    • Contains keys like "GroupTags", "Import_Importing", "Preview_InvalidName", etc.

External Dependencies

  • System.Windows.Markup.MarkupExtension: Base class for WPF markup extensions.
  • System: Used for string.IsNullOrEmpty, object.ReferenceEquals, etc.

Consumers (Inferred)

  • XAML files in the GroupImport module (e.g., *.xaml) that use {local:Translate KeyName} syntax to bind UI text to localized resources.

5. Gotchas

  • No null-safety for ResourceManager.GetString(): If StringResources.ResourceManager is misconfigured (e.g., wrong base name "GroupImport.Resources.StringResources"), GetString may return null or throw — the extension only handles null by appending the key to #stringnotfound#.
  • Hardcoded NotFound string: The placeholder "#stringnotfound#" is visible in the UI if a key is missing, which may confuse end users. No localization-aware fallback (e.g., returning the key itself) is implemented.
  • No caching of resolved values: ProvideValue is called repeatedly (e.g., during layout updates), and each call re-invokes ResourceManager.GetString. While ResourceManager caches internally, repeated calls are still inefficient.
  • Namespace mismatch: The class resides in DBImportExport.Resources but references GroupImport.Resources.StringResources. This may indicate legacy refactoring or intentional decoupling — developers should verify assembly/resource naming consistency.
  • No support for format strings: While StringResources contains format strings (e.g., "Importing {0}:{1}"), TranslateExtension does not accept or apply arguments — it only resolves the raw key. Formatting must be handled elsewhere (e.g., in code-behind or via String.Format after resolution).
  • Auto-generated StringResources.Designer.cs: Changes to .resx files require regeneration of StringResources. If keys are renamed/removed in .resx but not updated in XAML, TranslateExtension will return the #stringnotfound# fallback.

None identified from source alone regarding thread-safety, disposal, or WPF-specific lifecycle quirks — but given the simplicity of the extension, it is likely safe for standard WPF usage.