mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 16:38:37 +00:00
Add language mappings for non-standard language names
This commit is contained in:
parent
066f734378
commit
528d3b1335
5 changed files with 54 additions and 6 deletions
|
@ -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()
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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!";
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
@page "/"
|
||||
@using LBPUnion.ProjectLighthouse.Localization.StringLists
|
||||
@using LBPUnion.ProjectLighthouse.Types
|
||||
@using LBPUnion.ProjectLighthouse.Types.Settings
|
||||
@model LBPUnion.ProjectLighthouse.Pages.LandingPage
|
||||
|
@ -7,11 +8,11 @@
|
|||
Layout = "Layouts/BaseLayout";
|
||||
Model.ShowTitleInPage = false;
|
||||
}
|
||||
<h1>Welcome to Project Lighthouse!</h1>
|
||||
<h1>@Model.Translate(LandingPageStrings.Welcome)</h1>
|
||||
|
||||
@if (Model.User != null)
|
||||
{
|
||||
<p>You are currently logged in as <b>@Model.User.Username</b>.</p>
|
||||
<p>@string.Format(Model.Translate(LandingPageStrings.LoggedInAs), $"{Model.User.Username}")</p> // TODO: make this bold
|
||||
if (ServerSettings.Instance.UseExternalAuth && Model.AuthenticationAttemptsCount > 0)
|
||||
{
|
||||
<p>
|
||||
|
@ -22,7 +23,7 @@
|
|||
|
||||
@if (Model.PlayersOnlineCount == 1)
|
||||
{
|
||||
<p>There is 1 user currently online:</p>
|
||||
<p>@Model.Translate(LandingPageStrings.UsersSingle)</p>
|
||||
@foreach (User user in Model.PlayersOnline)
|
||||
{
|
||||
<a href="/user/@user.UserId" title="@user.Status.ToString()">@user.Username</a>
|
||||
|
@ -31,7 +32,7 @@
|
|||
|
||||
else if (Model.PlayersOnlineCount == 0)
|
||||
{
|
||||
<p>There are no users online. Why not hop on?</p>
|
||||
<p>@Model.Translate(LandingPageStrings.UsersNone)</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -94,7 +94,9 @@ public class Startup
|
|||
(
|
||||
config =>
|
||||
{
|
||||
List<CultureInfo> languages = LocalizationManager.GetAvailableLanguages().Select(l => new CultureInfo(l)).ToList();
|
||||
List<CultureInfo> languages = LocalizationManager.GetAvailableLanguages()
|
||||
.Select(l => new CultureInfo(LocalizationManager.MapLanguage(l)))
|
||||
.ToList();
|
||||
|
||||
config.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue