Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Core Translations #472

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions configs/addons/counterstrikesharp/lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"menu.button.previous": "Prev",
"menu.button.next": "Next",
"menu.button.close": "Close"
}
8 changes: 7 additions & 1 deletion managed/CounterStrikeSharp.API/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
using CounterStrikeSharp.API.Core.Translations;
using CounterStrikeSharp.API.Modules.Admin;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Serilog;

Expand Down Expand Up @@ -46,6 +48,10 @@ public static int Run()
services.AddScoped<IPluginContextQueryHandler, PluginContextQueryHandler>();
services.AddSingleton<ICommandManager, CommandManager>();

services.TryAddSingleton<IStringLocalizerFactory, CoreJsonStringLocalizerFactory>();
services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));
services.TryAddTransient(typeof(IStringLocalizer), typeof(StringLocalizer));

services.Scan(i => i.FromCallingAssembly()
.AddClasses(c => c.AssignableTo<IStartupService>())
.AsSelfWithInterfaces()
Expand All @@ -71,4 +77,4 @@ public static int Run()
return 0;
}
}
}
}
7 changes: 6 additions & 1 deletion managed/CounterStrikeSharp.API/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using CounterStrikeSharp.API.Modules.Menu;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;

namespace CounterStrikeSharp.API.Core
Expand All @@ -39,6 +40,8 @@ public sealed class Application

public static Application Instance => _instance!;

public static IStringLocalizer Localizer => Instance._localizer;

public static string RootDirectory => Instance._scriptHostConfiguration.RootPath;

private readonly IScriptHostConfiguration _scriptHostConfiguration;
Expand All @@ -48,11 +51,12 @@ public sealed class Application
private readonly IPluginContextQueryHandler _pluginContextQueryHandler;
private readonly IPlayerLanguageManager _playerLanguageManager;
private readonly ICommandManager _commandManager;
private readonly IStringLocalizer _localizer;

public Application(ILoggerFactory loggerFactory, IScriptHostConfiguration scriptHostConfiguration,
GameDataProvider gameDataProvider, CoreConfig coreConfig, IPluginManager pluginManager,
IPluginContextQueryHandler pluginContextQueryHandler, IPlayerLanguageManager playerLanguageManager,
ICommandManager commandManager)
ICommandManager commandManager, IStringLocalizer localizer)
{
Logger = loggerFactory.CreateLogger("Core");
_scriptHostConfiguration = scriptHostConfiguration;
Expand All @@ -62,6 +66,7 @@ public Application(ILoggerFactory loggerFactory, IScriptHostConfiguration script
_pluginContextQueryHandler = pluginContextQueryHandler;
_playerLanguageManager = playerLanguageManager;
_commandManager = commandManager;
_localizer = localizer;
_instance = this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace CounterStrikeSharp.API.Core.Hosting;


/// <summary>
/// Provides information about the CounterStrikeSharp host configuration.
/// </summary>
Expand All @@ -11,28 +10,34 @@ public interface IScriptHostConfiguration
/// e.g. /game/csgo/addons/counterstrikesharp
/// </summary>
string RootPath { get; }

/// <summary>
/// Gets the absolute path to the directory that contains CounterStrikeSharp plugins.
/// e.g. /game/csgo/addons/counterstrikesharp/plugins
/// </summary>
string PluginPath { get; }

/// <summary>
/// Gets the absolute path to the directory that contains CounterStrikeSharp plugin shared APIS.
/// e.g. /game/csgo/addons/counterstrikesharp/shared
/// </summary>
string SharedPath { get; }

/// <summary>
/// Gets the absolute path to the directory that contains CounterStrikeSharp configs.
/// e.g. /game/csgo/addons/counterstrikesharp/configs
/// </summary>
string ConfigsPath { get; }

/// <summary>
/// Gets the absolute path to the directory that contains CounterStrikeSharp game data.
/// e.g. /game/csgo/addons/counterstrikesharp/gamedata
/// </summary>
string GameDataPath { get; }
}

/// <summary>
/// Gets the absolute path to the directory that contains CounterStrikeSharp translation files.
/// e.g. /game/csgo/addons/counterstrikesharp/lang
/// </summary>
string LanguagePath { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal sealed class ScriptHostConfiguration : IScriptHostConfiguration
public string SharedPath { get; }
public string ConfigsPath { get; }
public string GameDataPath { get; }
public string LanguagePath { get; }

public ScriptHostConfiguration(IHostEnvironment hostEnvironment)
{
Expand All @@ -18,5 +19,6 @@ public ScriptHostConfiguration(IHostEnvironment hostEnvironment)
PluginPath = Path.Join(new[] { hostEnvironment.ContentRootPath, "plugins" });
ConfigsPath = Path.Join(new[] { hostEnvironment.ContentRootPath, "configs" });
GameDataPath = Path.Join(new[] { hostEnvironment.ContentRootPath, "gamedata" });
LanguagePath = Path.Join(new[] { hostEnvironment.ContentRootPath, "lang" });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CounterStrikeSharp.API.Core.Hosting;
using Microsoft.Extensions.Localization;

namespace CounterStrikeSharp.API.Core.Translations;

public class CoreJsonStringLocalizerFactory : IStringLocalizerFactory
{
private IScriptHostConfiguration _scriptHostConfiguration;

public CoreJsonStringLocalizerFactory(IScriptHostConfiguration scriptHostConfiguration)
{
_scriptHostConfiguration = scriptHostConfiguration;
}

public IStringLocalizer Create(Type resourceSource)
{
return new JsonStringLocalizer(_scriptHostConfiguration.LanguagePath);
}

public IStringLocalizer Create(string baseName, string location)
{
return new JsonStringLocalizer(_scriptHostConfiguration.LanguagePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class JsonStringLocalizer : IStringLocalizer
{
private readonly JsonResourceManager _resourceManager;
private readonly JsonStringProvider _resourceStringProvider;

public JsonStringLocalizer(string langPath)
{
_resourceManager = new JsonResourceManager(langPath);
_resourceStringProvider = new(_resourceManager);
}

public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
{
return GetAllStrings(includeParentCultures, CultureInfo.CurrentUICulture);
Expand Down Expand Up @@ -52,7 +52,7 @@ public LocalizedString this[string name]
return new LocalizedString(name, value, resourceNotFound: format == null);
}
}

protected string? GetStringSafely(string name, CultureInfo? culture = null)
{
if (name == null)
Expand All @@ -69,16 +69,22 @@ public LocalizedString this[string name]
{
result = _resourceManager.GetFallbackString(name);
}
// Fallback to the default culture (en-US) if the resource is not found for the current culture.

// Fallback to the default culture (whatever is in core.json) if the resource is not found for the current culture.
if (result == null && !culture.Equals(CultureInfo.DefaultThreadCurrentUICulture))
{
result = _resourceManager.GetString(name, CultureInfo.DefaultThreadCurrentUICulture!);
}

// Fallback to the default culture (en) if the resource is not found for the current culture.
if (result == null && !culture.Equals(CultureInfo.InvariantCulture))
{
result = _resourceManager.GetString(name, new CultureInfo("en"));
}

return result?.ReplaceColorTags();
}

protected virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures, CultureInfo culture)
{
if (culture == null)
Expand All @@ -96,7 +102,7 @@ protected virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentC
yield return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
}
}

private IEnumerable<string> GetResourceNamesFromCultureHierarchy(CultureInfo startingCulture)
{
var currentCulture = startingCulture;
Expand All @@ -119,4 +125,4 @@ private IEnumerable<string> GetResourceNamesFromCultureHierarchy(CultureInfo sta

return resourceNames;
}
}
}
15 changes: 8 additions & 7 deletions managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CenterHtmlMenu(string title, BasePlugin plugin) : base(ModifyTitle(title)
{
_plugin = plugin;
}

[Obsolete("Use the constructor that takes a BasePlugin")]
public CenterHtmlMenu(string title) : base(ModifyTitle(title))
{
Expand All @@ -45,8 +45,8 @@ public override void Open(CCSPlayerController player)
{
throw new InvalidOperationException("This method is unsupported with the CenterHtmlMenu constructor used." +
"Please provide a BasePlugin in the constructor.");
};
}

MenuManager.OpenCenterHtmlMenu(_plugin, player, this);
}

Expand Down Expand Up @@ -123,19 +123,20 @@ public override void Display()

if (HasPrevButton)
{
builder.AppendFormat($"<font color='{centerHtmlMenu.PrevPageColor}'>!7</font> &#60;- Prev");
builder.AppendFormat(
$"<font color='{centerHtmlMenu.PrevPageColor}'>!7</font> &#60;- {Application.Localizer["menu.button.previous"]}");
builder.AppendLine("<br>");
}

if (HasNextButton)
{
builder.AppendFormat($"<font color='{centerHtmlMenu.NextPageColor}'>!8</font> -> Next");
builder.AppendFormat($"<font color='{centerHtmlMenu.NextPageColor}'>!8</font> -> {Application.Localizer["menu.button.next"]}");
builder.AppendLine("<br>");
}

if (centerHtmlMenu.ExitButton)
{
builder.AppendFormat($"<font color='{centerHtmlMenu.CloseColor}'>!9</font> -> Close");
builder.AppendFormat($"<font color='{centerHtmlMenu.CloseColor}'>!9</font> -> {Application.Localizer["menu.button.close"]}");
builder.AppendLine("<br>");
}

Expand All @@ -157,4 +158,4 @@ private void RemoveOnTickListener()
var onTick = new Core.Listeners.OnTick(Display);
_plugin.RemoveListener("OnTick", onTick);
}
}
}
21 changes: 11 additions & 10 deletions managed/CounterStrikeSharp.API/Modules/Menu/ChatMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public class ChatMenu : BaseMenu
public char PrevPageColor { get; set; } = ChatColors.Yellow;
public char NextPageColor { get; set; } = ChatColors.Yellow;
public char CloseColor { get; set; } = ChatColors.Red;

public ChatMenu(string title) : base(title)
{
ExitButton = false;
}

public override void Open(CCSPlayerController player)
{
MenuManager.OpenChatMenu(player, this);
Expand All @@ -45,9 +46,9 @@ public ChatMenuInstance(CCSPlayerController player, ChatMenu menu) : base(player

public override void Display()
{
if (Menu is not ChatMenu chatMenu)
{
return;
if (Menu is not ChatMenu chatMenu)
{
return;
}

Player.PrintToChat($" {chatMenu.TitleColor} {chatMenu.Title}");
Expand All @@ -63,21 +64,21 @@ public override void Display()

if (HasPrevButton)
{
Player.PrintToChat($" {chatMenu.PrevPageColor}!7 {ChatColors.Default}-> Prev");
Player.PrintToChat($" {chatMenu.PrevPageColor}!7 {ChatColors.Default}-> {Application.Localizer["menu.button.previous"]}");
}

if (HasNextButton)
{
Player.PrintToChat($" {chatMenu.NextPageColor}!8 {ChatColors.Default}-> Next");
Player.PrintToChat($" {chatMenu.NextPageColor}!8 {ChatColors.Default}-> {Application.Localizer["menu.button.next"]}");
}

if (Menu.ExitButton)
{
Player.PrintToChat($" {chatMenu.CloseColor}!9 {ChatColors.Default}-> Close");
Player.PrintToChat($" {chatMenu.CloseColor}!9 {ChatColors.Default}-> {Application.Localizer["menu.button.close"]}");
}
}
}

public static class ChatMenus
{
[Obsolete("Use MenuManager.OpenChatMenu instead")]
Expand All @@ -91,4 +92,4 @@ public static void OnKeyPress(CCSPlayerController player, int key)
{
MenuManager.OnKeyPress(player, key);
}
}
}
12 changes: 6 additions & 6 deletions managed/CounterStrikeSharp.API/Modules/Menu/ConsoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ public override void Display()

Player.PrintToConsole($"{(option.Disabled ? "[Disabled] - " : "[Enabled]")} css_{keyOffset++} {option.Text}");
}

if (HasPrevButton)
{
Player.PrintToConsole("css_7 -> Prev");
Player.PrintToConsole($"css_7 -> {Application.Localizer["menu.button.previous"]}");
}

if (HasNextButton)
{
Player.PrintToConsole("css_8 -> Next");
Player.PrintToConsole($"css_8 -> {Application.Localizer["menu.button.next"]}");
}

if (Menu.ExitButton)
{
Player.PrintToConsole("css_9 -> Close");
Player.PrintToConsole($"css_9 -> {Application.Localizer["menu.button.close"]}");
}
}
}
}
Loading