|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |