I had a requirement for a website where I needed to have an A to Z list of letters which changed if you visit the website from another language variant e.g. Welsh.
In English the list looks like this :
In Welsh the list looks like this :
I wasn't sure how I could achieve this so I reached out to the fantastic Umbraco Community via Our Forum and Anders came to the rescue with a great response.
I've now used his suggestion on how to create this list and I thought I'd share the code.
This is a file called StringExtensions.cs
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
namespace MySite.Core.Extensions
{
public static class StringExtensions
{
public static readonly string[] EnglishAlphabet = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "X", "Y", "Z" };
public static readonly string[] WelshAlphabet = new string[] { "A", "B", "C", "CH", "D", "DD", "E", "F", "FF", "G", "NG", "H", "I", "J", "L", "LL", "M", "N", "O", "P", "PH", "R", "RH", "S", "T", "TH", "U", "W", "Y" };
public static string[] GetAlphabet(CultureInfo culture)
{
if (culture.TwoLetterISOLanguageName == "cy") return WelshAlphabet;
return EnglishAlphabet;
}
}
}
I can call GetAlphabet() and pass the current site culture which then returns the correct alphabet. It also means that if required, I can add more alphabets to the list quickly.
Then on my razor page I have the following text snippets
@using Oc = MySite.Core.Extensions.StringExtensions;
@{
var alphabetList = Oc.GetAlphabet(System.Threading.Thread.CurrentThread.CurrentCulture);
}
<ul class="alphalist-results-index">
@foreach (var letter in alphabetList)
{
<li class="alphalist-results-item __active"><a href="/" title="@letter">@letter</a></li>
}
</ul>
Update :
Since publishing this blog, Anders has made another update via a Gist. This is well worth a read. 👍
Update 2 : Anders solution works for Umbraco 9+ because it uses .Net Core and features available in c# 8, if you need a solution that works in Umbraco 8 and .Net Framework 4.7.2 then I've created an update via my own Gist - thanks for your continued help with this Anders.