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

Add localizations #152

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a97117b
Implement basic command localization
Sella-GH Aug 7, 2024
1d44019
Expand to parameters
Sella-GH Aug 7, 2024
b48fcf6
Fix some locale issues
Sella-GH Aug 8, 2024
53750df
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Aug 8, 2024
e3e1981
Parameter localization
Sella-GH Aug 8, 2024
d8f4d38
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Aug 9, 2024
992e684
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Aug 16, 2024
6445fee
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Aug 21, 2024
65c0a89
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Sep 30, 2024
42f3f45
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Oct 3, 2024
6ce5c06
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Oct 13, 2024
cf96852
Fix builds
Sella-GH Oct 13, 2024
72d8a37
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Oct 13, 2024
6c10a50
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Oct 15, 2024
6e6b1b4
Add localization for ChoiceProviders
Sella-GH Oct 15, 2024
1f94a09
Start to localize Core Commands
Sella-GH Oct 15, 2024
20dd6be
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Oct 18, 2024
d026ddd
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Oct 27, 2024
9825aea
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Nov 9, 2024
9bb1b3f
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Nov 12, 2024
ec64199
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Dec 28, 2024
8466c89
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Dec 29, 2024
542c940
Merge branch 'main' into dev/LocalizationSystem
Sella-GH Jan 2, 2025
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
45 changes: 45 additions & 0 deletions src/AzzyBot.Bot/AzzyBot.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@
</ItemGroup>

<ItemGroup>
<Compile Update="Localization\CommandChoices.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>CommandChoices.resx</DependentUpon>
</Compile>
<Compile Update="Localization\CommandDescriptions.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>CommandDescriptions.resx</DependentUpon>
</Compile>
<Compile Update="Localization\CommandNames.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>CommandNames.resx</DependentUpon>
</Compile>
<Compile Update="Localization\CommandParamDescriptions.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>CommandParamDescriptions.resx</DependentUpon>
</Compile>
<Compile Update="Localization\CommandParamNames.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>CommandParamNames.resx</DependentUpon>
</Compile>
<Compile Update="Resources\UriStrings.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand All @@ -81,6 +106,26 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Localization\CommandChoices.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>CommandChoices.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Localization\CommandDescriptions.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>CommandDescriptions.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Localization\CommandNames.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>CommandNames.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Localization\CommandParamDescriptions.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>CommandParamDescriptions.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Localization\CommandParamNames.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>CommandParamNames.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Resources\UriStrings.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>UriStrings.Designer.cs</LastGenOutput>
Expand Down
32 changes: 17 additions & 15 deletions src/AzzyBot.Bot/Commands/AdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using AzzyBot.Bot.Commands.Autocompletes;
using AzzyBot.Bot.Commands.Choices;
using AzzyBot.Bot.Localization;
using AzzyBot.Bot.Services;
using AzzyBot.Bot.Settings;
using AzzyBot.Bot.Utilities;
Expand All @@ -22,6 +23,7 @@
using DSharpPlus.Commands.ContextChecks;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Processors.SlashCommands.Localization;
using DSharpPlus.Entities;
using Microsoft.Extensions.Logging;

Expand All @@ -30,23 +32,23 @@ namespace AzzyBot.Bot.Commands;
[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class AdminCommands
{
[Command("admin"), RequireGuild, RequireApplicationOwner, RequirePermissions(DiscordPermissions.None, DiscordPermissions.Administrator)]
[Command("admin"), RequireGuild, RequireApplicationOwner, RequirePermissions(DiscordPermissions.None, DiscordPermissions.Administrator), InteractionLocalizer<CommandLocalizer>]
public sealed class AdminGroup(ILogger<AdminGroup> logger, AzzyBotSettingsRecord settings, DbActions dbActions, DiscordBotService botService)
{
private readonly ILogger<AdminGroup> _logger = logger;
private readonly AzzyBotSettingsRecord _settings = settings;
private readonly DbActions _dbActions = dbActions;
private readonly DiscordBotService _botService = botService;

[Command("change-bot-status"), Description("Change the global bot status according to your likes.")]
[Command("change-bot-status"), Description("Change the global bot status according to your likes."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask ChangeStatusAsync
(
SlashCommandContext context,
[Description("Choose the activity type which the bot should have."), SlashChoiceProvider<BotActivityProvider>] int activity,
[Description("Choose the status type which the bot should have."), SlashChoiceProvider<BotStatusProvider>] int status,
[Description("Enter a custom doing which is added after the activity type."), MinMaxLength(0, 128)] string doing,
[Description("Enter a custom url. Only usable when having activity type streaming or watching!")] Uri? url = null,
[Description("Reset the bot status to default."), SlashChoiceProvider<BooleanYesNoStateProvider>] int reset = 0
[Description("Choose the activity type which the bot should have."), SlashChoiceProvider<BotActivityProvider>, InteractionLocalizer<CommandLocalizer>] int activity,
[Description("Choose the status type which the bot should have."), SlashChoiceProvider<BotStatusProvider>, InteractionLocalizer<CommandLocalizer>] int status,
[Description("Enter a custom doing which is added after the activity type."), MinMaxLength(0, 128), InteractionLocalizer<CommandLocalizer>] string doing,
[Description("Enter a custom url. Only usable when having activity type streaming or watching!"), InteractionLocalizer<CommandLocalizer>] Uri? url = null,
[Description("Reset the bot status to default."), SlashChoiceProvider<BooleanYesNoStateProvider>, InteractionLocalizer<CommandLocalizer>] int reset = 0
)
{
ArgumentNullException.ThrowIfNull(context);
Expand All @@ -67,11 +69,11 @@ public async ValueTask ChangeStatusAsync
}
}

[Command("get-joined-server"), Description("Displays all servers the bot is in.")]
[Command("get-joined-server"), Description("Displays all servers the bot is in."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask GetJoinedGuildsAsync
(
SlashCommandContext context,
[Description("Select the server you want to get more information about."), SlashAutoCompleteProvider<GuildsAutocomplete>] string? serverId = null
[Description("Select the server you want to get more information about."), SlashAutoCompleteProvider<GuildsAutocomplete>, InteractionLocalizer<CommandLocalizer>] string? serverId = null
)
{
ArgumentNullException.ThrowIfNull(context);
Expand Down Expand Up @@ -125,11 +127,11 @@ public async ValueTask GetJoinedGuildsAsync
await context.EditResponseAsync(stringBuilder.ToString());
}

[Command("remove-joined-server"), Description("Removes the bot from a server.")]
[Command("remove-joined-server"), Description("Removes the bot from a server."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask RemoveJoinedGuildAsync
(
SlashCommandContext context,
[Description("Select the server you want to remove."), SlashAutoCompleteProvider<GuildsAutocomplete>] string serverId
[Description("Select the server you want to remove."), SlashAutoCompleteProvider<GuildsAutocomplete>, InteractionLocalizer<CommandLocalizer>] string serverId
)
{
ArgumentNullException.ThrowIfNull(context);
Expand Down Expand Up @@ -163,11 +165,11 @@ public async ValueTask RemoveJoinedGuildAsync
await context.EditResponseAsync($"I left **{guild.Name}** ({guild.Id}).");
}

[Command("send-bot-wide-message"), Description("Sends a message to all servers the bot is in.")]
[Command("send-bot-wide-message"), Description("Sends a message to all servers the bot is in."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask SendBotWideMessageAsync
(
SlashCommandContext context,
[Description("The message you want to send."), MinMaxLength(1, 2000)] string message
[Description("The message you want to send."), MinMaxLength(1, 2000), InteractionLocalizer<CommandLocalizer>] string message
)
{
ArgumentNullException.ThrowIfNull(context);
Expand Down Expand Up @@ -207,11 +209,11 @@ public async ValueTask SendBotWideMessageAsync
await context.EditResponseAsync(GeneralStrings.MessageSentToAll);
}

[Command("view-logs"), Description("View the logs of the bot.")]
[Command("view-logs"), Description("View the logs of the bot."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask ViewLogsAsync
(
SlashCommandContext context,
[Description("The log file you want to read."), SlashAutoCompleteProvider<AzzyViewLogsAutocomplete>] string? logfile = null
[Description("The log file you want to read."), SlashAutoCompleteProvider<AzzyViewLogsAutocomplete>, InteractionLocalizer<CommandLocalizer>] string? logfile = null
)
{
ArgumentNullException.ThrowIfNull(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AzzyBot.Bot.Localization;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Trees;
using DSharpPlus.Entities;
Expand All @@ -10,8 +11,8 @@ public sealed class AzuraExportPlaylistProvider : IChoiceProvider
{
private static readonly IEnumerable<DiscordApplicationCommandOptionChoice> ExportProviders =
[
new("M3U", "m3u"),
new("PLS", "pls")
new("M3U", "m3u", CommandChoiceLocalizer.GenerateTranslations(nameof(AzuraExportPlaylistProvider), "m3u")),
new("PLS", "pls", CommandChoiceLocalizer.GenerateTranslations(nameof(AzuraExportPlaylistProvider), "pls")),
];

public ValueTask<IEnumerable<DiscordApplicationCommandOptionChoice>> ProvideAsync(CommandParameter parameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AzzyBot.Bot.Localization;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Trees;
using DSharpPlus.Entities;
Expand All @@ -10,8 +11,8 @@ public sealed class BooleanEnableDisableStateProvider : IChoiceProvider
{
private static readonly IEnumerable<DiscordApplicationCommandOptionChoice> BooleanStates =
[
new("Enable", 1),
new("Disable", 2)
new("Enable", 1, CommandChoiceLocalizer.GenerateTranslations(nameof(BooleanEnableDisableStateProvider), "Enable")),
new("Disable", 2, CommandChoiceLocalizer.GenerateTranslations(nameof(BooleanEnableDisableStateProvider), "Disable")),
];

public ValueTask<IEnumerable<DiscordApplicationCommandOptionChoice>> ProvideAsync(CommandParameter parameter)
Expand Down
5 changes: 3 additions & 2 deletions src/AzzyBot.Bot/Commands/Choices/BooleanYesNoStateProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AzzyBot.Bot.Localization;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Trees;
using DSharpPlus.Entities;
Expand All @@ -10,8 +11,8 @@ public sealed class BooleanYesNoStateProvider : IChoiceProvider
{
private static readonly IEnumerable<DiscordApplicationCommandOptionChoice> BooleanStates =
[
new("Yes", 1),
new("No", 2)
new("Yes", 1, CommandChoiceLocalizer.GenerateTranslations(nameof(BooleanYesNoStateProvider), "Yes")),
new("No", 2, CommandChoiceLocalizer.GenerateTranslations(nameof(BooleanYesNoStateProvider), "No")),
];

public ValueTask<IEnumerable<DiscordApplicationCommandOptionChoice>> ProvideAsync(CommandParameter parameter)
Expand Down
11 changes: 6 additions & 5 deletions src/AzzyBot.Bot/Commands/Choices/BotActivityProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AzzyBot.Bot.Localization;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Trees;
using DSharpPlus.Entities;
Expand All @@ -10,11 +11,11 @@ public sealed class BotActivityProvider : IChoiceProvider
{
private static readonly IEnumerable<DiscordApplicationCommandOptionChoice> BotActivity =
[
new("Playing", 0),
new("Streaming", 1),
new("Listening To", 2),
new("Watching", 3),
new("Competing", 5)
new("Playing", 0, CommandChoiceLocalizer.GenerateTranslations(nameof(BotActivityProvider), "Playing")),
new("Streaming", 1, CommandChoiceLocalizer.GenerateTranslations(nameof(BotActivityProvider), "Streaming")),
new("Listening To", 2, CommandChoiceLocalizer.GenerateTranslations(nameof(BotActivityProvider), "Listening")),
new("Watching", 3, CommandChoiceLocalizer.GenerateTranslations(nameof(BotActivityProvider), "Watching")),
new("Competing", 5, CommandChoiceLocalizer.GenerateTranslations(nameof(BotActivityProvider), "Competing"))
];

public ValueTask<IEnumerable<DiscordApplicationCommandOptionChoice>> ProvideAsync(CommandParameter parameter)
Expand Down
9 changes: 5 additions & 4 deletions src/AzzyBot.Bot/Commands/Choices/BotStatusProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AzzyBot.Bot.Localization;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Trees;
using DSharpPlus.Entities;
Expand All @@ -10,10 +11,10 @@ public sealed class BotStatusProvider : IChoiceProvider
{
private static readonly IEnumerable<DiscordApplicationCommandOptionChoice> BotStatusChoices =
[
new("Offline", 0),
new("Online", 1),
new("Idle", 2),
new("Do Not Disturb", 4)
new("Offline", 0, CommandChoiceLocalizer.GenerateTranslations(nameof(BotStatusProvider), "Offline")),
new("Online", 1, CommandChoiceLocalizer.GenerateTranslations(nameof(BotStatusProvider), "Online")),
new("Idle", 2, CommandChoiceLocalizer.GenerateTranslations(nameof(BotStatusProvider), "Idle")),
new("Do Not Disturb", 4, CommandChoiceLocalizer.GenerateTranslations(nameof(BotStatusProvider), "Dnd")),
];

public ValueTask<IEnumerable<DiscordApplicationCommandOptionChoice>> ProvideAsync(CommandParameter parameter)
Expand Down
16 changes: 9 additions & 7 deletions src/AzzyBot.Bot/Commands/CoreCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading.Tasks;
using AzzyBot.Bot.Commands.Autocompletes;
using AzzyBot.Bot.Localization;
using AzzyBot.Bot.Services;
using AzzyBot.Bot.Settings;
using AzzyBot.Bot.Utilities;
Expand All @@ -19,6 +20,7 @@
using DSharpPlus.Commands.ContextChecks;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Processors.SlashCommands.Localization;
using DSharpPlus.Entities;
using Microsoft.Extensions.Logging;

Expand All @@ -27,15 +29,15 @@ namespace AzzyBot.Bot.Commands;
[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class CoreCommands
{
[Command("core"), RequireGuild]
[Command("core"), RequireGuild, InteractionLocalizer<CommandLocalizer>]
public sealed class CoreGroup(ILogger<CoreGroup> logger, AzzyBotSettingsRecord settings, DbActions dbActions, DiscordBotService botService)
{
private readonly ILogger<CoreGroup> _logger = logger;
private readonly AzzyBotSettingsRecord _settings = settings;
private readonly DbActions _dbActions = dbActions;
private readonly DiscordBotService _botService = botService;

[Command("force-channel-permissions-check"), Description("Forces a check of the permissions for the bot in the necessary channel."), RequirePermissions(DiscordPermissions.None, DiscordPermissions.ManageChannels)]
[Command("force-channel-permissions-check"), Description("Forces a check of the permissions for the bot in the necessary channels."), RequirePermissions(DiscordPermissions.None, DiscordPermissions.ManageChannels), InteractionLocalizer<CommandLocalizer>]
public async ValueTask ForceChannelPermissionsCheckAsync(SlashCommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
Expand All @@ -56,7 +58,7 @@ public async ValueTask ForceChannelPermissionsCheckAsync(SlashCommandContext con
await _botService.CheckPermissionsAsync(guild);
}

[Command("help"), Description("Gives you an overview about all the available commands.")]
[Command("help"), Description("Gives you an overview about all the available commands."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask HelpAsync
(
SlashCommandContext context,
Expand Down Expand Up @@ -103,13 +105,13 @@ public async ValueTask HelpAsync
await context.EditResponseAsync(messageBuilder);
}

[Command("stats")]
[Command("stats"), InteractionLocalizer<CommandLocalizer>]
public sealed class CoreStats(AppStatsRecord stats, ILogger<CoreStats> logger)
{
private readonly AppStatsRecord _stats = stats;
private readonly ILogger<CoreStats> _logger = logger;

[Command("hardware"), Description("Shows information about the hardware side of the bot.")]
[Command("hardware"), Description("Shows information about the hardware side of the bot."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask HardwareStatsAsync(SlashCommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
Expand All @@ -125,7 +127,7 @@ public async ValueTask HardwareStatsAsync(SlashCommandContext context)
await context.EditResponseAsync(embed);
}

[Command("info"), Description("Shows information about the bot and it's components.")]
[Command("info"), Description("Shows information about the bot and it's components."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask InfoStatsAsync(SlashCommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
Expand All @@ -141,7 +143,7 @@ public async ValueTask InfoStatsAsync(SlashCommandContext context)
await context.EditResponseAsync(embed);
}

[Command("ping"), Description("Ping the bot and get the latency to discord.")]
[Command("ping"), Description("Ping the bot and get the latency to discord."), InteractionLocalizer<CommandLocalizer>]
public async ValueTask PingAsync(SlashCommandContext context)
{
ArgumentNullException.ThrowIfNull(context);
Expand Down
21 changes: 21 additions & 0 deletions src/AzzyBot.Bot/Localization/CommandChoiceLocalizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Resources;

namespace AzzyBot.Bot.Localization;

public static class CommandChoiceLocalizer
{
public static Dictionary<string, string> GenerateTranslations(string typeName, string value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(typeName);
ArgumentException.ThrowIfNullOrWhiteSpace(value);

Dictionary<string, string> locales = new(1);
ResourceManager resources = new(typeof(CommandChoices));
locales.Add("de", resources.GetString($"{typeName}.{value}", new CultureInfo(1031)) ?? string.Empty);

return locales;
}
}
Loading