Replacing placeholders in Umbraco’s dictionary items

Umbraco’s dictionary feature is really cool as it allows editors to control the text you have on buttons, in form labels and other places on your website, removing the need to involve a developer in order to change simple sentences.

Today we ran into …


This content originally appeared on DEV Community and was authored by Morten Hartvig

Umbraco's dictionary feature is really cool as it allows editors to control the text you have on buttons, in form labels and other places on your website, removing the need to involve a developer in order to change simple sentences.

Today we ran into a scenario where we needed to make some text that was defined in a dictionary item a bit more dynamic. Instead of completely replacing the dictionary item with text directly in the template, we decided to look into replacing values in the dictionary item. This would allow editors to still control the majority of the text displayed.

An easy fix would be to simply perform some kind of replacement on the string returned by the specific dictionary item in whatever template you have. That is, however, not very scalable. Therefore I looked into further improving the UmbracoHelper's GetDictionaryValue method with two extension methods:

public static string GetDictionaryValueWithReplacement(this UmbracoHelper umbraco, string key, string needle, string replacement)
{
    return GetDictionaryValueWithReplacement(umbraco, key, string.Empty, needle, replacement);
}

public static string GetDictionaryValueWithReplacement(this UmbracoHelper umbraco, string key, string altText, string needle, string replacement)
{
    return Regex.Replace(umbraco.GetDictionaryValue(key, altText), needle, replacement);
}

This gives us the possibility to easily reuse the functionality anywhere on the website.

Now, given a dictionary item with the key Some.Dictionary.Item and text This message is super personal for you, %name%. Hope you like it., you can use the extension method above in, for example, a template like so:

@Umbraco.GetDictionaryValueWithReplacement("Some.Dictionary.Item", "%name%", Members.GetCurrentMember().Name)

Which will display the following (in my browser, at least):

This message is super personal for you, Morten Hartvig. Hope you like it.

One could of course make additional methods in order to work with more than one placeholder.. you know, something like:

public static string GetDictionaryValueWithReplacement(this UmbracoHelper umbraco, string key, IDictionary<string, string> replacements)
{
    var value = umbraco.GetDictionaryValue(key);

    foreach (var replacement in replacements)
    {
        value = Regex.Replace(value, replacement.Key, replacement.Value);
    }

    return value;
}

... but that is beyond the scope of what we need so I'll let you play around with that.


This content originally appeared on DEV Community and was authored by Morten Hartvig


Print Share Comment Cite Upload Translate Updates
APA

Morten Hartvig | Sciencx (2021-05-11T07:24:25+00:00) Replacing placeholders in Umbraco’s dictionary items. Retrieved from https://www.scien.cx/2021/05/11/replacing-placeholders-in-umbracos-dictionary-items/

MLA
" » Replacing placeholders in Umbraco’s dictionary items." Morten Hartvig | Sciencx - Tuesday May 11, 2021, https://www.scien.cx/2021/05/11/replacing-placeholders-in-umbracos-dictionary-items/
HARVARD
Morten Hartvig | Sciencx Tuesday May 11, 2021 » Replacing placeholders in Umbraco’s dictionary items., viewed ,<https://www.scien.cx/2021/05/11/replacing-placeholders-in-umbracos-dictionary-items/>
VANCOUVER
Morten Hartvig | Sciencx - » Replacing placeholders in Umbraco’s dictionary items. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/11/replacing-placeholders-in-umbracos-dictionary-items/
CHICAGO
" » Replacing placeholders in Umbraco’s dictionary items." Morten Hartvig | Sciencx - Accessed . https://www.scien.cx/2021/05/11/replacing-placeholders-in-umbracos-dictionary-items/
IEEE
" » Replacing placeholders in Umbraco’s dictionary items." Morten Hartvig | Sciencx [Online]. Available: https://www.scien.cx/2021/05/11/replacing-placeholders-in-umbracos-dictionary-items/. [Accessed: ]
rf:citation
» Replacing placeholders in Umbraco’s dictionary items | Morten Hartvig | Sciencx | https://www.scien.cx/2021/05/11/replacing-placeholders-in-umbracos-dictionary-items/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.