Replace a token in loaded text¶
After a template has been loaded and tokenized (see Load and tokenize a localized text template), a caller replaces a named placeholder with a supplied value by calling ReplaceToken. The method normalizes the requested token name, looks it up case-insensitively among the tokens parsed from the template, and — if found — applies a case-insensitive regular-expression replacement against the current ReplacedText, so repeated calls compose against previously replaced content.
| Field | Value |
|---|---|
| Operation ID | OP-RES-02 |
| Trigger | Caller invokes the instance method ReplaceToken(inTokenName, inReplacement) on a clsResourceManager (or a clsFileResourceManager / clsAssemblyResourceManager instance after loading), or the shared static overload clsResourceManager.ReplaceToken(text, inTokenName, inReplacement) for one-off replacement against arbitrary text |
| Inputs | Token name and replacement string |
| Outputs | Updated ReplacedText string, also returned directly from the call |
| Primary components | clsResourceManager.ReplaceToken, clsResourceManager.GetToken |
| Entry point | Resources\SAIF.Resources\clsResourceManager.vb |
Steps¶
- Caller supplies a token name and replacement. The caller passes
inTokenNameandinReplacementtoReplaceToken. - Normalize the token name.
GetTokentrims the configuredTokenStartDelimiterandTokenEndDelimitercharacters (default{and}) from both ends ofinTokenName, so callers may pass either the bare name or the fully delimited form (e.g.,userIDor{userID}). - Look up the token.
GetTokensearches theTokenslist parsed during loading for a case-insensitive name match and returns the first match, orNothingif the list is empty/unset or no match exists. - Apply the replacement if found. If a matching token is returned,
ReplaceTokencallsRegex.Replaceagainst the current_ReplacedText, using the token's stored pattern andRegexOptions.IgnoreCase, replacing every occurrence of that pattern withinReplacement. - If no matching token is found:
_ReplacedTextis left unchanged. - Return the current state. The method returns
_ReplacedText, reflecting either the newly applied replacement or the prior unchanged value. - Static one-off variant. The shared
ReplaceToken(text, inTokenName, inReplacement)overload constructs a newclsResourceManagerdirectly fromtext(which re-runsInitialize/LoadTokenson that text) and then performs steps 2–5 against that fresh instance, returning the result without requiring a caller-managed loader instance.
Process flow¶
The following diagram visualizes the token lookup and replacement flow.
flowchart TD
Start([Caller calls ReplaceToken<br>with name and replacement]) --> Trim[Trim delimiter characters<br>from token name]
Trim --> Lookup[Search Tokens list<br>case-insensitively]
Lookup --> Found{Token found?}
Found -->|No| Unchanged([ReplacedText unchanged;<br>return current value])
Found -->|Yes| Replace[Regex.Replace all occurrences<br>of the token pattern]
Replace --> Updated([Return updated ReplacedText])
Failure and edge-case behavior¶
- A token name that does not match any parsed token (case-insensitively) is silently ignored —
ReplaceTokenreturns the unchangedReplacedTextwithout raising an error. - Because the lookup and replacement operate on the token's stored pattern rather than a literal string match, content-bearing tokens (those parsed with a colon, e.g.
{token:content}) are matched and replaced as a whole unit via their token-specific pattern. - Replacement is applied against
_ReplacedText(not the original_Text), so multiple sequentialReplaceTokencalls compose — each call's replacement text becomes part of the input for the next call's regex match. - If
TokensisNothing(i.e., noLoadResource/Initializecall has populated it yet),GetTokenreturnsNothingimmediately and the replacement is a no-op. - The static
ReplaceToken(text, ...)overload always starts from a fresh single-use instance, so it does not benefit from or affect any previously loaded/replaced state elsewhere in the application.
Related documentation¶
Sources¶
Resources\SAIF.Resources\clsResourceManager.vb