Skip to content

Commit be65cf0

Browse files
committed
feat: translator base
1 parent 0f1ed01 commit be65cf0

10 files changed

Lines changed: 801 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<Project Sdk="Microsoft.NET.Sdk">
4+
5+
<Import Project="../Targets/Version.targets" />
6+
<Import Project="../Targets/DisCatSharp.targets" />
7+
<Import Project="../Targets/Package.targets" />
8+
<Import Project="../Targets/NuGet.targets" />
9+
<Import Project="../Targets/Library.targets" />
10+
11+
<PropertyGroup>
12+
<AssemblyName>DisCatSharp.Extensions.Translations.ApplicationCommands</AssemblyName>
13+
<RootNamespace>DisCatSharp.Extensions.Translations.ApplicationCommands</RootNamespace>
14+
</PropertyGroup>
15+
16+
<PropertyGroup>
17+
<PackageId>DisCatSharp.Extensions.Translations.ApplicationCommands</PackageId>
18+
<Description>
19+
DisCatSharp.Extensions.Translations.ApplicationCommands
20+
21+
Extension making it easy to use localizations in ApplicationCommands commands.
22+
</Description>
23+
<PackageTags>DisCatSharp,DisCatSharp Extension,Translations,Localization,ApplicationCommands,Discord,Bots,Discord Bots,AITSYS,Net8,Net9,Net10</PackageTags>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<PackageReference Include="DisCatSharp" Version="[10.7.0-nightly-049,)" />
28+
<PackageReference Include="DisCatSharp.Analyzer" Version="1.0.0">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
31+
</PackageReference>
32+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.1" />
33+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="10.0.1" />
34+
<PackageReference Include="DisCatSharp.ApplicationCommands" Version="[10.7.0-nightly-049,)" />
35+
<PackageReference Include="DisCatSharp.Attributes" Version="2025.3.27" />
36+
<PackageReference Include="DisCatSharp.Common" Version="[10.7.0-nightly-049,)" />
37+
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
38+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.1" />
39+
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.1" />
40+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
41+
</ItemGroup>
42+
43+
<ItemGroup>
44+
<ProjectReference Include="..\DisCatSharp.Extensions.Translations/DisCatSharp.Extensions.Translations.csproj" />
45+
</ItemGroup>
46+
47+
<ItemGroup>
48+
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
49+
</ItemGroup>
50+
51+
</Project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// This file is part of the DisCatSharp project.
2+
//
3+
// Copyright (c) 2021-2023 AITSYS
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
using DisCatSharp.ApplicationCommands.Context;
24+
25+
namespace DisCatSharp.Extensions.Translations.ApplicationCommands;
26+
27+
/// <summary>
28+
/// Defines various extensions specific to Translator.
29+
/// </summary>
30+
public static class ExtensionMethods
31+
{
32+
/// <summary>
33+
/// Translates a key using the locale from the <see cref="InteractionContext"/>.
34+
/// </summary>
35+
/// <param name="ctx">The interaction context.</param>
36+
/// <param name="key">The translation key.</param>
37+
/// <param name="placeholders">The placeholders to replace in the translation.</param>
38+
/// <param name="forceGuildLocale">Whether to force the guild locale.</param>
39+
/// <returns>The translated string.</returns>
40+
public static string T(this InteractionContext ctx, string key, object? placeholders = null, bool forceGuildLocale = false)
41+
{
42+
var locale = Translator.DefaultLocale;
43+
if (forceGuildLocale && !string.IsNullOrWhiteSpace(ctx.GuildLocale))
44+
locale = ctx.GuildLocale;
45+
else if (!string.IsNullOrWhiteSpace(ctx.Locale))
46+
locale = ctx.Locale;
47+
else if (!string.IsNullOrWhiteSpace(ctx.GuildLocale))
48+
locale = ctx.GuildLocale;
49+
return locale.TLocale(key, placeholders);
50+
}
51+
52+
/// <summary>
53+
/// Translates a key using the locale from the <see cref="ContextMenuContext"/>.
54+
/// </summary>
55+
/// <param name="ctx">The context menu context.</param>
56+
/// <param name="key">The translation key.</param>
57+
/// <param name="placeholders">The placeholders to replace in the translation.</param>
58+
/// <param name="forceGuildLocale">Whether to force the guild locale.</param>
59+
/// <returns>The translated string.</returns>
60+
public static string T(this ContextMenuContext ctx, string key, object? placeholders = null, bool forceGuildLocale = false)
61+
{
62+
var locale = Translator.DefaultLocale;
63+
if (forceGuildLocale && !string.IsNullOrWhiteSpace(ctx.GuildLocale))
64+
locale = ctx.GuildLocale;
65+
else if (!string.IsNullOrWhiteSpace(ctx.Locale))
66+
locale = ctx.Locale;
67+
else if (!string.IsNullOrWhiteSpace(ctx.GuildLocale))
68+
locale = ctx.GuildLocale;
69+
return locale.TLocale(key, placeholders);
70+
}
71+
72+
/// <summary>
73+
/// Translates a key using the locale from the <see cref="AutocompleteContext"/>.
74+
/// </summary>
75+
/// <param name="ctx">The autocomplete context.</param>
76+
/// <param name="key">The translation key.</param>
77+
/// <param name="placeholders">The placeholders to replace in the translation.</param>
78+
/// <param name="forceGuildLocale">Whether to force the guild locale.</param>
79+
/// <returns>The translated string.</returns>
80+
public static string T(this AutocompleteContext ctx, string key, object? placeholders = null, bool forceGuildLocale = false)
81+
{
82+
var locale = Translator.DefaultLocale;
83+
if (forceGuildLocale && !string.IsNullOrWhiteSpace(ctx.GuildLocale))
84+
locale = ctx.GuildLocale;
85+
else if (!string.IsNullOrWhiteSpace(ctx.Locale))
86+
locale = ctx.Locale;
87+
else if (!string.IsNullOrWhiteSpace(ctx.GuildLocale))
88+
locale = ctx.GuildLocale;
89+
return locale.TLocale(key, placeholders);
90+
}
91+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<Project Sdk="Microsoft.NET.Sdk">
4+
5+
<Import Project="../Targets/Version.targets" />
6+
<Import Project="../Targets/DisCatSharp.targets" />
7+
<Import Project="../Targets/Package.targets" />
8+
<Import Project="../Targets/NuGet.targets" />
9+
<Import Project="../Targets/Library.targets" />
10+
11+
<PropertyGroup>
12+
<AssemblyName>DisCatSharp.Extensions.Translations.CommandsNext</AssemblyName>
13+
<RootNamespace>DisCatSharp.Extensions.Translations.CommandsNext</RootNamespace>
14+
</PropertyGroup>
15+
16+
<PropertyGroup>
17+
<PackageId>DisCatSharp.Extensions.Translations.CommandsNext</PackageId>
18+
<Description>
19+
DisCatSharp.Extensions.Translations.CommandsNext
20+
21+
Extension making it easy to use localizations in CommandsNext commands.
22+
</Description>
23+
<PackageTags>DisCatSharp,DisCatSharp Extension,Translations,Localization,CommandsNext,Discord,Bots,Discord Bots,AITSYS,Net8,Net9,Net10</PackageTags>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<PackageReference Include="DisCatSharp" Version="[10.7.0-nightly-049,)" />
28+
<PackageReference Include="DisCatSharp.Analyzer" Version="1.0.0">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
31+
</PackageReference>
32+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.1" />
33+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="10.0.1" />
34+
<PackageReference Include="DisCatSharp.CommandsNext" Version="[10.7.0-nightly-049,)" />
35+
<PackageReference Include="DisCatSharp.Attributes" Version="2025.3.27" />
36+
<PackageReference Include="DisCatSharp.Common" Version="[10.7.0-nightly-049,)" />
37+
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
38+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.1" />
39+
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.1" />
40+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
41+
</ItemGroup>
42+
43+
<ItemGroup>
44+
<ProjectReference Include="..\DisCatSharp.Extensions.Translations/DisCatSharp.Extensions.Translations.csproj" />
45+
</ItemGroup>
46+
47+
<ItemGroup>
48+
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
49+
</ItemGroup>
50+
51+
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// This file is part of the DisCatSharp project.
2+
//
3+
// Copyright (c) 2021-2023 AITSYS
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
// This file is part of the DisCatSharp project.
24+
//
25+
// Copyright (c) 2021-2023 AITSYS
26+
//
27+
// Permission is hereby granted, free of charge, to any person obtaining a copy
28+
// of this software and associated documentation files (the "Software"), to deal
29+
// in the Software without restriction, including without limitation the rights
30+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31+
// copies of the Software, and to permit persons to whom the Software is
32+
// furnished to do so, subject to the following conditions:
33+
//
34+
// The above copyright notice and this permission notice shall be included in all
35+
// copies or substantial portions of the Software.
36+
//
37+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43+
// SOFTWARE.
44+
45+
using DisCatSharp.CommandsNext;
46+
47+
namespace DisCatSharp.Extensions.Translations.CommandsNext;
48+
49+
/// <summary>
50+
/// Defines various extensions specific to Translator.
51+
/// </summary>
52+
public static class ExtensionMethods
53+
{
54+
55+
56+
/// <summary>
57+
/// Translates a key using the locale from the <see cref="CommandContext"/>.
58+
/// </summary>
59+
/// <param name="ctx">The autocomplete context.</param>
60+
/// <param name="key">The translation key.</param>
61+
/// <param name="placeholders">The placeholders to replace in the translation.</param>
62+
/// <param name="forceGuildLocale">Whether to force the guild locale.</param>
63+
/// <returns>The translated string.</returns>
64+
public static string T(this CommandContext ctx, string key, object? placeholders = null, bool forceGuildLocale = false)
65+
{
66+
var locale = Translator.DefaultLocale;
67+
if (forceGuildLocale || !string.IsNullOrWhiteSpace(ctx.Guild?.PreferredLocale))
68+
locale = ctx.Guild?.PreferredLocale;
69+
return locale.TLocale(key, placeholders);
70+
}
71+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<Project Sdk="Microsoft.NET.Sdk">
4+
5+
<Import Project="../Targets/Version.targets" />
6+
<Import Project="../Targets/DisCatSharp.targets" />
7+
<Import Project="../Targets/Package.targets" />
8+
<Import Project="../Targets/NuGet.targets" />
9+
<Import Project="../Targets/Library.targets" />
10+
11+
<PropertyGroup>
12+
<AssemblyName>DisCatSharp.Extensions.Translations</AssemblyName>
13+
<RootNamespace>DisCatSharp.Extensions.Translations</RootNamespace>
14+
</PropertyGroup>
15+
16+
<PropertyGroup>
17+
<PackageId>DisCatSharp.Extensions.Translations</PackageId>
18+
<Description>
19+
DisCatSharp.Extensions.Translations
20+
21+
Core extension for DisCatSharp making it easy to use localizations in your bot.
22+
</Description>
23+
<PackageTags>DisCatSharp,DisCatSharp Extension,Translations,Localization,Discord,Bots,Discord Bots,AITSYS,Net8,Net9,Net10</PackageTags>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<PackageReference Include="DisCatSharp" Version="[10.7.0-nightly-049,)" />
28+
<PackageReference Include="DisCatSharp.Analyzer" Version="1.0.0">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
31+
</PackageReference>
32+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.1" />
33+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="10.0.1" />
34+
<PackageReference Include="DisCatSharp.Attributes" Version="2025.3.27" />
35+
<PackageReference Include="DisCatSharp.Common" Version="[10.7.0-nightly-049,)" />
36+
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
37+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.1" />
38+
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.1" />
39+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
40+
</ItemGroup>
41+
42+
<ItemGroup>
43+
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
44+
</ItemGroup>
45+
46+
</Project>

0 commit comments

Comments
 (0)