-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
68 changed files
with
1,236 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.ComponentModel; | ||
using Config.Net; | ||
|
||
namespace EvoSC.Common.Config.Models; | ||
|
||
public interface ILocaleConfig | ||
{ | ||
[Description("The default display language of the controller. Must be a \"language tag\" as found here: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c")] | ||
[Option(Alias = "defaultLanguage", DefaultValue = "en")] | ||
public string DefaultLanguage { get; } | ||
} |
21 changes: 21 additions & 0 deletions
21
src/EvoSC.Common/Database/Migrations/202306201107_AddPlayerSettingsTable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using FluentMigrator; | ||
|
||
namespace EvoSC.Common.Database.Migrations; | ||
|
||
[Migration(1687252035)] | ||
public class AddPlayerSettingsTable : Migration | ||
{ | ||
public const string PlayerSettings = "PlayerSettings"; | ||
|
||
public override void Up() | ||
{ | ||
Create.Table(PlayerSettings) | ||
.WithColumn("PlayerId").AsInt64().Unique() | ||
.WithColumn("DisplayLanguage").AsString().WithDefaultValue("en"); | ||
} | ||
|
||
public override void Down() | ||
{ | ||
Delete.Table(PlayerSettings); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/EvoSC.Common/Database/Models/Player/DbPlayerSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using EvoSC.Common.Interfaces.Models; | ||
using LinqToDB.Mapping; | ||
|
||
namespace EvoSC.Common.Database.Models.Player; | ||
|
||
[Table("PlayerSettings")] | ||
public class DbPlayerSettings : IPlayerSettings | ||
{ | ||
[Column] | ||
public long PlayerId { get; set; } | ||
|
||
[Column] | ||
public string DisplayLanguage { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/EvoSC.Common/Interfaces/Localization/ILocalizationManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Globalization; | ||
using System.Resources; | ||
|
||
namespace EvoSC.Common.Interfaces.Localization; | ||
|
||
public interface ILocalizationManager | ||
{ | ||
/// <summary> | ||
/// The resource manager for the resource of the locales. | ||
/// </summary> | ||
public ResourceManager Manager { get; } | ||
|
||
/// <summary> | ||
/// Get the string of a locale key using the provided culture. | ||
/// </summary> | ||
/// <param name="culture">The culture/language to use.</param> | ||
/// <param name="name">Name of the locale.</param> | ||
/// <param name="args">Arguments passed to string.Format</param> | ||
/// <returns></returns> | ||
public string GetString(CultureInfo culture, string name, params object[] args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Dynamic; | ||
using System.Resources; | ||
|
||
namespace EvoSC.Common.Interfaces.Localization; | ||
|
||
public abstract class Locale : DynamicObject | ||
{ | ||
/// <summary> | ||
/// Get the string of a locale key. | ||
/// </summary> | ||
/// <param name="name">Name of the locale</param> | ||
/// <param name="args">Any formatting arguments to pass to string.Format</param> | ||
public abstract string this[string name, params object[] args] { get; } | ||
|
||
/// <summary> | ||
/// Use the player's selected language when returning locale strings. | ||
/// </summary> | ||
public abstract Locale PlayerLanguage { get; } | ||
|
||
/// <summary> | ||
/// Get the resource set for the current resource. | ||
/// </summary> | ||
/// <returns></returns> | ||
public abstract ResourceSet? GetResourceSet(); | ||
|
||
/// <summary> | ||
/// Translate a string pattern containing locale names. | ||
/// </summary> | ||
/// <param name="pattern">The string to translate. Any locale name in the format | ||
/// [LocaleName] will be replaced.</param> | ||
/// <param name="args">Any formatting arguments to pass to string.Format</param> | ||
/// <returns></returns> | ||
public abstract string Translate(string pattern, params object[] args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,4 +37,5 @@ public interface IPlayer | |
/// </summary> | ||
public string? Zone { get; } | ||
|
||
public IPlayerSettings Settings { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace EvoSC.Common.Interfaces.Models; | ||
|
||
public interface IPlayerSettings | ||
{ | ||
public string DisplayLanguage { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System.Dynamic; | ||
using System.Globalization; | ||
using System.Resources; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using EvoSC.Common.Config.Models; | ||
using EvoSC.Common.Controllers.Context; | ||
using EvoSC.Common.Interfaces.Controllers; | ||
using EvoSC.Common.Interfaces.Localization; | ||
|
||
namespace EvoSC.Common.Localization; | ||
|
||
public class LocaleResource : Locale | ||
{ | ||
private readonly ILocalizationManager _localeManager; | ||
private readonly IContextService _context; | ||
private readonly IEvoScBaseConfig _config; | ||
|
||
private bool _useDefaultCulture = true; | ||
|
||
private static readonly Regex TranslationTag = | ||
new(@"\[([\w\d_]+)\]", RegexOptions.Compiled, TimeSpan.FromMilliseconds(50)); | ||
|
||
public override string this[string name, params object[] args] => GetString(name, args); | ||
|
||
public override Locale PlayerLanguage => UsePlayerLanguage(); | ||
|
||
public LocaleResource(ILocalizationManager localeManager, IContextService context, IEvoScBaseConfig config) | ||
{ | ||
_localeManager = localeManager; | ||
_context = context; | ||
_config = config; | ||
} | ||
|
||
public override ResourceSet? GetResourceSet() => | ||
_localeManager.Manager.GetResourceSet(GetCulture(), true, true); | ||
|
||
public override string Translate(string pattern, params object[] args) | ||
{ | ||
var matches = TranslationTag.Matches(pattern); | ||
var sb = new StringBuilder(); | ||
|
||
var currIndex = 0; | ||
foreach (Match match in matches) | ||
{ | ||
sb.Append(pattern.Substring(currIndex, match.Index - currIndex)); | ||
var translation = GetString(match.Groups[1].Value, args); | ||
currIndex = match.Index + match.Value.Length; | ||
|
||
sb.Append(translation); | ||
} | ||
|
||
if (currIndex + 1 < pattern.Length) | ||
{ | ||
sb.Append(pattern.Substring(currIndex)); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
private CultureInfo GetCulture() | ||
{ | ||
var context = _context.GetContext() as PlayerInteractionContext; | ||
|
||
if (_useDefaultCulture || context?.Player?.Settings == null) | ||
{ | ||
return CultureInfo.GetCultureInfo(_config.Locale.DefaultLanguage); | ||
} | ||
|
||
return CultureInfo.GetCultureInfo(context.Player.Settings.DisplayLanguage); | ||
} | ||
|
||
private Locale UsePlayerLanguage() | ||
{ | ||
_useDefaultCulture = false; | ||
return this; | ||
} | ||
|
||
private string GetString(string name, params object[] args) | ||
{ | ||
var localString = _localeManager.GetString(GetCulture(), name, args); | ||
_useDefaultCulture = true; | ||
return localString; | ||
} | ||
|
||
public override bool TryGetMember(GetMemberBinder binder, out object? result) | ||
{ | ||
var name = binder.Name.Replace("_", ".", StringComparison.Ordinal); | ||
result = this[name]; | ||
return true; | ||
} | ||
|
||
public override bool TryInvokeMember(InvokeMemberBinder binder, object?[]? args, out object? result) | ||
{ | ||
var name = binder.Name.Replace("_", ".", StringComparison.Ordinal); | ||
result = this[name, args!]; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Globalization; | ||
using System.Reflection; | ||
using System.Resources; | ||
using EvoSC.Common.Interfaces.Localization; | ||
|
||
namespace EvoSC.Common.Localization; | ||
|
||
public class LocalizationManager : ILocalizationManager | ||
{ | ||
private readonly ResourceManager _resourceManager; | ||
|
||
public LocalizationManager(Assembly assembly, string resource) | ||
{ | ||
_resourceManager = new ResourceManager(resource, assembly); | ||
|
||
// verify resource | ||
_resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true); | ||
} | ||
|
||
public ResourceManager Manager => _resourceManager; | ||
|
||
public string GetString(CultureInfo culture, string name, params object[] args) | ||
{ | ||
var localeString = _resourceManager.GetString(name, culture); | ||
|
||
if (localeString == null) | ||
{ | ||
throw new KeyNotFoundException($"Failed to find locale name {name}."); | ||
} | ||
|
||
return string.Format(localeString, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.