Add language mappings for non-standard language names

This commit is contained in:
jvyden 2022-04-16 19:29:31 -04:00
commit 528d3b1335
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
5 changed files with 54 additions and 6 deletions

View file

@ -11,6 +11,10 @@ public static class LocalizationManager
public static string GetLocalizedString(TranslationAreas translationArea, string language, string key)
{
// ASP.NET requires specific names for certain languages (like ja for japanese as opposed to the standard ja-JP)
// We map that value back here.
language = mapLanguageBack(language);
#if DEBUG
Console.WriteLine($"Attempting to load '{key}' for '{language}'");
#endif
@ -36,6 +40,43 @@ public static class LocalizationManager
return localizedString;
}
// If a language isn't working, it might be because a language is using a different name than what ASP.NET expects.
// You can retrieve the name of the language from the <c>Accept-Language</c> header in an HTTP request.
private static readonly Dictionary<string, string> languageMappings = new()
{
{
"ja-JP", "ja"
},
{
"eo-UY", "eo"
},
};
/// <summary>
/// Some languages crowdin uses have names that differ from the ones that ASP.NET expects. This function converts the names.
/// </summary>
/// <param name="language">The language to convert to ASP.NET names</param>
/// <returns>The name of the language that ASP.NET expects.</returns>
public static string MapLanguage(string language)
{
foreach ((string? key, string? value) in languageMappings)
{
if (key == language) return value;
}
return language;
}
private static string mapLanguageBack(string language)
{
foreach ((string? key, string? value) in languageMappings)
{
if (value == language) return key;
}
return language;
}
// This is a bit scuffed, but it will work for what I need it to do.
public static IEnumerable<string> GetAvailableLanguages()
{

View file

@ -11,5 +11,5 @@ public static class LandingPageStrings
public static readonly TranslatableString AuthAttemptsPending = create("authAttemptsPending");
private static TranslatableString create(string key) => new(TranslationAreas.BaseLayout, key);
private static TranslatableString create(string key) => new(TranslationAreas.LandingPage, key);
}

View file

@ -13,8 +13,12 @@ public class TranslatableString
public string Translate(string language) => LocalizationManager.GetLocalizedString(this.Area, language, this.Key);
// CS0809 is a warning about obsolete methods overriding non-obsoleted methods.
// That works against what we're trying to do here, so we disable the warning here.
#pragma warning disable CS0809
[Obsolete("Do not translate by using ToString. Use TranslatableString.Translate().", true)]
public override string ToString() => "NOT TRANSLATED CORRECTLY!";
#pragma warning restore CS0809
[Obsolete("Do not translate by using ToString. Use TranslatableString.Translate().", true)]
public static implicit operator string(TranslatableString _) => "NOT TRANSLATED CORRECTLY!";