Skip to content

Instantly share code, notes, and snippets.

@OwainWilliams
Last active April 21, 2023 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OwainWilliams/f5f510ddbb42ff6c5ed697f9e634520e to your computer and use it in GitHub Desktop.
Save OwainWilliams/f5f510ddbb42ff6c5ed697f9e634520e to your computer and use it in GitHub Desktop.
Creating an alphabet list which has multilingual support in .Net Framework 4.7.2

I wrote a blog about creating a list of letters that changes depending on the language / culture the website is in.

I then had a follow up reply from Anders with a Gist that he had created and this code works in .Net Core but not in .Net Framework and since this was going to be used in an Umbraco 8 site, I needed to adjust things a bit.

Here is the code that worked for me in Umbraco 8 and .Net Framework

@using Umbraco.Core.Composing
@using Site.Core.Localisations;
@{
var localisation = Current.Factory.GetInstance<LocalizationService>();
}
<html>
<!-- this is your razor view -->
<ul class="compliance-results-index">
@foreach (var letter in localisation.GetAlphabet())
{
<li class="compliance-results-item __active"><a href="/" title="@letter">@letter</a></li>
}
</ul>
</html>
using Umbraco.Core;
using Umbraco.Core.Composing;
using Site.Core.Localisations;
namespace Site.Core.Composing
{
public class LocalisationComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<LocalizationService>(Lifetime.Singleton);
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Site.Core.Localisations
{
/// <summary>
/// Class to handle various tasks related to localization.
/// </summary>
public class LocalizationService
{
public static readonly IReadOnlyList<string> EnglishAlphabet = new[] {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "X", "Y", "Z"
};
public static readonly IReadOnlyList<string> WelshAlphabet = new[] {
"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"
};
/// <summary>
/// Returns an array representing each character in the alphabet of the current culture.
/// </summary>
/// <returns>A string array representing the alphabet.</returns>
public virtual IReadOnlyList<string> GetAlphabet()
{
return GetAlphabet(CultureInfo.CurrentCulture);
}
/// <summary>
/// Returns an array representing each character in the alphabet of the specified <paramref name="culture"/>.
/// </summary>
/// <param name="culture">The culture to be used.</param>
/// <returns>A string array representing the alphabet.</returns>
public virtual IReadOnlyList<string> GetAlphabet(CultureInfo culture)
{
switch(culture.TwoLetterISOLanguageName)
{
case "cy":
return WelshAlphabet;
default:
return EnglishAlphabet;
}
}
/// <summary>
/// Returns the first character of the specified value, according to the current culture.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A string represeting the first character.</returns>
public virtual string GetFirstLetter(string value)
{
return GetFirstLetter(value, CultureInfo.CurrentCulture);
}
/// <summary>
/// Returns the first character of the specified value, according to the the specified <paramref name="culture"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="culture">The culture to be used.</param>
/// <returns>A string represeting the first character.</returns>
public virtual string GetFirstLetter(string value, CultureInfo culture)
{
if (value.Length == 0) throw new Exception("Computer says no!");
value = value.ToLowerInvariant();
if (value.Length == 1) return value.Substring(0,1);
if (culture.TwoLetterISOLanguageName == "cy")
{
if (value[0] == 'c' && value[1] == 'h') return "ch";
if (value[0] == 'd' && value[1] == 'd') return "dd";
if (value[0] == 'f' && value[1] == 'f') return "ff";
if (value[0] == 'n' && value[1] == 'g') return "ng";
if (value[0] == 'l' && value[1] == 'l') return "ll";
if (value[0] == 'p' && value[1] == 'h') return "ph";
if (value[0] == 'r' && value[1] == 'h') return "rh";
if (value[0] == 't' && value[1] == 'h') return "th";
}
return value.Substring(0,1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment